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


    import java.io.*;
    import java.util.Map;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.nio.file.StandardCopyOption;
    import java.net.URL;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.client.methods.CloseableHttpResponse;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.ContentType;
    import org.apache.http.entity.mime.MultipartEntityBuilder;
    import org.apache.http.impl.client.HttpClients;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.util.EntityUtils;
    
    public class FaceDetect {
        public static void main(String[] args) throws Exception {
            String urlpath = "/Users/sunxianyan/Desktop/test/input.csv";
            File urlcsv = new File (urlpath);
            BufferedReader bufferedReader = new BufferedReader (new FileReader (urlpath));
            String line = null;
            while ((line = bufferedReader.readLine ()) != null) {
                String[] item = line.split (",");
                String url = "https://api-cn.faceplusplus.com/facepp/v3/detect";
    
                HashMap<String, String> 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<String, byte[]> 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<String, String> map, File file) throws Exception {
    
            CloseableHttpClient client = HttpClients.createDefault();
            HttpPost post = new HttpPost(url);
    
            MultipartEntityBuilder builder = MultipartEntityBuilder.create();
    
            Iterator iter = map.entrySet ().iterator ();
            while (iter.hasNext ()) {
                Map.Entry<String, String> 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 = new URL (url).openStream ()) {
                Path target = Paths.get (saveDir, fileName);
                Files.createDirectories (target.getParent ());
                Files.copy (ins, target, StandardCopyOption.REPLACE_EXISTING);
            } catch (IOException e) {
                e.printStackTrace ();
            }
        }
    }
  • 相关阅读:
    用react重构个人网站 3-23
    用react重构个人网站 3-22
    React官方文档笔记之快速入门
    .Net多线程编程—同步机制
    .Net多线程编程—Parallel LINQ、线程池
    .Net多线程编程—并发集合
    .Net多线程编程—任务Task
    【翻译】MongoDB指南/聚合——聚合管道
    【翻译】MongoDB指南/CRUD操作(四)
    【翻译】MongoDB指南/CRUD操作(三)
  • 原文地址:https://www.cnblogs.com/yangyuxiaozi/p/11244267.html
Copyright © 2020-2023  润新知