zoukankan      html  css  js  c++  java
  • 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 ();
            }
        }
    }
  • 相关阅读:
    js Image对象 及rollover效果
    精通javascript:元素的尺寸
    javascript 快捷操作
    精通javascript:获取位置
    javascript对象小问题
    javascript 获取元素的真实,最终的css样式
    MySQL索引
    精通javascript:元素的可见性
    javascript 图像预载入和如何判断图片是否加载完成
    ASP.NET Ajax的CalendarExtender控件被其它Div遮住问题
  • 原文地址:https://www.cnblogs.com/yangyuxiaozi/p/11244267.html
Copyright © 2011-2022 走看看