package cliu.TutorialOnFaceDetect;

/*
* MyImageView.java
* Download by http://www.codefans.net
* [AUTHOR]: Chunyen Liu
* [SDK ]: Android SDK 2.1 and up
* [NOTE ]: developer.com tutorial, "Face Detection with Android APIs"
*/

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.ImageView;

class MyImageView extends ImageView {
private Bitmap mBitmap;
private Canvas mCanvas;
private int mBitmapWidth = 200;
private int mBitmapHeight = 200;
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private int mDisplayStyle = 0;
private int [] mPX = null;
private int [] mPY = null;

public MyImageView(Context c) {
super(c);
init();
}

public MyImageView(Context c, AttributeSet attrs) {
super(c, attrs);
init();
}

private void init() {
mBitmap = Bitmap.createBitmap(mBitmapWidth, mBitmapHeight, Bitmap.Config.RGB_565);
mCanvas = new Canvas(mBitmap);

mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setColor(0x80ff0000);
mPaint.setStrokeWidth(3);
}

public Bitmap getBitmap() {
return mBitmap;
}

@Override
public void setImageBitmap(Bitmap bm) {
if (bm != null) {
mBitmapWidth = bm.getWidth();
mBitmapHeight = bm.getHeight();

mBitmap = Bitmap.createBitmap(mBitmapWidth, mBitmapHeight, Bitmap.Config.RGB_565);
mCanvas = new Canvas();
mCanvas.setBitmap(mBitmap);
mCanvas.drawBitmap(bm, 0, 0, null);
}

super.setImageBitmap(bm);
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);

mBitmapWidth = (mBitmap != null) ? mBitmap.getWidth() : 0;
mBitmapHeight = (mBitmap != null) ? mBitmap.getHeight() : 0;
if (mBitmapWidth == w && mBitmapHeight == h) {
return;
}

if (mBitmapWidth < w) mBitmapWidth = w;
if (mBitmapHeight < h) mBitmapHeight = h;
}

// set up detected face features for display
public void setDisplayPoints(int [] xx, int [] yy, int total, int style) {
mDisplayStyle = style;
mPX = null;
mPY = null;

if (xx != null && yy != null && total > 0) {
mPX = new int[total];
mPY = new int[total];

for (int i = 0; i < total; i++) {
mPX[i] = xx[i];
mPY[i] = yy[i];
}
}
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

if (mBitmap != null) {
canvas.drawBitmap(mBitmap, 0, 0, null);

if (mPX != null && mPY != null) {
for (int i = 0; i < mPX.length; i++) {
if (mDisplayStyle == 1) {
canvas.drawCircle(mPX[i], mPY[i], 10.0f, mPaint);
} else {
canvas.drawRect(mPX[i] - 20, mPY[i] - 20, mPX[i] + 20, mPY[i] + 20, mPaint);
}
}
}
}
}
}

----------------------------------------------

package cliu.TutorialOnFaceDetect;

/*
* TutorialOnFaceDetect
* Download by http://www.codefans.net
* [AUTHOR]: Chunyen Liu
* [SDK ]: Android SDK 2.1 and up
* [NOTE ]: developer.com tutorial, "Face Detection with Android APIs"
*/

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PointF;
import android.media.FaceDetector;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.LinearLayout.LayoutParams;

public class TutorialOnFaceDetect extends Activity {
private MyImageView mIV;
private Bitmap mFaceBitmap;
private int mFaceWidth = 200;
private int mFaceHeight = 200;
private static final int MAX_FACES = 10;
private static String TAG = "TutorialOnFaceDetect";
private static boolean DEBUG = false;

protected static final int GUIUPDATE_SETFACE = 999;
protected Handler mHandler = new Handler(){
// @Override
public void handleMessage(Message msg) {
mIV.invalidate();
super.handleMessage(msg);
}
};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mIV = new MyImageView(this);
setContentView(mIV, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

// load the photo
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.face3);
mFaceBitmap = b.copy(Bitmap.Config.RGB_565, true);
b.recycle();

mFaceWidth = mFaceBitmap.getWidth();
mFaceHeight = mFaceBitmap.getHeight();
mIV.setImageBitmap(mFaceBitmap);
mIV.invalidate();

// perform face detection in setFace() in a background thread
doLengthyCalc();
}

public void setFace() {
FaceDetector fd;
FaceDetector.Face [] faces = new FaceDetector.Face[MAX_FACES];
PointF eyescenter = new PointF();
float eyesdist = 0.0f;
int [] fpx = null;
int [] fpy = null;
int count = 0;

try {
fd = new FaceDetector(mFaceWidth, mFaceHeight, MAX_FACES);
count = fd.findFaces(mFaceBitmap, faces);
} catch (Exception e) {
Log.e(TAG, "setFace(): " + e.toString());
return;
}

// check if we detect any faces
if (count > 0) {
fpx = new int[count * 2];
fpy = new int[count * 2];

for (int i = 0; i < count; i++) {
try {
faces[i].getMidPoint(eyescenter);
eyesdist = faces[i].eyesDistance();

// set up left eye location
fpx[2 * i] = (int)(eyescenter.x - eyesdist / 2);
fpy[2 * i] = (int)eyescenter.y;

// set up right eye location
fpx[2 * i + 1] = (int)(eyescenter.x + eyesdist / 2);
fpy[2 * i + 1] = (int)eyescenter.y;

if (DEBUG)
Log.e(TAG, "setFace(): face " + i + ": confidence = " + faces[i].confidence()
+ ", eyes distance = " + faces[i].eyesDistance()
+ ", pose = ("+ faces[i].pose(FaceDetector.Face.EULER_X) + ","
+ faces[i].pose(FaceDetector.Face.EULER_Y) + ","
+ faces[i].pose(FaceDetector.Face.EULER_Z) + ")"
+ ", eyes midpoint = (" + eyescenter.x + "," + eyescenter.y +")");
} catch (Exception e) {
Log.e(TAG, "setFace(): face " + i + ": " + e.toString());
}
}
}

mIV.setDisplayPoints(fpx, fpy, count * 2, 1);
}

private void doLengthyCalc() {
Thread t = new Thread() {
Message m = new Message();

public void run() {
try {
setFace();
m.what = TutorialOnFaceDetect.GUIUPDATE_SETFACE;
TutorialOnFaceDetect.this.mHandler.sendMessage(m);
} catch (Exception e) {
Log.e(TAG, "doLengthyCalc(): " + e.toString());
}
}
};

t.start();
}

}

-------------------------------------------------------

package cliu.TutorialOnFaceDetect;

/*
* TutorialOnFaceDetect1
* Download by http://www.codefans.net
* [AUTHOR]: Chunyen Liu
* [SDK ]: Android SDK 2.1 and up
* [NOTE ]: developer.com tutorial, "Face Detection with Android APIs"
*/

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PointF;
import android.media.FaceDetector;
import android.os.Bundle;
import android.util.Log;
import android.widget.LinearLayout.LayoutParams;

public class TutorialOnFaceDetect1 extends Activity {
private MyImageView mIV;
private Bitmap mFaceBitmap;
private int mFaceWidth = 200;
private int mFaceHeight = 200;
private static final int MAX_FACES = 10;
private static String TAG = "TutorialOnFaceDetect";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mIV = new MyImageView(this);
setContentView(mIV, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

// load the photo
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.face3);
mFaceBitmap = b.copy(Bitmap.Config.RGB_565, true);
b.recycle();

mFaceWidth = mFaceBitmap.getWidth();
mFaceHeight = mFaceBitmap.getHeight();
mIV.setImageBitmap(mFaceBitmap);

// perform face detection and set the feature points
setFace();

mIV.invalidate();
}

public void setFace() {
FaceDetector fd;
FaceDetector.Face [] faces = new FaceDetector.Face[MAX_FACES];
PointF midpoint = new PointF();
int [] fpx = null;
int [] fpy = null;
int count = 0;

try {
fd = new FaceDetector(mFaceWidth, mFaceHeight, MAX_FACES);
count = fd.findFaces(mFaceBitmap, faces);
} catch (Exception e) {
Log.e(TAG, "setFace(): " + e.toString());
return;
}

// check if we detect any faces
if (count > 0) {
fpx = new int[count];
fpy = new int[count];

for (int i = 0; i < count; i++) {
try {
faces[i].getMidPoint(midpoint);

fpx[i] = (int)midpoint.x;
fpy[i] = (int)midpoint.y;
} catch (Exception e) {
Log.e(TAG, "setFace(): face " + i + ": " + e.toString());
}
}
}

mIV.setDisplayPoints(fpx, fpy, count, 0);
}
}

人脸检测的API例子的更多相关文章

  1. caffe_实战之两个简单的例子(物体分类和人脸检测)

    一.物体分类: 这里使用的是caffe官网中自带的例子,我这里主要是对代码的解释~ 首先导入一些必要的库: import caffe import numpy as np import matplot ...

  2. paper 88:人脸检测和识别的Web服务API

    本文汇总了全球范围内提供基于Web服务的人脸检测和识别的API,便于网络中快速部署和人脸相关的一些应用. 1:从How-old的火爆说起 最开始,网站的开发者只是给一个几百人的群发送email,请他们 ...

  3. [转]40多个关于人脸检测/识别的API、库和软件

    [转]40多个关于人脸检测/识别的API.库和软件 http://news.cnblogs.com/n/185616/ 英文原文:List of 40+ Face Detection / Recogn ...

  4. 40多个关于人脸检测/识别的API、库和软件

    英文原文:List of 40+ Face Detection / Recognition APIs, libraries, and software 译者:@吕抒真 译文:链接 自从谷歌眼镜被推出以 ...

  5. 转:40多个关于人脸检测/识别的API、库和软件

    文章来自于:http://blog.jobbole.com/45936/ 自从谷歌眼镜被推出以来,围绕人脸识别,出现了很多争议.我们相信,不管是不是通过智能眼镜,人脸识别将在人与人交往甚至人与物交互中 ...

  6. javacv 340使用 人脸检测例子【转载】

    Java下使用opencv进行人脸检测 工作需要,研究下人脸识别,发现opencv比较常用,尽管能检测人脸,但识别率不高,多数是用来获取摄像头的视频流的,提取里面的视频帧,实现人脸识别时通常会和其他框 ...

  7. 基于TensorFlow Object Detection API进行迁移学习训练自己的人脸检测模型(二)

    前言 已完成数据预处理工作,具体参照: 基于TensorFlow Object Detection API进行迁移学习训练自己的人脸检测模型(一) 设置配置文件 新建目录face_faster_rcn ...

  8. 虹软人脸检测和识别C# - API

    using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D ...

  9. OpenCV + Python 人脸检测

    必备知识 Haar-like opencv api 读取图片 灰度转换 画图 显示图像 获取人脸识别训练数据 探测人脸 处理人脸探测的结果 实例 图片素材 人脸检测代码 人脸检测结果 总结 下午的时候 ...

随机推荐

  1. 不能读取文件“itunes.library.itl”因为它是由更高级别的itunes所创建的

    转自:https://zhidao.baidu.com/question/80796363.html 是因为你安装过高版本的后又装你版本的itunes. 你在电脑上搜索所有硬盘上的itunes lib ...

  2. 旅行计划-DAG上最长路

    http://www.luogu.org/problem/show?pid=1137 题目描述 小明要去一个国家旅游.这个国家有N个城市,编号为1-N,并且有M条道路连接着,小明准备从其中一个城市出发 ...

  3. 学习总结 html图片热点,网页划区,拼接,表单

    表单: action="负责处理的 <form id="" name="" method="post/get"服务端&quo ...

  4. VS/Visual studio 源代码编辑器里的空处出现点号解决办法

    此原因是不小心按错了键盘上的组合键Ctr+E+S, 再次按Ctr+E+S可消除.

  5. lib制作

    生成模拟器和真机通用lib命令: lipo -create libKIF-os.a libKIF-simulator.a -output libKIF.a. 需要cd到  愿文件.a所在的目录. li ...

  6. 如果公司里有上百个表要做触发器,如果手动写代码的话。很累,所以今天写了一个小程序,自动生成mysql的触发代码。

    <?php $dbname = 'test';//数据库 $tab1 = 'user'; //执行的表 $tab2 = 'user_bak'; //被触发的表 $conn = mysql_con ...

  7. MITM to crack Https connections

    Everybody knows that https is http over SSL, and https is a secure way for protecting confidential d ...

  8. Android窗口为弹出框样式

    1.XML android:theme="@android:style/Theme.Dialog <?xml version="1.0" encoding=&quo ...

  9. Linux下对于inode的理解

    0x01 什么是inode 文件存储在硬盘上,硬盘的最小存储单位叫做“扇区”(Sector),每个扇区储存512字节: 操作系统读取硬盘时,不会一个个扇区地读取,这样效率低,而是一次性连续读取多个扇区 ...

  10. EasyUI-Combox

    Combox的数据格式和默认选中项设置 [{ "id":1, "text":"text1" },{ "id":2, &q ...