今天介绍的是通过face++联网API实现人脸识别功能、打开相机及从相册获取图片功能。
文档请到face++官网查看(https://www.faceplusplus.com.cn/)
程序的主流程:
1、创建一个人脸的集合FaceSet,用于存储人脸标识 face_token
调用face++的url:https://api-cn.faceplusplus.com/facepp/v3/faceset/create
RequestParams params = new RequestParams(); params.put("api_key",Constant.Key); params.put("api_secret",Constant.Secret); params.put("display_name","FaceSet"); params.put("outer_id","faceSet"); HttpUtil.post(Constant.createUrl,params,new AsyncHttpResponseHandler(){ @Override public void onSuccess(String resultJson) { super.onSuccess(resultJson); Log.e("createFaceSet==Success","success"); Log.e("resultJson===",resultJson.toString()); } @Override public void onFailure(Throwable throwable, String resultJson) { super.onFailure(throwable, resultJson); Log.e("createFaceSet==Error","error"); Log.e("resultJson===",resultJson.toString()); Toast.makeText(MainActivity.this,"createFaceSet=="+resultJson.toString(),Toast.LENGTH_SHORT).show(); } });
2、传入图片进行人脸检测和人脸分析
调用face++的url:https://api-cn.faceplusplus.com/facepp/v3/detect
Bitmap bitmap = BitmapFactory.decodeFile(urlImg); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG,100,outputStream ); byte[] imagebyte = outputStream.toByteArray(); String image_base64 = Constant.encode(imagebyte); RequestParams params = new RequestParams(); params.put("api_key", Constant.Key); params.put("api_secret",Constant.Secret); params.put("image_base64",image_base64); params.put("return_attributes","gender,age"); HttpUtil.post(Constant.detectUrl,params,new AsyncHttpResponseHandler(){ @Override public void onSuccess(String resultJson) { super.onSuccess(resultJson); Log.e("detect==testSuccess","success"); Log.e("resultJson===",resultJson.toString()); result.setText(resultJson.toString()); if(resultJson != null){ JSONObject object; try { object = new JSONObject(resultJson); JSONArray array = object.getJSONArray("faces"); String face_token = null; for(int i = 0;i<array.length();i++){ JSONObject oj = (JSONObject) array.get(i); face_token = (String) oj.get("face_token"); } if(face_token != null){ Log.e("face_token",face_token); if (state == 1){ addFace(face_token); }else if(state == 2){ serachFace(face_token); }else{ Toast.makeText(MainActivity.this,"state=="+state,Toast.LENGTH_SHORT).show(); } }else{ Toast.makeText(MainActivity.this,"上传图片无法获取人脸标识",Toast.LENGTH_SHORT).show(); } } catch (JSONException e) { e.printStackTrace(); } } } @Override public void onFailure(Throwable throwable, String resultJson) { super.onFailure(throwable, resultJson); Log.e("detect==testError","error"); Log.e("resultJson===",resultJson.toString()); Toast.makeText(MainActivity.this,"detect=="+resultJson.toString(),Toast.LENGTH_SHORT).show(); result.setText(resultJson.toString()); } });
3、为一个已创建的人脸集合FaceSet 添加人脸标识face_token
调用face++的url: https://api-cn.faceplusplus.com/facepp/v3/faceset/addface
RequestParams params = new RequestParams(); params.put("api_key",Constant.Key); params.put("api_secret",Constant.Secret); params.put("faceset_token",faceset_token); params.put("face_tokens",face_token); HttpUtil.post(Constant.addfaceUrl,params,new AsyncHttpResponseHandler(){ @Override public void onSuccess(String resultJson) { super.onSuccess(resultJson); Log.e("addFace==Success","success"); Log.e("resultJson===",resultJson.toString()); JSONObject object; try { object = new JSONObject(resultJson); int face_added = Integer.parseInt(object.getString("face_added")); if(face_added > 0){ Toast.makeText(MainActivity.this,"添加人脸标识成功!",Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(MainActivity.this,"添加人脸标识失败!",Toast.LENGTH_SHORT).show(); } } catch (JSONException e) { e.printStackTrace(); } } @Override public void onFailure(Throwable throwable, String resultJson) { super.onFailure(throwable, resultJson); Log.e("addFace==Error","error"); Log.e("resultJson===",resultJson.toString()); Toast.makeText(MainActivity.this,"addFace=="+resultJson.toString(),Toast.LENGTH_SHORT).show(); } });
4、在一个已有的人脸集合FaceSet 中找出与目标人脸最相似的一张或多张人脸,返回置信度和不同误识率下的阈值
调用face++的url:https://api-cn.faceplusplus.com/facepp/v3/search
RequestParams params = new RequestParams(); params.put("api_key",Constant.Key); params.put("api_secret",Constant.Secret); params.put("face_token",face_token); params.put("faceset_token",faceset_token); /** * 控制返回比对置信度最高的结果的数量。合法值为一个范围 [1,5] 的整数。默认值为 1 */ HttpUtil.post(Constant.searchUrl,params,new AsyncHttpResponseHandler(){ @Override public void onSuccess(String resultJson) { super.onSuccess(resultJson); Log.e("serachFace==Success","success"); Log.e("resultJson===",resultJson.toString()); JSONObject object; try { object = new JSONObject(resultJson); JSONArray array = object.getJSONArray("results"); //比对结果置信度 int confidence = 0; for(int i = 0;i<array.length();i++){ JSONObject oj = (JSONObject) array.get(i); confidence = oj.getInt("confidence"); } if(confidence != 0){ Log.e("confidence",confidence+""); if(confidence >= 80){ AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("比对结果"); builder.setMessage("通过"); byte[] requestData; requestData = CommandGenerate.generateLockCmd(CommandGenerate.CMD.UNLOCK,4); sendDataToSerial(requestData); builder.show(); }else{ AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this); builder.setTitle("比对结果"); builder.setMessage("未通过"); builder.show(); //Toast.makeText(MainActivity.this,"比对结果置信度低于80=="+confidence,Toast.LENGTH_SHORT).show(); } }else { Toast.makeText(MainActivity.this,"FaceSet中未搜索到相吻合的信息",Toast.LENGTH_SHORT).show(); } } catch (JSONException e) { e.printStackTrace(); } } @Override public void onFailure(Throwable throwable, String resultJson) { super.onFailure(throwable, resultJson); Log.e("serachFace==Error","error"); Log.e("resultJson===",resultJson.toString()); Toast.makeText(MainActivity.this,"serachFace=="+resultJson.toString(),Toast.LENGTH_SHORT).show(); } });
项目源码:http://download.csdn.net/download/daxudada/10229284
希望对大家有用,如有问题请多多指教。关注我哟