這個是人臉識別時沒法檢測到圖片報的錯,有時候咱們檢測一張圖片是否在庫裏面,當一張圖片明顯在裏面,還檢測不到,以下面是個人代碼java
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才能正確的和庫裏面的圖片比對,下面引入了加密函數json
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函數
這樣能夠正確的檢測到圖片了。編碼