欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

阿里开源人脸识别API

程序员文章站 2022-07-12 20:57:33
...

一、项目
阿里开源人脸识别API
二、jar包导入

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.citydo</groupId>
    <artifactId>faceadd</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>faceadd</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-core</artifactId>
            <version>4.4.8</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.52</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.aliyun/aliyun-java-sdk-facebody -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-facebody</artifactId>
            <version>1.0.8</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

三、案例

package com.citydo.faceadd;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import com.aliyuncs.facebody.model.v20191230.*;


public class FaceDemo {

    // 阿里云常见参数获取位置 https://yq.aliyun.com/articles/693979
    private static DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "<access key>", "<access secret>");
    private static IAcsClient client = new DefaultAcsClient(profile);

    public static void main(String[] args) {

        // 人脸识别  --  人脸属性识别
        String recognizeFaceURL = "http://vigen-invi.oss-cn-shanghai.aliyuncs.com/demo/test.jpg";
        recognizeFace(recognizeFaceURL);

        // 人体识别  --  人体计算
        String detectBodyCountURL = "https://viapi-test.oss-cn-shanghai.aliyuncs.com/test/facebody/RecognizeBodyCount/ddd2512910b84bc3ffeba5ac44b30d81.jpg";
        detectBodyCount(detectBodyCountURL);

        // 人脸对比
        String dbName = "tarogroup";
        Integer Offset = 0;
        String wangfei1 = "https://taroshanghai.oss-cn-shanghai.aliyuncs.com/viapipictest/face1.jpg";
        String wangfei2 = "https://taroshanghai.oss-cn-shanghai.aliyuncs.com/viapipictest/face2.jpg";
        String jingtian1 = "https://taroshanghai.oss-cn-shanghai.aliyuncs.com/viapipictest/jingtian1.jpeg";
        String jingtian2 = "https://taroshanghai.oss-cn-shanghai.aliyuncs.com/viapipictest/jingtian2.jpeg";

        compareFace(wangfei1,jingtian1);// 人脸对比

        // 1:N 人脸查找
        //        deleteFaceDb(dbName);
        //        listFaceEntities(dbName, Offset);
        //        deleteFaceEntity(dbName,"2");

        //        listFaceDbs();
        //        createFaceDbRequest(dbName);
        //        listFaceDbs();


        String entityId1 = "wangfei";
        String entityId2 = "jingtian";

        // 创建人脸样本
        //        addFaceEntity(dbName, entityId1);
        //        addFaceEntity(dbName, entityId2);


        // 想人脸样本中加入人脸,每个样本人脸上限为5
        //        addFace(dbName,entityId1,wangfei1);
        //        addFace(dbName,entityId1,wangfei2);
        //
        //        addFace(dbName,entityId2,jingtian1);
        //        addFace(dbName,entityId2,jingtian2);
        //
        //        listFaceEntities(dbName, 0);

        // 到人脸库中查找
        searchFace(dbName,wangfei1,1);

    }

    /**
     * 人脸属性识别
     * @param imageURL 图片URL地址
     */
    private static void recognizeFace(String imageURL)
    {
        RecognizeFaceRequest recognizeFaceRequest = new RecognizeFaceRequest();
        recognizeFaceRequest.setImageURL(imageURL);

        try {
            RecognizeFaceResponse recognizeFaceResponse = client.getAcsResponse(recognizeFaceRequest);
            System.out.println("人脸属性识别:");
            System.out.println(new Gson().toJson(recognizeFaceResponse));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }

    /**
     * 人体计数
     * @param imageURL 图片URL地址
     */
    private static void detectBodyCount(String imageURL)
    {
        DetectBodyCountRequest detectBodyCountRequest = new DetectBodyCountRequest();
        detectBodyCountRequest.setImageURL(imageURL);

        try {
            DetectBodyCountResponse detectBodyCountResponse = client.getAcsResponse(detectBodyCountRequest);
            System.out.println("人体计数:");
            System.out.println(new Gson().toJson(detectBodyCountResponse));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }

    /**
     * 创建人脸数据库
     * @param dbName 数据库名称
     */
    private static void createFaceDbRequest(String dbName)
    {
        CreateFaceDbRequest createFaceDbRequest = new CreateFaceDbRequest();
        createFaceDbRequest.setName(dbName);

        try {
            CreateFaceDbResponse response = client.getAcsResponse(createFaceDbRequest);
            System.out.println("创建人脸数据库:");
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }

    /**
     * 查看数据库列表
     */
    private static void listFaceDbs()
    {
        ListFaceDbsRequest listFaceDbsRequest = new ListFaceDbsRequest();
        try {
            ListFaceDbsResponse response = client.getAcsResponse(listFaceDbsRequest);
            System.out.println("查看数据库列表: ");
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }

    /**
     * 添加人脸样本
     * @param dbName 数据库名称
     * @param entityId 实体ID
     */
    private static void addFaceEntity(String dbName, String entityId)
    {
        AddFaceEntityRequest addFaceEntityRequest = new AddFaceEntityRequest();
        addFaceEntityRequest.setDbName(dbName);
        addFaceEntityRequest.setEntityId(entityId);
        try{
            AddFaceEntityResponse addFaceEntityResponse = client.getAcsResponse(addFaceEntityRequest);
            System.out.println("添加人脸样本:");
            System.out.println(new Gson().toJson(addFaceEntityResponse));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }

    /**
     * 查询人脸样本
     * @param dbName 数据库名称
     * @param entityId 实体ID
     */
    private static void getFaceEntity(String dbName, String entityId)
    {
        GetFaceEntityRequest getFaceEntityRequest = new GetFaceEntityRequest();
        getFaceEntityRequest.setDbName(dbName);
        getFaceEntityRequest.setEntityId(entityId);
        try{
            GetFaceEntityResponse getFaceEntityResponse = client.getAcsResponse(getFaceEntityRequest);
            System.out.println("查询人脸样本:");
            System.out.println(new Gson().toJson(getFaceEntityResponse));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }

    /**
     * 查询人脸样本列表
     * @param dbName 数据库名称
     * @param Offset 起始记录
     */
    private static void listFaceEntities(String dbName, Integer Offset)
    {
        ListFaceEntitiesRequest listFaceEntitiesRequest = new ListFaceEntitiesRequest();
        listFaceEntitiesRequest.setDbName(dbName);
        listFaceEntitiesRequest.setOffset(Offset);
        try{
            ListFaceEntitiesResponse listFaceEntitiesResponse = client.getAcsResponse(listFaceEntitiesRequest);
            System.out.println("查询人脸样本列表:");
            System.out.println(new Gson().toJson(listFaceEntitiesResponse));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }


    /**
     * 更新人脸样本
     * @param dbName 数据库名称
     * @param entityId 实体ID
     * @param labels 标签名称
     */
    private static void updateFaceEntity(String dbName, String entityId, String labels)
    {
        UpdateFaceEntityRequest updateFaceEntityRequest = new UpdateFaceEntityRequest();
        updateFaceEntityRequest.setDbName(dbName);
        updateFaceEntityRequest.setEntityId(entityId);
        updateFaceEntityRequest.setLabels(labels);
        try{
            UpdateFaceEntityResponse updateFaceEntityResponse = client.getAcsResponse(updateFaceEntityRequest);
            System.out.println("更新人脸样本:");
            System.out.println(new Gson().toJson(updateFaceEntityResponse));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }

    /**
     * 删除人脸样本
     * @param dbName 数据库名称
     * @param entityId 实体ID
     */
    private static void deleteFaceEntity(String dbName, String entityId)
    {
        DeleteFaceEntityRequest deleteFaceEntityRequest = new DeleteFaceEntityRequest();
        deleteFaceEntityRequest.setDbName(dbName);
        deleteFaceEntityRequest.setEntityId(entityId);
        try{
            DeleteFaceEntityResponse deleteFaceEntityResponse = client.getAcsResponse(deleteFaceEntityRequest);
            System.out.println("删除人脸样本:");
            System.out.println(new Gson().toJson(deleteFaceEntityResponse));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }

    /**
     * 添加人脸数据
     * @param dbName 数据库名称
     * @param entityId 实体ID
     * @param imageUrl 人脸图片地址,必须是同Region的OSS的图片地址。人脸必须是正面无遮挡单人人脸。
     */
    private static void addFace(String dbName, String entityId, String imageUrl)
    {
        AddFaceRequest addFaceRequest = new AddFaceRequest();
        addFaceRequest.setDbName(dbName);
        addFaceRequest.setEntityId(entityId);
        addFaceRequest.setImageUrl(imageUrl);

        try{
            AddFaceResponse addFaceResponse = client.getAcsResponse(addFaceRequest);
            System.out.println("添加人脸数据:");
            System.out.println(new Gson().toJson(addFaceResponse));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }

    /**
     * 搜索人脸
     * @param dbName 数据库名称
     * @param imageUrl 图片URL地址。必须是同Region的OSS地址。
     */
    private static void searchFace(String dbName, String imageUrl, Integer limit)
    {
        SearchFaceRequest searchFaceRequest = new SearchFaceRequest();
        searchFaceRequest.setDbName(dbName);
        searchFaceRequest.setImageUrl(imageUrl);
        searchFaceRequest.setLimit(limit);
        try{
            SearchFaceResponse searchFaceResponse = client.getAcsResponse(searchFaceRequest);
            System.out.println("搜索人脸:");
            System.out.println(new Gson().toJson(searchFaceResponse));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }

    /**
     * 删除数据库
     * @param dbName 数据库名称
     */
    private static void deleteFace(String dbName, String faceId)
    {
        DeleteFaceRequest deleteFaceRequest = new DeleteFaceRequest();
        deleteFaceRequest.setDbName(dbName);
        deleteFaceRequest.setFaceId(faceId);
        try{
            DeleteFaceResponse deleteFaceResponse = client.getAcsResponse(deleteFaceRequest);
            System.out.println("删除人脸:");
            System.out.println(new Gson().toJson(deleteFaceResponse));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }

    /**
     * 删除数据库
     * @param dbName 数据库名称
     */
    private static void deleteFaceDb(String dbName)
    {
        DeleteFaceDbRequest deleteFaceDbRequest = new DeleteFaceDbRequest();
        deleteFaceDbRequest.setName(dbName);
        try{
            DeleteFaceDbResponse deleteFaceDbResponse = client.getAcsResponse(deleteFaceDbRequest);
            System.out.println("删除数据库:");
            System.out.println(new Gson().toJson(deleteFaceDbResponse));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }

    /**
     * 人脸对比
     * @param imageURLA 待对比图片URL地址A
     * @param imageURLB 待对比图片URL地址B
     */
    private static void compareFace(String imageURLA, String imageURLB)
    {
        CompareFaceRequest compareFaceRequest = new CompareFaceRequest();
        compareFaceRequest.setImageURLA(imageURLA);
        compareFaceRequest.setImageURLB(imageURLB);
        try{
            CompareFaceResponse compareFaceResponse = client.getAcsResponse(compareFaceRequest);
            System.out.println("人脸对比:");
            System.out.println(new Gson().toJson(compareFaceResponse));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            System.out.println("ErrCode:" + e.getErrCode());
            System.out.println("ErrMsg:" + e.getErrMsg());
            System.out.println("RequestId:" + e.getRequestId());
        }
    }
}

参考:阿里开源视觉算法
参考:阿里云视觉开放平台