人脸口罩检测(DetectMask)是阿里云智能开放平台推出的人脸人体能力下的一个API接口,您可以调用该接口检测图片中最大人脸佩戴口罩的情况。
开通服务
调用人脸口罩检测的API前,您需要开通人脸人体服务。在进行口罩检测时,需要先将图片上传至上海地域的OSS Bucket中生成URL链接,通过图片URL链接传入图片数据。关于如何开通OSS服务请参见开通OSS服务。下面为您介绍如何开通人脸人体服务。
生成URL
例如我们需要对下图进行口罩检测。
添加依赖包
请根据您的代码选择添加不同的依赖包。
- Java
在您的Java项目中添加人脸人体能力的POM依赖。
<dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>4.5.14</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.2.2</version> </dependency>
- Python
打开命令提示符窗口,依次执行如下命令安装Python SDK。
pip install aliyun-python-sdk-core
pip install aliyun-python-sdk-facebody
- Node.js
使用NPM安装Node.js SDK。
npm install @alicloud/pop-core -S
npm install @alicloud/facebody20191230@1.2.1 --save
- PHP
使用composer工具安装PHP SDK。
composer require alibabacloud/facebody-20191230
- C#
使用NuGet程序包安装.NET SDK。
- 如果您的Visual Studio没有安装NuGet,请先安装NuGet。
- 在Visual Studio中新建或者打开已有的项目,选择 。
- 搜索aliyun-net-sdk-core,在结果中找到aliyun-net-sdk-core,选择最新版本,单击安装。
请求示例
请求示例如下:
- Java请求示例
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 java.util.*; import com.aliyuncs.facebody.model.v20191230.*; public class DetectMask { public static void main(String[] args) { DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "<accessKeyId>", "<accessSecret>"); IAcsClient client = new DefaultAcsClient(profile); DetectMaskRequest request = new DetectMaskRequest(); request.setRegionId("cn-shanghai"); request.setImageURL("https://viapi-test.oss-cn-shanghai.aliyuncs.com/test-team/tiankong/1%E5%8F%A3%E7%BD%A9%E8%AF%86%E5%88%AB.jpg"); try { DetectMaskResponse response = client.getAcsResponse(request); 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()); } } }
- Python请求示例
#!/usr/bin/env python #coding=utf-8 from aliyunsdkcore.client import AcsClient from aliyunsdkcore.acs_exception.exceptions import ClientException from aliyunsdkcore.acs_exception.exceptions import ServerException from aliyunsdkfacebody.request.v20191230.DetectMaskRequest import DetectMaskRequest client = AcsClient('<accessKeyId>', '<accessSecret>', 'cn-shanghai') request = DetectMaskRequest() request.set_accept_format('json') request.set_ImageURL("https://viapi-test.oss-cn-shanghai.aliyuncs.com/test-team/tiankong/1%E5%8F%A3%E7%BD%A9%E8%AF%86%E5%88%AB.jpg") response = client.do_action_with_exception(request) # python2: print(response) print(str(response, encoding='utf-8'))
- Node.js请求示例
const Core = require('@alicloud/pop-core'); var client = new Core({ accessKeyId: '<accessKeyId>', accessKeySecret: '<accessSecret>', endpoint: 'https://facebody.cn-shanghai.aliyuncs.com', apiVersion: '2019-12-30' }); var params = { "RegionId": "cn-shanghai", "ImageURL": "https://viapi-test.oss-cn-shanghai.aliyuncs.com/test-team/tiankong/1%E5%8F%A3%E7%BD%A9%E8%AF%86%E5%88%AB.jpg" } var requestOption = { method: 'POST' }; client.request('DetectMask', params, requestOption).then((result) => { console.log(JSON.stringify(result)); }, (ex) => { console.log(ex); })
- PHP请求示例
<?php use AlibabaCloud\Client\AlibabaCloud; use AlibabaCloud\Client\Exception\ClientException; use AlibabaCloud\Client\Exception\ServerException; // Download:https://github.com/aliyun/openapi-sdk-php // Usage:https://github.com/aliyun/openapi-sdk-php/blob/master/README.md AlibabaCloud::accessKeyClient('<accessKeyId>', '<accessSecret>') ->regionId('cn-shanghai') ->asDefaultClient(); try { $result = AlibabaCloud::rpc() ->product('facebody') // ->scheme('https') // https | http ->version('2019-12-30') ->action('DetectMask') ->method('POST') ->host('facebody.cn-shanghai.aliyuncs.com') ->options([ 'query' => [ 'RegionId' => "cn-shanghai", 'ImageURL' => "https://viapi-test.oss-cn-shanghai.aliyuncs.com/test-team/tiankong/1%E5%8F%A3%E7%BD%A9%E8%AF%86%E5%88%AB.jpg", ], ]) ->request(); print_r($result->toArray()); } catch (ClientException $e) { echo $e->getErrorMessage() . PHP_EOL; } catch (ServerException $e) { echo $e->getErrorMessage() . PHP_EOL; }
- .NET请求示例
using System; using System.Collections.Generic; using Aliyun.Acs.Core; using Aliyun.Acs.Core.Profile; using Aliyun.Acs.Core.Exceptions; using Aliyun.Acs.Core.Http; namespace CommonRequestDemo { class Program { static void Main(string[] args) { IClientProfile profile = DefaultProfile.GetProfile("cn-shanghai", "<accessKeyId>", "<accessSecret>"); DefaultAcsClient client = new DefaultAcsClient(profile); CommonRequest request = new CommonRequest(); request.Method = MethodType.POST; request.Domain = "facebody.cn-shanghai.aliyuncs.com"; request.Version = "2019-12-30"; request.Action = "DetectMask"; // request.Protocol = ProtocolType.HTTP; request.AddQueryParameters("ImageURL", "https://viapi-test.oss-cn-shanghai.aliyuncs.com/test-team/tiankong/1%E5%8F%A3%E7%BD%A9%E8%AF%86%E5%88%AB.jpg"); try { CommonResponse response = client.GetCommonResponse(request); Console.WriteLine(System.Text.Encoding.Default.GetString(response.HttpResponse.Content)); } catch (ServerException e) { Console.WriteLine(e); } catch (ClientException e) { Console.WriteLine(e); } } } }
结果示例
该接口的请求结果示例如下。
{
"RequestId": "9EF50A27-8C66-4835-948E-C47E3DD77B3F",
"Data": {
"Mask": 2,
"FaceProbability": "0.63257104158401489"
}
}
从返回结果来看,该图片中人物佩戴了口罩。具体信息如下:
- RequestId:请求ID。
- Mask:取值为2,表示佩戴了口罩。
- FaceProbability:检测结果的可信度。
关于Mask的取值参考如下:
- 0:没有检测出人脸或人脸清晰度不够。
- 1:没有戴口罩。
- 2:有戴口罩。
- 3:口罩没有带好。
说明 如果检测到有带口罩,但是没有遮住鼻子或者嘴巴,那么就会被认为是没有带好,则Mask的值就为3。