java调用face_java调用face++人脸检测和分析接口

import java.io.*;importjava.util.Map;importjava.util.HashMap;importjava.util.Iterator;importjava.nio.file.Files;importjava.nio.file.Path;importjava.nio.file.Paths;importjava.nio.file.StandardCopyOption;importjava.net.URL;importorg.apache.http.HttpEntity;importorg.apache.http.client.methods.CloseableHttpResponse;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.entity.ContentType;importorg.apache.http.entity.mime.MultipartEntityBuilder;importorg.apache.http.impl.client.HttpClients;importorg.apache.http.impl.client.CloseableHttpClient;importorg.apache.http.util.EntityUtils;public classFaceDetect {public static void main(String[] args) throwsException {

String urlpath= "/Users/sunxianyan/Desktop/test/input.csv";

File urlcsv= newFile (urlpath);

BufferedReader bufferedReader= new BufferedReader (newFileReader (urlpath));

String line= null;while ((line = bufferedReader.readLine ()) != null) {

String[] item= line.split (",");

String url= "https://api-cn.faceplusplus.com/facepp/v3/detect";

HashMap map = new HashMap<>();

map.put ("api_key", "***");

map.put ("api_secret", "***");

map.put ("return_landmark", "1");

map.put ("return_attributes", "gender,age,smiling,headpose,facequality,blur,eyestatus,emotion,ethnicity,beauty,mouthstatus,eyegaze,skinstatus");

HashMap byteMap = new HashMap<>();

downloadByNIO2 (item[0], urlcsv.getParent () + File.separator + "temp", item[0].substring (item[0].lastIndexOf ("/") + 1));

File file= new File (urlcsv.getParent () + File.separator + "temp" + File.separator + item[0].substring (item[0].lastIndexOf ("/") + 1));

detectFace(url, map, file);

}

}protected static void detectFace(String url, HashMap map, File file) throwsException {

CloseableHttpClient client=HttpClients.createDefault();

HttpPost post= newHttpPost(url);

MultipartEntityBuilder builder=MultipartEntityBuilder.create();

Iterator iter=map.entrySet ().iterator ();while(iter.hasNext ()) {

Map.Entry entry =(Map.Entry) iter.next ();

String key=entry.getKey ();

String value=entry.getValue ();

builder.addTextBody(key, value);

}

builder.addBinaryBody("image_file", file, ContentType.MULTIPART_FORM_DATA, file.getName());

HttpEntity multipart=builder.build();

post.setEntity(multipart);

CloseableHttpResponse response=client.execute(post);

HttpEntity responseEntity=response.getEntity();

String sResponse=EntityUtils.toString(responseEntity, "UTF-8");

System.out.println("Post 返回结果"+sResponse);

}//图片链接下载

public static void downloadByNIO2(String url, String saveDir, String fileName) {

try (InputStream ins = newURL (url).openStream ()) {

Path target=Paths.get (saveDir, fileName);

Files.createDirectories (target.getParent ());

Files.copy (ins, target, StandardCopyOption.REPLACE_EXISTING);

}catch(IOException e) {

e.printStackTrace ();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值