腾讯云【人脸识别】服务的一次尝试(JAVA)
背景
人臉識別是人工智能智能領域中應用最廣泛的服務之一。個人認為,人臉識別也是目前人工智能領域中技術最成熟的技術之一。各大云服務廠商均開通了人臉識別的服務。
那就來嘗嘗吧。。
登陸注冊->找到人臉識別服務
登陸的過程就不說了。很久以前我就有騰訊云賬號了,現在微信掃一掃二維碼就能登陸了。在藤須品首頁就可以找到人臉識別服務。
?開通人臉識別服務
點擊入門按鈕,來到指導頁面 。點擊云控制臺的連接,能夠直接跳到開通頁面,點擊開通按鈕。人臉識別服務就開通了。
或者去自己找到界面
?
?人臉識別應用
(1)配置JAVA環境,maven環境,新建maven工程。
這里面要配置一下騰訊的SDK,也就是引包,先去這個網站上面查一下,版本號。
https://search.maven.org/search?q=tencentcloud-sdk-java
這里面發現現在的版本號是3.1.46,那么maven文件當中對應的就是
<dependency><groupId>com.tencentcloudapi</groupId><artifactId>tencentcloud-sdk-java</artifactId><version>3.1.46</version></dependency>(2)編碼
通過API Explorer進行編碼,點開之后找到人臉發現的API,并在個人密鑰處輸入自己的密鑰。
剛來使用的小伙伴肯定不知道密鑰去哪找,但剛好在輸入框上邊有一個連接可以直接點過去。
然后去仔細看看參數的內容要輸入哪些。仔細看看,其實只有region和圖片是必要的。
region的話選擇一個就可以了。
但是圖片可以是個連接,也可以是個URL,但要存儲在騰訊云中。這里面選擇直接用Base64的圖片字符串好了。
?如果直接輸入圖片的字符串,那將會是這樣,復制起來,簡直累暈了。
并且運行起來也會有問題。
所以,暫時不填圖片信息,在代碼里面進行修改。將如下代碼復制到IDEA
import com.tencentcloudapi.common.Credential; import com.tencentcloudapi.common.profile.ClientProfile; import com.tencentcloudapi.common.profile.HttpProfile; import com.tencentcloudapi.common.exception.TencentCloudSDKException;import com.tencentcloudapi.iai.v20180301.IaiClient;import com.tencentcloudapi.iai.v20180301.models.DetectFaceRequest; import com.tencentcloudapi.iai.v20180301.models.DetectFaceResponse;public class DetectFace {public static void main(String[] args) {try {Credential cred = new Credential("XXXXXXXXXXXXXXx", "XXXXXXXXx");HttpProfile httpProfile = new HttpProfile();httpProfile.setEndpoint("iai.tencentcloudapi.com");ClientProfile clientProfile = new ClientProfile();clientProfile.setHttpProfile(httpProfile);IaiClient client = new IaiClient(cred, "ap-beijing", clientProfile);String params = "{}";DetectFaceRequest req = DetectFaceRequest.fromJsonString(params, DetectFaceRequest.class);DetectFaceResponse resp = client.DetectFace(req);System.out.println(DetectFaceRequest.toJsonString(resp));} catch (TencentCloudSDKException e) {System.out.println(e.toString());}}}?(3)修改
十分關鍵的一步,目的有兩個:
其一是,將圖片轉為BASE64的String,構造params
其二是,利用識別的結果,標注人臉。
用到了json的包
<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.68</version> </dependency>代碼如下。
import com.alibaba.fastjson.JSON; import com.tencentcloudapi.common.Credential; import com.tencentcloudapi.common.profile.ClientProfile; import com.tencentcloudapi.common.profile.HttpProfile; import com.tencentcloudapi.common.exception.TencentCloudSDKException; import com.tencentcloudapi.iai.v20180301.IaiClient; import com.tencentcloudapi.iai.v20180301.models.DetectFaceRequest; import com.tencentcloudapi.iai.v20180301.models.DetectFaceResponse; import com.tencentcloudapi.iai.v20180301.models.FaceInfo; import sun.misc.BASE64Encoder;import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.HashMap;public class DetectFace {public static void main(String[] args) {try {String imageUrl = "/Users/yuchk/Desktop/haha.png";String markImageUrl = "/Users/yuchk/Desktop/haha_res.png";// 替換自己的密鑰Credential cred = new Credential("XX", "XX");HttpProfile httpProfile = new HttpProfile();httpProfile.setEndpoint("iai.tencentcloudapi.com");ClientProfile clientProfile = new ClientProfile();clientProfile.setHttpProfile(httpProfile);IaiClient client = new IaiClient(cred, "ap-beijing", clientProfile);HashMap map = new HashMap<String, String>(8);String image = getBase64Image(imageUrl);map.put("Image", image);map.put("NeedQualityDetection", "1");String params = JSON.toJSONString(map);DetectFaceRequest req = DetectFaceRequest.fromJsonString(params, DetectFaceRequest.class);DetectFaceResponse resp = client.DetectFace(req);System.out.println(DetectFaceRequest.toJsonString(resp));FaceInfo[] faceInfos = resp.getFaceInfos();long height = faceInfos[0].getHeight();long width = faceInfos[0].getWidth();long x = faceInfos[0].getX();long y = faceInfos[0].getY();// 將人臉標注起來BufferedImage bufferedImage = ImageIO.read(new File(imageUrl));Graphics g = bufferedImage.getGraphics();g.setColor(Color.RED);//矩形框(原點x坐標,原點y坐標,矩形的長,矩形的寬)g.drawRect((int) x, (int) y, (int) width, (int) height);g.dispose();FileOutputStream out = new FileOutputStream(markImageUrl);ImageIO.write(bufferedImage, "png", out);} catch (TencentCloudSDKException | IOException e) {System.out.println(e.toString());}}private static String getBase64Image(String url) {try {return getBase64Image(new FileInputStream(url));} catch (FileNotFoundException e) {e.printStackTrace();}return null;}private static String getBase64Image(FileInputStream inputStream) {try {byte[] data = new byte[inputStream.available()];inputStream.read(data);inputStream.close();BASE64Encoder encoder = new BASE64Encoder();return encoder.encode(data);} catch (IOException e) {e.printStackTrace();}return null;}}結果
{"ImageWidth":509,"ImageHeight":429,"FaceInfos":[{"X":152,"Y":51,"Width":135,"Height":175,"FaceAttributesInfo":{"Gender":0,"Age":0,"Expression":0,"Glass":false,"Pitch":0,"Yaw":0,"Roll":0,"Beauty":0,"Hat":false,"Mask":false,"Hair":{"Length":0,"Bang":0,"Color":0},"EyeOpen":false},"FaceQualityInfo":{"Score":84,"Sharpness":61,"Brightness":43,"Completeness":{"Eyebrow":87,"Eye":92,"Nose":97,"Cheek":89,"Mouth":99,"Chin":94}}}],"FaceModelVersion":"3.0","RequestId":"xxxxxxxxxx"}
?
?
?
總結
以上是生活随笔為你收集整理的腾讯云【人脸识别】服务的一次尝试(JAVA)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mac上PDF中插入替换删除页面
- 下一篇: 百度人脸搜索的一次尝试(JAVA)