首页 热点资讯 义务教育 高等教育 出国留学 考研考公

百度AI接口实现人脸识别?

发布网友 发布时间:2022-04-19 15:40

我来回答

1个回答

热心网友 时间:2023-07-12 12:33

import com..aip.util.BaseUtil;

import com.enation.app.base.core.service.Exception.CreditAuthFaceException;

import com.enation.app.base.core.service.ICreditAuthManager;

import com.enation.app.base.core.util.AuthTokenService;

import com.enation.app.base.core.util.HttpClientUtils;

import org.apache.http.client.methods.CloseableHttpResponse;

import java.io.*;

import javax.imageio.stream.FileImageInputStream;

import javax.imageio.stream.FileImageInputStream;

import java.net.HttpURLConnection;

import java.net.URLEncoder;

import java.util.HashMap;

import java.util.Map;

/**

* 人脸识别service

* @param name

* @param id_card

* @param faceUrl

*/

@Override

public void face(String name, String id_card, String faceUrl) {

//调用接口获取tocken(有效期一个月)

    String token = AuthTokenService.getAuth();

System.out.println("1:token:" + token);

//调用身份验证api地址

    String url ="https://aip.bce.com/rest/2.0/face/v2/person/verify?access_token=" + token;

File face=new File(faceUrl);

FileImageInputStream input =null;

byte[] data =null;

String baseImage =null;

try {

input =new FileImageInputStream(face);

ByteArrayOutputStream output =new ByteArrayOutputStream();

byte[] buf =new byte[1024];

int numBytesRead =0;

while ((numBytesRead = input.read(buf)) != -1) {

output.write(buf,0, numBytesRead);

}

data = output.toByteArray();

baseImage = BaseUtil.encode(data);

System.out.println("4base转码:"+baseImage);

}catch (IOException e) {

e.printStackTrace();

}finally {

try {

if (input !=null) {

input.close();

}

}catch (IOException e) {

e.printStackTrace();

}

}

Map headers =new HashMap();

headers.put("Content-Type","application/x-www-form-urlencoded");

Map bodys =new HashMap();

bodys.put("image", baseImage);

bodys.put("id_card_number", id_card);

bodys.put("name", name);

try {

CloseableHttpResponse response = HttpClientUtils.doHttpsPost(url, headers, bodys);

String result= HttpClientUtils.toString(response);

System.out.println("5返回json数据:" + result);

org.json.JSONObject jsonObject=new org.json.JSONObject(result);

System.out.println("jsonObject:"+jsonObject);

Object jsonResult = jsonObject.get("result");

Float floatResult = Float.parseFloat(jsonResult.toString());

if (floatResult >=0.80) {

System.out.println("floatResult:"+floatResult+"人脸身份验证成功");

}else {

System.out.println("floatResult:"+floatResult+"人脸身份验证失败");

throw new CreditAuthFaceException("人脸身份验证失败");

}

}catch (Exception e) {

e.printStackTrace();

System.out.println("异常输出");

throw new CreditAuthFaceException("人脸认证失败");

}

}

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com