口罩識別實戰教程
人臉人體識別技術是基於阿里雲深度學習算法,結合圖像或視頻的人臉檢測、分析、比對以及人體檢測等技術,為您提供人臉人體的檢測定位、人臉屬性識別和人臉比對等能力。本教程介紹如何使用Alibaba Cloud SDK for Java對圖片中的人物進行口罩檢測。
前提條件
在開始之前,請確保完成以下步驟:
- 開通人臉人體能力,請參見上述開發前準備。
- 在您的Java工程中添加人臉人體能力的pom依賴:
<!-- 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>
口罩識別
DetectMask可以對輸入圖片中面積最大的人臉進行口罩檢測。
例如要識別下面的圖片中的人物是否戴了口罩。
示例代碼如下:
import com.aliyun.CommonConfig;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.facebody.model.v20191230.DetectMaskRequest;
import com.aliyuncs.facebody.model.v20191230.DetectMaskResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
public class DetectMaskDemo {
private static DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "<access key id>", "<access key secret>");
private static IAcsClient client = new DefaultAcsClient(profile);
public static void main(String[] args) {
String wearMaskSampleImgURL = "https://visionapi-test.oss-cn-shanghai.aliyuncs.com/mask_1.jpg";
detectMask(wearMaskSampleImgURL);
}
/**
* 口罩識別
* @param wearMaskSampleImgURL 圖片URL地址
*/
private static void detectMask(String wearMaskSampleImgURL) {
DetectMaskRequest detectMaskRequest = new DetectMaskRequest();
detectMaskRequest.setImageURL(wearMaskSampleImgURL);
try {
DetectMaskResponse detectMaskResponse = client.getAcsResponse(detectMaskRequest);
System.out.println("口罩識別:");
System.out.println(new Gson().toJson(detectMaskResponse));
} 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());
}
}
}
代碼返回結果類似如下:
{
"requestId": "3DFE230C-CB35-4F92-981F-F70D078E0C8D",
"data": {
"mask": 2,
"faceProbability": 0.57101476
}
}
從返回結果中得到的該圖片識別結果如下:
- 圖片中的人物戴了口罩。
- 檢測結果的可信度為0.57101476。
其中,返回結果中參數mask取值如下:
- 0:沒有檢測出人臉或人臉清晰度不夠。
- 1:沒有戴口罩。
- 2:有戴口罩。
- 3:口罩沒有帶好。