腾讯云【人脸识别】服务的一次尝试(JAVA)
背景
人臉識(shí)別是人工智能智能領(lǐng)域中應(yīng)用最廣泛的服務(wù)之一。個(gè)人認(rèn)為,人臉識(shí)別也是目前人工智能領(lǐng)域中技術(shù)最成熟的技術(shù)之一。各大云服務(wù)廠商均開通了人臉識(shí)別的服務(wù)。
那就來嘗嘗吧。。
登陸注冊(cè)->找到人臉識(shí)別服務(wù)
登陸的過程就不說了。很久以前我就有騰訊云賬號(hào)了,現(xiàn)在微信掃一掃二維碼就能登陸了。在藤須品首頁就可以找到人臉識(shí)別服務(wù)。
?開通人臉識(shí)別服務(wù)
點(diǎn)擊入門按鈕,來到指導(dǎo)頁面 。點(diǎn)擊云控制臺(tái)的連接,能夠直接跳到開通頁面,點(diǎn)擊開通按鈕。人臉識(shí)別服務(wù)就開通了。
或者去自己找到界面
?
?人臉識(shí)別應(yīng)用
(1)配置JAVA環(huán)境,maven環(huán)境,新建maven工程。
這里面要配置一下騰訊的SDK,也就是引包,先去這個(gè)網(wǎng)站上面查一下,版本號(hào)。
https://search.maven.org/search?q=tencentcloud-sdk-java
這里面發(fā)現(xiàn)現(xiàn)在的版本號(hào)是3.1.46,那么maven文件當(dāng)中對(duì)應(yīng)的就是
<dependency><groupId>com.tencentcloudapi</groupId><artifactId>tencentcloud-sdk-java</artifactId><version>3.1.46</version></dependency>(2)編碼
通過API Explorer進(jìn)行編碼,點(diǎn)開之后找到人臉發(fā)現(xiàn)的API,并在個(gè)人密鑰處輸入自己的密鑰。
剛來使用的小伙伴肯定不知道密鑰去哪找,但剛好在輸入框上邊有一個(gè)連接可以直接點(diǎn)過去。
然后去仔細(xì)看看參數(shù)的內(nèi)容要輸入哪些。仔細(xì)看看,其實(shí)只有region和圖片是必要的。
region的話選擇一個(gè)就可以了。
但是圖片可以是個(gè)連接,也可以是個(gè)URL,但要存儲(chǔ)在騰訊云中。這里面選擇直接用Base64的圖片字符串好了。
?如果直接輸入圖片的字符串,那將會(huì)是這樣,復(fù)制起來,簡直累暈了。
并且運(yùn)行起來也會(huì)有問題。
所以,暫時(shí)不填圖片信息,在代碼里面進(jìn)行修改。將如下代碼復(fù)制到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)修改
十分關(guān)鍵的一步,目的有兩個(gè):
其一是,將圖片轉(zhuǎn)為BASE64的String,構(gòu)造params
其二是,利用識(shí)別的結(jié)果,標(biāo)注人臉。
用到了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();// 將人臉標(biāo)注起來BufferedImage bufferedImage = ImageIO.read(new File(imageUrl));Graphics g = bufferedImage.getGraphics();g.setColor(Color.RED);//矩形框(原點(diǎn)x坐標(biāo),原點(diǎn)y坐標(biāo),矩形的長,矩形的寬)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;}}結(jié)果
{"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"}
?
?
?
總結(jié)
以上是生活随笔為你收集整理的腾讯云【人脸识别】服务的一次尝试(JAVA)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mac上PDF中插入替换删除页面
- 下一篇: 梳理百年深度学习发展史-七月在线机器学习