当前位置:   article > 正文

腾讯云【人脸识别】服务的一次尝试(JAVA)_tencentcloud-sdk-java-iai

tencentcloud-sdk-java-iai

背景

人脸识别是人工智能智能领域中应用最广泛的服务之一。个人认为,人脸识别也是目前人工智能领域中技术最成熟的技术之一。各大云服务厂商均开通了人脸识别的服务。

那就来尝尝吧。。

登陆注册->找到人脸识别服务

登陆的过程就不说了。很久以前我就有腾讯云账号了,现在微信扫一扫二维码就能登陆了。在藤须品首页就可以找到人脸识别服务。

 开通人脸识别服务

点击入门按钮,来到指导页面 。点击云控制台的连接,能够直接跳到开通页面,点击开通按钮。人脸识别服务就开通了。

或者去自己找到界面

 

 人脸识别应用

(1)配置JAVA环境,maven环境,新建maven工程。

这里面要配置一下腾讯的SDK,也就是引包,先去这个网站上面查一下,版本号。

https://search.maven.org/search?q=tencentcloud-sdk-java

这里面发现现在的版本号是3.1.46,那么maven文件当中对应的就是

  1. <dependency>
  2. <groupId>com.tencentcloudapi</groupId>
  3. <artifactId>tencentcloud-sdk-java</artifactId>
  4. <version>3.1.46</version>
  5. </dependency>

(2)编码

通过API Explorer进行编码,点开之后找到人脸发现的API,并在个人密钥处输入自己的密钥。

刚来使用的小伙伴肯定不知道密钥去哪找,但刚好在输入框上边有一个连接可以直接点过去。

然后去仔细看看参数的内容要输入哪些。仔细看看,其实只有region和图片是必要的。

region的话选择一个就可以了。

但是图片可以是个连接,也可以是个URL,但要存储在腾讯云中。这里面选择直接用Base64的图片字符串好了。

 如果直接输入图片的字符串,那将会是这样,复制起来,简直累晕了。

并且运行起来也会有问题。

所以,暂时不填图片信息,在代码里面进行修改。将如下代码复制到IDEA

  1. import com.tencentcloudapi.common.Credential;
  2. import com.tencentcloudapi.common.profile.ClientProfile;
  3. import com.tencentcloudapi.common.profile.HttpProfile;
  4. import com.tencentcloudapi.common.exception.TencentCloudSDKException;
  5. import com.tencentcloudapi.iai.v20180301.IaiClient;
  6. import com.tencentcloudapi.iai.v20180301.models.DetectFaceRequest;
  7. import com.tencentcloudapi.iai.v20180301.models.DetectFaceResponse;
  8. public class DetectFace {
  9. public static void main(String[] args) {
  10. try {
  11. Credential cred = new Credential("XXXXXXXXXXXXXXx", "XXXXXXXXx");
  12. HttpProfile httpProfile = new HttpProfile();
  13. httpProfile.setEndpoint("iai.tencentcloudapi.com");
  14. ClientProfile clientProfile = new ClientProfile();
  15. clientProfile.setHttpProfile(httpProfile);
  16. IaiClient client = new IaiClient(cred, "ap-beijing", clientProfile);
  17. String params = "{}";
  18. DetectFaceRequest req = DetectFaceRequest.fromJsonString(params, DetectFaceRequest.class);
  19. DetectFaceResponse resp = client.DetectFace(req);
  20. System.out.println(DetectFaceRequest.toJsonString(resp));
  21. } catch (TencentCloudSDKException e) {
  22. System.out.println(e.toString());
  23. }
  24. }
  25. }

 (3)修改

十分关键的一步,目的有两个:

其一是,将图片转为BASE64的String,构造params

其二是,利用识别的结果,标注人脸。

用到了json的包

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.68</version>
</dependency>

代码如下。

  1. import com.alibaba.fastjson.JSON;
  2. import com.tencentcloudapi.common.Credential;
  3. import com.tencentcloudapi.common.profile.ClientProfile;
  4. import com.tencentcloudapi.common.profile.HttpProfile;
  5. import com.tencentcloudapi.common.exception.TencentCloudSDKException;
  6. import com.tencentcloudapi.iai.v20180301.IaiClient;
  7. import com.tencentcloudapi.iai.v20180301.models.DetectFaceRequest;
  8. import com.tencentcloudapi.iai.v20180301.models.DetectFaceResponse;
  9. import com.tencentcloudapi.iai.v20180301.models.FaceInfo;
  10. import sun.misc.BASE64Encoder;
  11. import javax.imageio.ImageIO;
  12. import java.awt.*;
  13. import java.awt.image.BufferedImage;
  14. import java.io.File;
  15. import java.io.FileInputStream;
  16. import java.io.FileNotFoundException;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import java.util.HashMap;
  20. public class DetectFace {
  21. public static void main(String[] args) {
  22. try {
  23. String imageUrl = "/Users/yuchk/Desktop/haha.png";
  24. String markImageUrl = "/Users/yuchk/Desktop/haha_res.png";
  25. // 替换自己的密钥
  26. Credential cred = new Credential("XX", "XX");
  27. HttpProfile httpProfile = new HttpProfile();
  28. httpProfile.setEndpoint("iai.tencentcloudapi.com");
  29. ClientProfile clientProfile = new ClientProfile();
  30. clientProfile.setHttpProfile(httpProfile);
  31. IaiClient client = new IaiClient(cred, "ap-beijing", clientProfile);
  32. HashMap map = new HashMap<String, String>(8);
  33. String image = getBase64Image(imageUrl);
  34. map.put("Image", image);
  35. map.put("NeedQualityDetection", "1");
  36. String params = JSON.toJSONString(map);
  37. DetectFaceRequest req = DetectFaceRequest.fromJsonString(params, DetectFaceRequest.class);
  38. DetectFaceResponse resp = client.DetectFace(req);
  39. System.out.println(DetectFaceRequest.toJsonString(resp));
  40. FaceInfo[] faceInfos = resp.getFaceInfos();
  41. long height = faceInfos[0].getHeight();
  42. long width = faceInfos[0].getWidth();
  43. long x = faceInfos[0].getX();
  44. long y = faceInfos[0].getY();
  45. // 将人脸标注起来
  46. BufferedImage bufferedImage = ImageIO.read(new File(imageUrl));
  47. Graphics g = bufferedImage.getGraphics();
  48. g.setColor(Color.RED);
  49. //矩形框(原点x坐标,原点y坐标,矩形的长,矩形的宽)
  50. g.drawRect((int) x, (int) y, (int) width, (int) height);
  51. g.dispose();
  52. FileOutputStream out = new FileOutputStream(markImageUrl);
  53. ImageIO.write(bufferedImage, "png", out);
  54. } catch (TencentCloudSDKException | IOException e) {
  55. System.out.println(e.toString());
  56. }
  57. }
  58. private static String getBase64Image(String url) {
  59. try {
  60. return getBase64Image(new FileInputStream(url));
  61. } catch (FileNotFoundException e) {
  62. e.printStackTrace();
  63. }
  64. return null;
  65. }
  66. private static String getBase64Image(FileInputStream inputStream) {
  67. try {
  68. byte[] data = new byte[inputStream.available()];
  69. inputStream.read(data);
  70. inputStream.close();
  71. BASE64Encoder encoder = new BASE64Encoder();
  72. return encoder.encode(data);
  73. } catch (IOException e) {
  74. e.printStackTrace();
  75. }
  76. return null;
  77. }
  78. }

结果

{"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"}
 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/225358
推荐阅读
相关标签
  

闽ICP备14008679号