阿里云人脸搜索最佳实践

Step By Step

1、pom.xml
    <dependencies>
        <!--添加oss相关依赖-->
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>3.10.2</version>
        </dependency>

        <!--java核心库-->
        <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>

        <!-- 人脸识别相关API -->
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>aliyun-java-sdk-facebody</artifactId>
            <version>1.0.8</version>
        </dependency>
    </dependencies>
2、完整的Java Code Sample
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
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.*;
import java.io.File;

public class SearchFaceDemo {

    // yourEndpoint填写Bucket所在地域对应的Endpoint。viapi绝大部分api目前仅支持上海区域oss,所以这里需要创建上海区域bucket,Endpoint填写为https://oss-cn-shanghai.aliyuncs.com。
    static String ossEndpoint = "https://oss-cn-shanghai.aliyuncs.com";
    static String bucketName = "<oss bucket>";
    static String ossDir = "facepics";  // 在oss bucket下面手动创建的文件夹
    // 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
    static String accessKeyId = "<ak>";
    static String accessKeySecret = "<sk>";
    // 创建OSSClient实例。
    static OSS ossClient = new OSSClientBuilder().build(ossEndpoint, accessKeyId, accessKeySecret);

    private static DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", accessKeyId, accessKeySecret);
    private static IAcsClient client = new DefaultAcsClient(profile);

    public static void main(String[] args) {

        // 1、人脸查找数据准备
        String dbName = "group1";
        String wangfei1 = "C:\\Users\\Administrator\\Desktop\\新建文件夹\\face1.jpg";
        String wangfei2 = "C:\\Users\\Administrator\\Desktop\\新建文件夹\\face2.jpg";
        String jingtian1 = "C:\\Users\\Administrator\\Desktop\\新建文件夹\\jingtian1.jpeg";
        String jingtian2 = "C:\\Users\\Administrator\\Desktop\\新建文件夹\\jingtian2.jpeg";

        // 2、创建FaceDB
//        createFaceDbRequest(dbName);

        // 3、添加人脸样本
//        String entityId1 = "wangfei";
//        String entityId2 = "jingtian";
//
//        addFaceEntity(dbName, entityId1);
//        addFaceEntity(dbName, entityId2);

        // 4、为样本添加人脸图片,注意本部分将图片url地址添加到:ExtraData参数
//        addFace(dbName,entityId1,uploadPicGetUrl(wangfei1));
//        addFace(dbName,entityId1,uploadPicGetUrl(wangfei2));
//        addFace(dbName,entityId2,uploadPicGetUrl(jingtian1));
//        addFace(dbName,entityId2,uploadPicGetUrl(jingtian2));

        // 5、查询目前人脸库的样本情况
//        listFaceEntities(dbName, 0);

        // 6、人脸搜索(人脸搜索功能可以根据输入图片,在数据库中搜索并返回相似的人脸图片数据。)
        searchFace(dbName,uploadPicGetUrl(wangfei1),2);

    }

    /**
     * 向oss上传文件,返回公共可读的oss 图片地址
     * @param filePath  图片本地路径
     * @return  oss url
     */
    public static String uploadPicGetUrl(String filePath)
    {
        // 创建PutObjectRequest对象。
        // 依次填写Bucket名称(例如examplebucket)、Object完整路径(例如exampledir/exampleobject.txt)和本地文件的完整路径。Object完整路径中不能包含Bucket名称。
        // 如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件,这里在ossbucket下面创建目录facepics用于存储上传的图片
        File file = new File(filePath);
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, ossDir+"/" + file.getName(), new File(filePath));

        // 如果需要上传时设置存储类型和访问权限,请参考以下示例代码。
         ObjectMetadata metadata = new ObjectMetadata();
        // metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
         metadata.setObjectAcl(CannedAccessControlList.PublicRead);  //为了方便获取的文件的公网读路径,这里设置文件的访问权限为公共读
         putObjectRequest.setMetadata(metadata);

        // 上传文件。
        ossClient.putObject(putObjectRequest);
        return "https://" + bucketName + ".oss-cn-shanghai.aliyuncs.com" + "/"+ossDir+"/" + file.getName();
    }

    /**
     * 创建人脸数据库
     * @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);
        addFaceRequest.setExtraData(imageUrl);// 注意,此处将图片地址保持到ExtraData中,为后续查询提供方便

        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());
        }
    }
}
3、查询结果
extraData对应的值即为对应样本人脸的原始图片地址。
搜索人脸:
{"requestId":"60981448-DC3B-518D-82B1-7**********","data":{"matchList":[{"faceItems":[{"faceId":"9770860","score":1.0,"extraData":"https://taro-shangai.oss-cn-shanghai.aliyuncs.com/facepics/face1.jpg","entityId":"wangfei"},{"faceId":"9770861","score":0.7668418,"extraData":"https://taro-shangai.oss-cn-shanghai.aliyuncs.com/facepics/face2.jpg","entityId":"wangfei"}],"location":{"x":217,"y":110,"width":170,"height":233}}]}}
4、使用说明
如果对1:N人脸查找流程使用有疑问,建议可以重点参考:阿里云视觉智能开放平台--人脸识别使用教程或者人脸搜索教程

相关参考

人脸搜索1:N

上一篇:如何使用MQTT.fx连接微服务消息队列


下一篇:JAVA分布式--ActiveMQ 消息中间件(上)