人脸识别API-人脸检测(java例子) - Go语言中文社区

人脸识别API-人脸检测(java例子)


检测人脸图片

HTTP发送类

package engine.demo;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import com.alibaba.fastjson.JSONObject;

public class HttpClientUtil {
    public static String sendPostRequestByJava(String reqURL, String sendData, int connectTimeout, int readTimeout) {
        HttpURLConnection httpURLConnection = null;
        OutputStream out = null;
        InputStream in = null;
        int httpStatusCode = 0;
        try {
            URL sendUrl = new URL(reqURL);
            httpURLConnection = (HttpURLConnection) sendUrl.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setUseCaches(false);
            httpURLConnection.setConnectTimeout(connectTimeout);
            httpURLConnection.setReadTimeout(readTimeout);
            httpURLConnection.setRequestProperty("Content-Type", "application/json");
            out = httpURLConnection.getOutputStream();
            out.write(sendData.getBytes());
            out.flush();
            httpStatusCode = httpURLConnection.getResponseCode();
            in = httpURLConnection.getInputStream();
            byte[] by = new byte[1024];
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            int len = -1;
            while ((len = in.read(by)) != -1) {
                bos.write(by, 0, len);
            }
            return bos.toString("utf-8");
        } catch (Exception e) {
            JSONObject json = new JSONObject();
            json.put("result", -10000);
            json.put("info", "http error");
            json.put("httpStatusCode", httpStatusCode);
            return json.toString();
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (Exception e) {
                }
            }
            if (in != null) {
                try {
                    in.close();
                } catch (Exception e) {
                }
            }
            if (httpURLConnection != null) {
                httpURLConnection.disconnect();
                httpURLConnection = null;
            }
        }
    }

    public static String sendPostRequestByJava(String reqURL, String sendData) {
        return sendPostRequestByJava(reqURL, sendData, 30000, 30000);
    }
}

人脸检测API封装类

package engine.demo;

import com.alibaba.fastjson.JSONObject;

public class FaceDetect {
	public static int Process(String url,byte[] image ) {
		JSONObject json = new JSONObject();
		json.put("app_id", "system");
		json.put("app_secret", "12345");
		json.put("img", image);
		json.put("mode", true);
		String response=HttpClientUtil.sendPostRequestByJava(url, json.toString());
		System.out.println(response);;
		return 0;
	}
}

发送请求给服务器

package engine.demo;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class App 
{
    public static void main( String[] args )
    {
    	//读取一个本地jpg文件
        byte[] image=readFile("g:/1.jpg");
        //向服务器发送请求
        FaceDetect.Process("http://192.168.10.25:7100/face/tool/detect", image);
    }
    
    public static byte[] readFile(String path){
    	File filePic = new File(path);
    	if(filePic.exists()){
	    	FileInputStream is=null;
	    	try {
				is = new FileInputStream(filePic);
				int i = is.available(); 
				byte data[] = new byte[i];  
				is.read(data); 
				is.close(); 
				return data; 
			} catch (Exception e) {
				if (is!=null){
					try {
						is.close();
					} catch (IOException e1) {
					}
				}
				return null;
			}
	    }else{
	    	return null;	
	    }
    }
}


返回结果

{
	"debugInfo": {
		"elapse": 29,
		"path": "/face/tool/detect"
	},
	"faces": [{
		"height": 92,
		"img": "/9j/4AApQ+rz9ne7seVS........2Q==",
		"width": 92,
		"x": 66,
		"y": 52
	}, {
		"height": 91,
		"img": "/9j/4AAQSkZJRgABAQ.......AAAQABA6sVY/9k=",
		"width": 91,
		"x": 246,
		"y": 51
	}],
	"info": "success",
	"result": 0,
	"seq": "4674d205288445119430a9da63d446ed"
}

faces是json数组,里面包含了图片中所有人脸信息及人脸抠图

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/yimin_tank/article/details/82864061
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。
  • 发表于 2020-06-06 10:51:33
  • 阅读 ( 1181 )
  • 分类:

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢