腾讯云【人脸识别】服务的一次尝试(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"}
 

 

 

## 使用前准备​ 1. 前往注册: [腾讯云账号注册](https://cloud.tencent.com/register) (详细指引见 [注册腾讯云](https://cloud.tencent.com/document/product/378/9603)) 2. 取得存储桶名称 **BucketName**: 请前往 [创建存储桶](https://cloud.tencent.com/document/product/460/10637) 3. 取得 **APPID**、**SecretId**、**SecretKey**:请前往 [云API密钥](https://console.cloud.tencent.com/cam/capi) ,点击“新建密钥” ## 快速体验 1. 修改文件 src/main/java/com/qcloud/image/demo/Demo.java 的 main() 方法,填入上述申请到的 **APPID**、**SecretId**、**SecretKey**、**BucketName** 2. 导入到 IDE:工程用 Maven 构建,以 Intellij IDEA 为例,导入方式为:Import Project -> 选择工程目录 -> Import project from external model -> Maven 3. 运行:Demo.java 右键,Run Demo.main() ## 使用简介 ### 初始化 ```java ImageClient imageClient = new ImageClient(APPID, SecretId, SecretKey); ``` ### 设置代理 根据实际网络环境,可能要设置代理,例如: ```java Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress("127.0.0.1", 8080)); imageClient.setProxy(proxy); ``` ### 使用 SDK 提供功能如下: **图像识别**:鉴黄,标签 **文字识别(OCR)**:身份证,名片,通用,驾驶证行驶证,营业执照,银行卡,车牌号 **人脸识别**:人脸检测,五官定位,个体信息管理,人脸验证,人脸对比及人脸检索 **人脸核身**:照片核身(通过照片和身份证信息),获取唇语验证码(用于活体核身),活体核身(通过视频和照片),活体核身(通过视频和身份证信息) ```java // 调用车牌识别API示例 String imageUrl = "http://youtu.qq.com/app/img/experience/char_general/icon_ocr_license_3.jpg"; String result = imageClient.ocrPlate(new OcrPlateRequest("bucketName", imageUrl)); System.out.println(result); ``` 更多例子详情可参见 [Demo.java](https://github.com/tencentyun/image-java-sdk-v2.0/blob/master/src/main/java/com/qcloud/image/demo/Demo.java) 的代码。 ## 集成到你的项目中 ### 获得 SDK jar 文件 1. 直接使用 release/*-with-dependencies.jar 2. 自行编译:在工程根目录下执行命令 `mvn assembly:assembly`,编译结果见 target/*-with-dependencies.jar ### 导入 jar 文件 根据项目具体情况导入 *-with-dependencies.jar
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

康雨城

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值