在好例子网,分享、交流、成长!
您当前所在位置:首页Java 开发实例Android平台开发 → 人脸年龄识别

人脸年龄识别

Android平台开发

下载此实例
  • 开发语言:Java
  • 实例大小:1.53M
  • 下载次数:58
  • 浏览次数:1207
  • 发布时间:2015-07-06
  • 实例类别:Android平台开发
  • 发 布 人:aa201b
  • 文件格式:.rar
  • 所需积分:2
 相关标签: 人脸年龄识别

实例介绍

【实例简介】

最近How-Old很火呀,正好又看到了hongyang大大的视频,于是写了一个小demo,加入了一些自己的想法。

【实例截图】

girl.png

【核心代码】

public class MainActivity extends Activity implements OnClickListener {
private ImageView iv_photo;
private Button btn_select, btn_take, btn_detect;
private TextView tv_age;
private String photoPath;
private Bitmap photo;
private ProgressDialog dialog;
private Paint paint;
private Handler mHandler = new Handler() {
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 0x1:// 检测成功
JSONObject result = (JSONObject) msg.obj;
handleResult(result);
iv_photo.setImageBitmap(photo);
break;
case 0x2:// 检测失败
String error = (String) msg.obj;
Toast.makeText(MainActivity.this, "检测失败:" error,
Toast.LENGTH_LONG).show();
break;
}
if (dialog != null) {
dialog.dismiss();
dialog = null;
}
};
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);

btn_detect = (Button) findViewById(R.id.btn_detect);
btn_select = (Button) findViewById(R.id.btn_select);
btn_take = (Button) findViewById(R.id.btn_take);
iv_photo = (ImageView) findViewById(R.id.iv_photo);
btn_detect.setOnClickListener(this);
btn_select.setOnClickListener(this);
btn_take.setOnClickListener(this);
tv_age = (TextView) findViewById(R.id.tv_age);
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.WHITE);
paint.setStrokeWidth(3);
}

protected void handleResult(JSONObject result) {
Bitmap bitmap = Bitmap.createBitmap(photo.getWidth(),
photo.getHeight(), photo.getConfig());
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(photo, 0, 0, null);
try {
JSONArray faces = result.getJSONArray("face");
int faceCount = faces.length();
if (faceCount == 0) {
Toast.makeText(this, "没有检测到人脸~", Toast.LENGTH_LONG).show();
} else {
for (int i = 0; i < faceCount; i ) {
JSONObject face = faces.getJSONObject(i);
JSONObject position = face.getJSONObject("position");
float cx = (float) position.getJSONObject("center")
.getDouble("x");
float cy = (float) position.getJSONObject("center")
.getDouble("y");
float width = (float) position.getDouble("width");
float height = (float) position.getDouble("height");
cx = cx / 100 * bitmap.getWidth();
cy = cy / 100 * bitmap.getHeight();
width = width / 100 * bitmap.getWidth();
height = height / 100 * bitmap.getHeight();

canvas.drawLine(cx - width / 2, cy - height / 2, cx - width
/ 2, cy height / 2, paint);
canvas.drawLine(cx - width / 2, cy - height / 2, cx width
/ 2, cy - height / 2, paint);
canvas.drawLine(cx width / 2, cy - height / 2, cx width
/ 2, cy height / 2, paint);
canvas.drawLine(cx - width / 2, cy height / 2, cx width
/ 2, cy height / 2, paint);

int age = face.getJSONObject("attribute")
.getJSONObject("age").getInt("value");
String gender = face.getJSONObject("attribute")
.getJSONObject("gender").getString("value");

Bitmap ageBm = buildAgeBm(age, gender);
int ageWidth = ageBm.getWidth();
int ageHeight = ageBm.getHeight();
if (bitmap.getWidth() < iv_photo.getWidth()
&& bitmap.getHeight() < iv_photo.getHeight()) {
float ratio = Math.max(bitmap.getWidth() * 1.0f
/ iv_photo.getWidth(), bitmap.getHeight()
* 1.0f / iv_photo.getWidth());
ageBm = Bitmap.createScaledBitmap(ageBm,
(int) (ageWidth * ratio),
(int) (ageHeight * ratio), false);
}
canvas.drawBitmap(ageBm, cx - ageBm.getWidth() / 2, cy
- height / 2 - ageBm.getHeight(), null);
photo = bitmap;
}
}
} catch (JSONException e) {
e.printStackTrace();
}

}

private Bitmap buildAgeBm(int age, String gender) {
tv_age.setText(age "");
if ("Female".equals(gender)) {
tv_age.setCompoundDrawablesWithIntrinsicBounds(getResources()
.getDrawable(R.drawable.female), null, null, null);
} else {
tv_age.setCompoundDrawablesWithIntrinsicBounds(getResources()
.getDrawable(R.drawable.male), null, null, null);
}
tv_age.setDrawingCacheEnabled(true);
Bitmap bm = Bitmap.createBitmap(tv_age.getDrawingCache());
tv_age.destroyDrawingCache();
return bm;
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_detect:
if (photo == null) {
btn_select.performClick();
} else {
dialog = ProgressDialog
.show(MainActivity.this, null, "正在检测...");
dialog.setCancelable(false);
Util.detect(photo, new Callback() {

@Override
public void onSuccess(JSONObject result) {
Message msg = Message.obtain();
msg.what = 0x1;
msg.obj = result;
mHandler.sendMessage(msg);
}

@Override
public void onFail(FaceppParseException e) {
Message msg = Message.obtain();
msg.what = 0x2;
msg.obj = e.getErrorMessage();
mHandler.sendMessage(msg);
}
});
}

break;
case R.id.btn_select:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 101);
break;
case R.id.btn_take:
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
Intent getImageByCamera = new Intent(
"android.media.action.IMAGE_CAPTURE");
File dir = new File(Util.SaveDir);
if (!dir.exists()) {
dir.mkdirs();
}
photoPath = Util.SaveDir System.currentTimeMillis() ".jpg";
getImageByCamera.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File(photoPath)));
getImageByCamera.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(getImageByCamera, 102);
System.out.println(photoPath);
} else {
Toast.makeText(getApplicationContext(), "请确认已经插入SD卡",
Toast.LENGTH_LONG).show();
}
break;
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 101) {// 从相册中选取
if (data != null) {
Uri uri = data.getData();
Cursor cursor = getContentResolver().query(uri, null, null,
null, null);
if (cursor.moveToFirst()) {
int index = cursor
.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
photoPath = cursor.getString(index);
cursor.close();
getPhoto();
iv_photo.setImageBitmap(photo);
}
}
} else if (requestCode == 102) {// 拍照获取照片
getPhoto();
iv_photo.setImageBitmap(photo);
// Uri uri = data.getData();
// if (uri == null) {// 兼容机型的不同
// Bundle bundle = data.getExtras();
// if (bundle != null) {
// photo = (Bitmap) bundle.get("data");
// iv_photo.setImageBitmap(photo);
// }
// } else {
// Cursor cursor = getContentResolver().query(uri, null, null,
// null, null);
// if (cursor.moveToFirst()) {
// int index = cursor
// .getColumnIndex(MediaStore.Images.ImageColumns.DATA);
// photoPath = cursor.getString(index);
// cursor.close();
// getPhoto();
// iv_photo.setImageBitmap(photo);
//
// }
// }
}
super.onActivityResult(requestCode, resultCode, data);
}

private void getPhoto() {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(photoPath, options);
options.inJustDecodeBounds = false;
float h = iv_photo.getHeight();
float w = iv_photo.getWidth();
// int ratio = 1;
double ratio = Math.max(options.outWidth * 1.0d / 1024f,
options.outHeight * 1.0d / 1024f);
// System.out.println(options.outWidth ":" options.outHeight "::"
// w ":" h);
// if (options.outWidth > options.outHeight && options.outWidth > w) {//
// 如果宽度大的话,则根据宽度缩放图片
// ratio = (int) (options.outWidth / w);
// } else if (options.outHeight > options.outWidth
// && options.outHeight > h) {
// ratio = (int) (options.outHeight / h);
// }
options.inSampleSize = (int) Math.ceil(ratio);
photo = BitmapFactory.decodeFile(photoPath, options);
}
}


标签: 人脸年龄识别

实例下载地址

人脸年龄识别

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警