这个是人脸识别时无法检测到图片报的错,有时候我们检测一张图片是否在库里面,当一张图片明显在里面,还检测不到,如下面是我的代码
1 package Test1; 2 3 import java.io.IOException; 4 import java.util.Arrays; 5 import java.util.HashMap; 6 7 import org.json.JSONObject; 8 9 import com.baidu.aip.face.AipFace; 10 11 /** 12 * 人脸对比 13 */ 14 public class BaiduFaceTest { 15 16 // 这里填写你自己应用的三项 17 public static final String APP_ID = "16090420"; 18 public static final String API_KEY = "iinpWTE1pvOnH3YNmk4tG5Z6"; 19 public static final String SECRET_KEY = "LMl3pgidH2AzGcTnOM3qh1x3GFnh6jt5"; 20 21 22 23 public static void main(String[] args) throws IOException { 24 AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY); 25 26 String image1 = "C:\Users\19575\Pictures\Saved Pictures\紫霞仙子\a.jpg"; 27 String image2 = "C:\Users\19575\Pictures\Saved Pictures\紫霞仙子\b.jpg"; 28 29 30 JSONObject rs = client.search(image1, "BASE64", "group002", new HashMap<>()); 31 // JSONObject rs=client.detect(image1, "URL", new HashMap<>()); 32 System.out.println(rs.toString(2)); 33 34 } 35 36 }
最后才搞明白是因为我们必须把图片转为BASE64才能正确的和库里面的图片比对,下面引入了加密函数
1 package Test1; 2 3 import java.io.FileInputStream; 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.util.HashMap; 7 8 import org.json.JSONObject; 9 10 import com.baidu.aip.face.AipFace; 11 12 import sun.misc.BASE64Encoder; 13 14 /** 15 * 人脸对比 16 */ 17 public class BaiduFaceTest { 18 19 // 这里填写你自己应用的三项 20 public static final String APP_ID = "16090420"; 21 public static final String API_KEY = "iinpWTE1pvOnH3YNmk4tG5Z6"; 22 public static final String SECRET_KEY = "LMl3pgidH2AzGcTnOM3qh1x3GFnh6jt5"; 23 24 25 26 public static void main(String[] args) throws IOException { 27 AipFace client = new AipFace(APP_ID, API_KEY, SECRET_KEY); 28 29 String image1 = "C:\Users\19575\Pictures\Saved Pictures\紫霞仙子\c.jpg"; 30 System.out.println(image1); 31 String image2 = "C:\Users\19575\Pictures\Saved Pictures\紫霞仙子\b.jpg"; 32 33 String msg=GetImageStr(image1); 34 JSONObject rs = client.search(msg, "BASE64", "group002", new HashMap<>()); 35 // JSONObject rs=client.detect(image1, "URL", new HashMap<>()); 36 System.out.println(rs.toString(2)); 37 38 } 39 public static String GetImageStr(String imgFile) 40 {//将图片文件转化为字节数组字符串,并对其进行Base64编码处理 41 InputStream in = null; 42 byte[] data = null; 43 //读取图片字节数组 44 try 45 { 46 in = new FileInputStream(imgFile); 47 data = new byte[in.available()]; 48 in.read(data); 49 in.close(); 50 } 51 catch (IOException e) 52 { 53 e.printStackTrace(); 54 } 55 //对字节数组Base64编码 56 BASE64Encoder encoder = new BASE64Encoder(); 57 return encoder.encode(data);//返回Base64编码过的字节数组字符串 58 } 59 60 61 }
不过加密解密函数无法直接使用,需要通过下面这个教程把自带的jar包导进去
https://blog.csdn.net/u011514810/article/details/72725398
这样可以正确的检测到图片了。