• face++3D人脸重建API,并保存人脸3D模型文件face.obj,face.mtl,tex.jpg


    /**
     * @Description:
     * @author: kpzc
     * @date: 2020-11-02 17:48
     */
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    
    import java.io.*;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.net.URLDecoder;
    import java.net.URLEncoder;
    import java.util.*;
    import javax.net.ssl.SSLException;
    public class FaceTest {
    
        public static void main(String[] args) throws Exception{
            main2();
        }
        public static void main1(){
            File file = new File("C:\Users\kpzc\Desktop\25.jpg");
            byte[] buff = getBytesFromFile(file);
            String url = "https://api-cn.faceplusplus.com/facepp/v3/detect";
            HashMap<String, String> map = new HashMap<>();
            HashMap<String, byte[]> byteMap = 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");
            byteMap.put("image_file", buff);
            try{
                byte[] bacd = post(url, map, byteMap);
                String str = new String(bacd);
                System.out.println(str);
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
        
        public static void main2(){
            File file = new File("C:\Users\kpzc\Desktop\26.jpg");
            byte[] buff = getBytesFromFile(file);
            String url = "https://api-cn.faceplusplus.com/facepp/v1/3dface";
            HashMap<String, String> map = new HashMap<>();
            HashMap<String, byte[]> byteMap = new HashMap<>();
            map.put("api_key", "");
            map.put("api_secret", "");
            map.put("texture", "1");
            map.put("mtl", "1");
            byteMap.put("image_file_1", buff);
            try {
                byte[] bacd = post(url, map, byteMap);
                String str = new String(bacd);
                JSONObject jo = JSON.parseObject(str);
                //System.out.println(str);
                final Base64.Decoder decoder = Base64.getDecoder();
                //解码
                String str1 =new String(decoder.decode(jo.getString("obj_file")), "UTF-8");
    //            System.out.println(str1);
                //写操作
                BufferedWriter bufout = new BufferedWriter(new FileWriter("C:\Users\kpzc\Desktop\face.obj"));
                bufout.write(str1);
                bufout.close();
    
    
                String str2 =new String(decoder.decode(jo.getString("mtl_file")), "UTF-8");
                BufferedWriter bufout2 = new BufferedWriter(new FileWriter("C:\Users\kpzc\Desktop\face.mtl"));
                bufout2.write(str2);
                bufout2.close();
    
                String str3 =jo.getString("texture_img");
    //            System.out.println(str3);
                GenerateImage(str3, "C:\Users\kpzc\Desktop\tex.jpg");
    
    
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public static boolean GenerateImage(String imgStr, String imgFilePath) {// 对字节数组字符串进行Base64解码并生成图片
            if (imgStr == null) // 图像数据为空
                return false;
            BASE64Decoder decoder = new BASE64Decoder();
            try {
                // Base64解码
                byte[] bytes = decoder.decodeBuffer(imgStr);
                for (int i = 0; i < bytes.length; ++i) {
                    if (bytes[i] < 0) {// 调整异常数据
                        bytes[i] += 256;
                    }
                }
                // 生成jpeg图片
                OutputStream out = new FileOutputStream(imgFilePath);
                out.write(bytes);
                out.flush();
                out.close();
                return true;
            } catch (Exception e) {
                return false;
            }
        }
        private final static int CONNECT_TIME_OUT = 30000;
        private final static int READ_OUT_TIME = 50000;
        private static String boundaryString = getBoundary();
        protected static byte[] post(String url, HashMap<String, String> map, HashMap<String, byte[]> fileMap) throws Exception {
            HttpURLConnection conne;
            URL url1 = new URL(url);
            conne = (HttpURLConnection) url1.openConnection();
            conne.setDoOutput(true);
            conne.setUseCaches(false);
            conne.setRequestMethod("POST");
            conne.setConnectTimeout(CONNECT_TIME_OUT);
            conne.setReadTimeout(READ_OUT_TIME);
            conne.setRequestProperty("accept", "*/*");
            conne.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundaryString);
            conne.setRequestProperty("connection", "Keep-Alive");
            conne.setRequestProperty("user-agent", "Mozilla/4.0 (compatible;MSIE 6.0;Windows NT 5.1;SV1)");
            DataOutputStream obos = new DataOutputStream(conne.getOutputStream());
            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();
                obos.writeBytes("--" + boundaryString + "
    ");
                obos.writeBytes("Content-Disposition: form-data; name="" + key
                        + ""
    ");
                obos.writeBytes("
    ");
                obos.writeBytes(value + "
    ");
            }
            if(fileMap != null && fileMap.size() > 0){
                Iterator fileIter = fileMap.entrySet().iterator();
                while(fileIter.hasNext()){
                    Map.Entry<String, byte[]> fileEntry = (Map.Entry<String, byte[]>) fileIter.next();
                    obos.writeBytes("--" + boundaryString + "
    ");
                    obos.writeBytes("Content-Disposition: form-data; name="" + fileEntry.getKey()
                            + ""; filename="" + encode(" ") + ""
    ");
                    obos.writeBytes("
    ");
                    obos.write(fileEntry.getValue());
                    obos.writeBytes("
    ");
                }
            }
            obos.writeBytes("--" + boundaryString + "--" + "
    ");
            obos.writeBytes("
    ");
            obos.flush();
            obos.close();
            InputStream ins = null;
            int code = conne.getResponseCode();
            try{
                if(code == 200){
                    ins = conne.getInputStream();
                }else{
                    ins = conne.getErrorStream();
                }
            }catch (SSLException e){
                e.printStackTrace();
                return new byte[0];
            }
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buff = new byte[4096];
            int len;
            while((len = ins.read(buff)) != -1){
                baos.write(buff, 0, len);
            }
            byte[] bytes = baos.toByteArray();
            ins.close();
            return bytes;
        }
        private static String getBoundary() {
            StringBuilder sb = new StringBuilder();
            Random random = new Random();
            for(int i = 0; i < 32; ++i) {
                sb.append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-".charAt(random.nextInt("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_".length())));
            }
            return sb.toString();
        }
        private static String encode(String value) throws Exception{
            return URLEncoder.encode(value, "UTF-8");
        }
    
        public static byte[] getBytesFromFile(File f) {
            if (f == null) {
                return null;
            }
            try {
                FileInputStream stream = new FileInputStream(f);
                ByteArrayOutputStream out = new ByteArrayOutputStream(1000);
                byte[] b = new byte[1000];
                int n;
                while ((n = stream.read(b)) != -1)
                    out.write(b, 0, n);
                stream.close();
                out.close();
                return out.toByteArray();
            } catch (IOException e) {
            }
            return null;
        }
    }

     效果如下

  • 相关阅读:
    poj 3468 A Simple Problem with Integers 线段树区间更新
    poj 2096 概率dp
    JSP页面的基本结构 及声明变量
    怎样制作一个横版格斗过关游戏 Cocos2d-x 2.0.4
    块状元素与内联元素的差别
    ZOJ 3526 Weekend Party
    linux下javadoc生成文件出现中文乱码
    Centos6.0 通过devtoolset-2工具安装gcc 4.8
    fre7 offonline for firefox
    Aix Lamp openssh bash
  • 原文地址:https://www.cnblogs.com/zjk1/p/13932660.html
Copyright © 2020-2023  润新知