写了一个简单的调用百度人脸识别在线的JAVA接口,识别率挺高的,但是目前写得还不是很全面,然后打开相册选取图片那一部分,以及工具类代码,是有参考别人写的代码,具体是摘自哪里之前没有保存。
这里贴一些主要的代码。
人脸对比部分:
package ss.demo14;
import android.Manifest;
import android.content.ContentUris;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.chainsaw.Main;
import java.io.File;
import java.text.DecimalFormat;
import ss.demo14.mets.CFace;
/*
*@Author:Swallow
*@Date:2018/12/12
*/
public class FaceActivity extends AppCompatActivity implements View.OnClickListener {
public static final int SELECT_PHOTO1 = 2;
public static final int SELECT_PHOTO2 = 3;
public static final int TAKE_PHOTO = 4;
private ImageView imageViewShow1;
private ImageView imageViewShow2;
public TextView tv_cmp;
String path1, path2,fsres;
Button btn1, btn2, btn_make;
//private Bitmap orc_bitmap;
//新建Handler的对象,在这里接收Message,然后更新TextView控件的内容
//这一部分暂时没时间完成
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_face);
btn1 = (Button) findViewById(R.id.photo1);
btn2 = (Button) findViewById(R.id.photo2);
btn_make = (Button) findViewById(R.id.make);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn_make.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.photo1:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}else{
openAlbum1();
}
break;
case R.id.photo2:
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}else {
//opencare();
openAlbum2();
}
break;
case R.id.make:
if(path1!=null&&path2!=null){
sendRequestWithHttpClient();
tv_cmp = (TextView)findViewById(R.id.cmp);
tv_cmp.setText(fsres);
}else{
Toast.makeText(this, "图片获取失败", Toast.LENGTH_LONG).show();
}
break;
}
}
//开启子线程
private void sendRequestWithHttpClient() {
new Thread(new Runnable() {
@Override
public void run() {
CFace cf = new CFace();
String ss = cf.cface(path1, path2);
//获取JSON字符串中的数据
String result = JSONObject.parseObject(ss).getString("result");
float score = JSONObject.parseObject(result).getFloatValue("score");
Log.v("score", "score"+score);
//Double so = score;
DecimalFormat df = new DecimalFormat("0.00");
Double cny = Double.parseDouble(df.format(score));
String fsres = face(cny);
}
}).start();
}
public String face(double d){
//ss=null;
if(d>=75.00){
fsres = "这两个人是同一个人";
}
else {
fsres = "这两个人不是同一个人";
}
Log.v("ss",fsres);
return fsres;
}
//按钮一事件
public void openAlbum1() {
Intent intent = new Intent("android.intent.action.GET_CONTENT");
intent.setType("image/*");
startActivityForResult(intent, SELECT_PHOTO1);
}
//按钮二事件
public void openAlbum2() {
Intent intent = new Intent("android.intent.action.GET_CONTENT");
intent.setType("image/*");
startActivityForResult(intent, SELECT_PHOTO2);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
//打开相册后返回
case SELECT_PHOTO1:
if (resultCode == RESULT_OK) {
//判断手机系统版本号
if (Build.VERSION.SDK_INT > 19) {
path1 = handleImgeOnKitKat(data);
Bitmap bmap1 = displayImage(path1);//获取图片Bitmap显示到对应的控件上
if (bmap1 != null) {
imageViewShow1 = (ImageView) findViewById(R.id.iv_show1);
imageViewShow1.setImageBitmap(bmap1);
}
} else {
Toast.makeText(this, "图片获取失败", Toast.LENGTH_LONG).show();
}
}
break;
case SELECT_PHOTO2:
if (resultCode == RESULT_OK) {
//判断手机系统版本号
if (Build.VERSION.SDK_INT > 19) {
//4.4及以上系统使用这个方法处理图片
path2 = handleImgeOnKitKat(data);
Bitmap bmap2 = displayImage(path2);
if (bmap2 != null) {
imageViewShow2 = (ImageView) findViewById(R.id.iv_show2);
imageViewShow2.setImageBitmap(bmap2);
}
else {
Toast.makeText(this, "图片获取失败", Toast.LENGTH_LONG).show();
}
}
}break;
}
//API19以上的图片处理方法
private String handleImgeOnKitKat(Intent data) {
String path=null;
Uri uri = data.getData();
Log.d("uri=intent.getData :", "" + uri);
if (DocumentsContract.isDocumentUri(this, uri)) {
String docId = DocumentsContract.getDocumentId(uri); //数据表里指定的行
Log.d("getDocumentId(uri) :", "" + docId);
Log.d("uri.getAuthority() :", "" + uri.getAuthority());
if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
String id = docId.split(":")[1];
String selection = MediaStore.Images.Media._ID + "=" + id;
path = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
} else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId));
path = getImagePath(contentUri, null);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
path = getImagePath(uri, null);
}
Log.v("path:",path);
return path;
//displayImage(path);
}
private Bitmap displayImage(String imagePath) {
Bitmap orc_bitmap = null;
if (!TextUtils.isEmpty(imagePath)) {
orc_bitmap = BitmapFactory.decodeFile(imagePath);//获取图片
}
return orc_bitmap;
}
//获取图片的真实路径
private String getImagePath(Uri uri, String selection) {
String path = null;
Cursor cursor = getContentResolver().query(uri, null, selection, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
}
cursor.close();
}
return path;
}
}
检测人脸信息的部分
package ss.demo14;
import android.Manifest;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.ContentUris;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import ss.demo14.mets.Find;
/*
*@Author:Swallow
*@Date:2018/12/12
*/
public class MessgActivity extends AppCompatActivity implements View.OnClickListener{
public Button addph,putmes,btn_list;
public ImageView ivphoto;
public TextView tvnum;
public static final int SELECT_PHOTO = 2;
public String path=null;
private int num;
private Bitmap orc_bitmap;
private ListView showlv;
private List<Map<String, Object>> list = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_messg);
addph = (Button)findViewById(R.id.add_photo);
putmes = (Button)findViewById(R.id.b_check);
btn_list = (Button)findViewById(R.id.tolist);
addph.setOnClickListener(this);
putmes.setOnClickListener(this);
btn_list.setOnClickListener(this);
}
@SuppressLint("WrongViewCast")
@Override
public void onClick(View v) {
switch (v.getId()) {
//添加一张照片,获取照片路径显示到控件上面,并且把路径值传给接口
case R.id.add_photo:
//判断权限是否打开
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
} else {
openAlbum();//打开相册
}
break;
case R.id.b_check:
//如果已经选择图片,传入图片路径值给接口,如果没有选择图片则弹出提示
if(path!=null){
//获取到的User信息集合
list = new ArrayList<Map<String, Object>>();
//开启新的子线程
new Thread(new Runnable(){
@Override
public void run() {
Find fn = new Find();
//String path="/storage/emulated/0/Download/a5.jpg";
String mas = fn.findfacemsg(path);
//获取JSON字符串中的数据
//对取得的JSON数据进行解析,获取人脸列表当中的具体数据
String res = JSONObject.parseObject(mas).getString("result");
Log.v("res=",res);
String facelist = JSONObject.parseObject(res).getString("face_list");
Log.v("list",facelist);
num = JSONObject.parseObject(res).getInteger("face_num");
Log.v("num=",""+num);
//获取人脸列表数组
JSONArray jsonArray = JSON.parseArray(facelist);
//人脸列表当中的数据循环重构到list当中
for (int i = 0; i < num; i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
//race数组中获取人种
String racelist = jsonObject.getString("race");
String race = JSONObject.parseObject(racelist).getString("type");
//判断人种
switch (race){
case "yellow":
race = "黄种人";
break;
case "white":
race = "白种人";
break;
case "black":
race = "黑种人";
break;
case "arabs":
race = "阿拉伯人";
break;
}
//gender数组中获取性别
String genderlist = jsonObject.getString("gender");
String gender = JSONObject.parseObject(genderlist).getString("type");
//判断性别
switch (gender){
case "female":
gender = "女";
break;
case "male":
gender = "男";
break;
}
int age = jsonObject.getInteger("age");
Log.v("listmes:",age+race+gender);
//数据传入
Map<String, Object> map = new HashMap<String, Object>();
map.put("f_num", i+1);
map.put("f_sex,", gender);
map.put("f_age", age);
map.put("f_type", race);
list.add(map);
}
}
}).start();
}else {
Toast.makeText(this, "没有选取图片", Toast.LENGTH_LONG).show();
}
break;
case R.id.tolist:
//将获取的数据显示到界面当中
showlv = (ListView)findViewById(R.id.listView1);
//构造listview的适配器
SimpleAdapter adapter = new SimpleAdapter(this, list
, R.layout.faceshow
, new String[] { "f_num", "f_sex,", "f_age","f_type" }
, new int[] {R.id.f_num, R.id.f_sex, R.id.f_age, R.id.f_type});
showlv.setAdapter(adapter);
String count = Integer.toString(num);
tvnum = (TextView)findViewById(R.id.tv_num);
tvnum.setText(count);
break;
}
}
private void openAlbum() {
Intent intent = new Intent("android.intent.action.GET_CONTENT");
intent.setType("image/*");
startActivityForResult(intent, SELECT_PHOTO);
}
@TargetApi(Build.VERSION_CODES.KITKAT)
private void handleImgeOnKitKat(Intent data) {
//String imagePath = null;
Uri uri = data.getData();
Log.d("uri=intent.getData :", "" + uri);
if (DocumentsContract.isDocumentUri(this, uri)) {
String docId = DocumentsContract.getDocumentId(uri); //数据表里指定的行
Log.d("getDocumentId(uri) :", "" + docId);
Log.d("uri.getAuthority() :", "" + uri.getAuthority());
if ("com.android.providers.media.documents".equals(uri.getAuthority())) {
String id = docId.split(":")[1];
String selection = MediaStore.Images.Media._ID + "=" + id;
path = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
} else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId));
path = getImagePath(contentUri, null);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
path = getImagePath(uri, null);
}
Log.v("path:",path);
displayImage(path);
}
/**
* 通过uri和selection来获取真实的图片路径,从相册获取图片时要用
*/
private String getImagePath(Uri uri, String selection) {
//String path = null;
Cursor cursor = getContentResolver().query(uri, null, selection, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
}
cursor.close();
}
return path;
}
//显示图片到控件上面
private void displayImage(String imagePath) {
if (!TextUtils.isEmpty(imagePath)) {
orc_bitmap = BitmapFactory.decodeFile(imagePath);//获取图片
if (orc_bitmap != null) {
ivphoto = (ImageView)findViewById(R.id.imageView);
ivphoto.setImageBitmap(orc_bitmap);
}
} else {
Toast.makeText(this, "图片获取失败", Toast.LENGTH_LONG).show();
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
//打开相册后返回
case SELECT_PHOTO:
if (resultCode == RESULT_OK) {
//判断手机系统版本号
if (Build.VERSION.SDK_INT > 19) {
//4.4及以上系统使用这个方法处理图片
handleImgeOnKitKat(data);
}
}break;
}
}
}
调用接口的类,其中appID以及Key请自行到百度AI开放平台获取
人脸对比接口
package ss.demo14.mets;
import android.util.Log;
import com.baidu.aip.face.AipFace;
import com.baidu.aip.face.MatchRequest;
import com.baidu.aip.util.Base64Util;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import ss.demo14.tools.FileUtil;
/*
*@Author:Swallow
*@Date:2018/12/12
*/
public class CFace {
// public Keys keys = null;
public String cface(String path1, String path2) {
//初始化百度引擎,传入对应的Key
AipFace client = new AipFace("********", "****", "******");
String ss = null;
try {
//图片格式转换,百度接口要求传入的图片为BASE64格式
byte[] bytes1 = FileUtil.readFileByBytes(path1);
byte[] bytes2 = FileUtil.readFileByBytes(path2);
String image1 = Base64Util.encode(bytes1);
String image2 = Base64Util.encode(bytes2);
Log.v("img1", image1);
Log.v("img2", image2);
MatchRequest req1 = new MatchRequest(image1, "BASE64");
MatchRequest req2 = new MatchRequest(image2, "BASE64");
ArrayList<MatchRequest> requests = new ArrayList<MatchRequest>();
requests.add(req1);
requests.add(req2);
//返回JSON字符串
JSONObject res = client.match(requests);
ss = res.toString();
Log.v("mes", ss);
} catch (IOException e) {
e.printStackTrace();
}
return ss;
}
}
人脸检测接口
package ss.demo14.mets;
import android.util.Log;
import com.baidu.aip.face.AipFace;
import com.baidu.aip.face.MatchRequest;
import com.baidu.aip.util.Base64Util;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.util.HashMap;
import ss.demo14.tools.FileUtil;
/*
*@Author:Swallow
*@Date:2018/12/12
*/
public class Find {
public String findfacemsg(String path){
AipFace client = new AipFace("********", "********", "********");
String mesage = null;
try {
byte[] bytes = FileUtil.readFileByBytes(path);
String image = Base64Util.encode(bytes);
//MatchRequest req1 = new MatchRequest(image1, "BASE64");
HashMap<String, String> options = new HashMap<String, String>();
//获取人脸信息当中的返回值包括年龄,性别,人种
//官网可以返回的信息包括age,beauty,expression,faceshape,gender,glasses,landmark,race,quality,facetype信息
options.put("face_field", "age,gender,race");
options.put("max_face_num", "10");
//options.put("face_type", "LIVE");
JSONObject res = client.detect(image, "BASE64", options);
mesage = res.toString();
Log.v("mes", mesage);
}catch (IOException e) {
e.printStackTrace();
}
return mesage;
}
}
然后是几个会用到的工具类
参考自https://blog.csdn.net/qq_34042417/article/details/82927785
package ss.demo14.tools;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* 文件读取工具类
*/
public class FileUtil {
/**
* 读取文件内容,作为字符串返回
*/
public static String readFileAsString(String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
throw new FileNotFoundException(filePath);
}
if (file.length() > 1024 * 1024 * 1024) {
throw new IOException("File is too large");
}
StringBuilder sb = new StringBuilder((int) (file.length()));
// 创建字节输入流
FileInputStream fis = new FileInputStream(filePath);
// 创建一个长度为10240的Buffer
byte[] bbuf = new byte[10240];
// 用于保存实际读取的字节数
int hasRead = 0;
while ( (hasRead = fis.read(bbuf)) > 0 ) {
sb.append(new String(bbuf, 0, hasRead));
}
fis.close();
return sb.toString();
}
/**
* 根据文件路径读取byte[] 数组
*/
public static byte[] readFileByBytes(String filePath) throws IOException {
File file = new File(filePath);
if (!file.exists()) {
throw new FileNotFoundException(filePath);
} else {
ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());
BufferedInputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(file));
short bufSize = 1024;
byte[] buffer = new byte[bufSize];
int len1;
while (-1 != (len1 = in.read(buffer, 0, bufSize))) {
bos.write(buffer, 0, len1);
}
byte[] var7 = bos.toByteArray();
return var7;
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException var14) {
var14.printStackTrace();
}
bos.close();
}
}
}
}
/*
* Copyright (C) 2017 Baidu, Inc. All Rights Reserved.
*/
package ss.demo14.tools;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;
import java.lang.reflect.Type;
/**
* Json工具类.
*/
public class GsonUtils {
private static Gson gson = new GsonBuilder().create();
public static String toJson(Object value) {
return gson.toJson(value);
}
public static <T> T fromJson(String json, Class<T> classOfT) throws JsonParseException {
return gson.fromJson(json, classOfT);
}
public static <T> T fromJson(String json, Type typeOfT) throws JsonParseException {
return (T) gson.fromJson(json, typeOfT);
}
}
package ss.demo14.tools;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
/**
* http 工具类
*/
public class HttpUtil {
public static String post(String requestUrl, String accessToken, String params)
throws Exception {
String contentType = "application/x-www-form-urlencoded";
return HttpUtil.post(requestUrl, accessToken, contentType, params);
}
public static String post(String requestUrl, String accessToken, String contentType, String params)
throws Exception {
String encoding = "UTF-8";
if (requestUrl.contains("nlp")) {
encoding = "GBK";
}
return HttpUtil.post(requestUrl, accessToken, contentType, params, encoding);
}
public static String post(String requestUrl, String accessToken, String contentType, String params, String encoding)
throws Exception {
String url = requestUrl + "?access_token=" + accessToken;
return HttpUtil.postGeneralUrl(url, contentType, params, encoding);
}
public static String postGeneralUrl(String generalUrl, String contentType, String params, String encoding)
throws Exception {
URL url = new URL(generalUrl);
// 打开和URL之间的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
// 设置通用的请求属性
connection.setRequestProperty("Content-Type", contentType);
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setUseCaches(false);
connection.setDoOutput(true);
connection.setDoInput(true);
// 得到请求的输出流对象
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.write(params.getBytes(encoding));
out.flush();
out.close();
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> headers = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : headers.keySet()) {
System.err.println(key + "--->" + headers.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
BufferedReader in = null;
in = new BufferedReader(
new InputStreamReader(connection.getInputStream(), encoding));
String result = "";
String getLine;
while ((getLine = in.readLine()) != null) {
result += getLine;
}
in.close();
System.err.println("result:" + result);
return result;
}
}
返回的结果是JSON字符串,解析就可以了。这里我解析了数量、年龄、人种、性别、以及是否为同一个人。
可以在日志里输出。
需要更新到UI界面的话,这部分还没有完成,后面有时间会继续写
这里我经过几组图片的对比测试,如果是同一个人,不同造型,不同场景下比对结果相似度都在75以上,所以我设定的是相似度为75分以上就判断为同一个人。
如果是同一个人,造型以及场景都差不多,一般都有90分以上