阿里云视觉智能开放平台--人脸识别使用教程

  近期平台上线了30余个算法,其中就包括大家比较关心的人脸比对1:N算法,为了更方便大家调用此算法,接下来给大家分享一份代码样例,其中包含了如何对接并调用人脸比对1:N,对此算法感兴趣的小伙伴千万不要错过哟~
步骤
1、服务开通,参考链接:阿里云视觉智能开放平台使用简明教程

2、目前主要提供三个方面的能力:人脸识别、人体识别、人脸对比,能力介绍

3、其中人脸识别、人体识别 目前主要是单个API的调用,不涉及多个API的协同调用,使用相对简单(实例仅选择一个API演示)。人脸对比涉及到人脸库、样本、图片等的操作,需要多个API的协同操作:
阿里云视觉智能开放平台--人脸识别使用教程

4、代码样例

  • 4.1 pom.xml
  <dependencies>
        <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>
    </dependencies>

- 4.2 Java Code

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 Demo1 {

    // 阿里云常见参数获取位置 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());
        }
    }
}

以上内容转载自taro_秋刀鱼

  为了更好的帮助中小企业和独立开发者快速对接视觉AI算法,阿里云视觉智能开放平台(vision.aliyun.com)免费开放平台上现有的100余种视觉AI算法服务的使用权限,没有中间商赚差价,服务调用不收取任何费用!感兴趣的同学可点击下方链接访问我们的官网进行体验,也可搜索钉钉群23109592或是扫描文章结尾的钉群二维码,进群和我们沟通!
  官网地址:https://vision.aliyun.com/

阿里云视觉智能开放平台--人脸识别使用教程
上一篇:cas4.2.7与shiro进行整合


下一篇:从传统银行到互联网,异地多活难不难?(1)