springboot專案實現人臉對比
阿新 • • 發佈:2020-12-21
技術標籤:java後端
話不多說,直接擼。。。
這邊我選用阿里雲的
maven 依賴
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-facebody</artifactId>
<version>1.0.8</version>
</dependency>
介面展示:
@ApiOperation(value = "匹配照片相似度")
@RequestMapping (value = "matchingImgSimilarity",method = RequestMethod.GET)
public Result matchingImgSimilarity(@RequestParam @ApiParam(name = "img1",required = true,value = "照片一")String img1,
@RequestParam @ApiParam(name = "img2", required = true,value = "照片二")String img2){
Float aFloat = faceContrast(img1, img2);
return new Result(true, StatusCode.OK,"操作成功", Math.round(aFloat));
}
匹配業務層
/**
* 方法實現說明
圖片要求:
請求格式:JPEG、JPG、PNG、BMP。
影象大小:影象大小不超過3M。
影象解析度:圖片大小要求5x5畫素以上,人臉的尺寸建議大於64x64畫素。
@param imageURLA 圖片A
@param imageURLB 圖片B
* @author 金剛
* @date 2020/12/18 16:18
*/
public static Float faceContrast(String imageURLA,String imageURLB){
//目前區域只開通了華東2(上海)
DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "你的阿里雲賬號id", "你的阿里祕鑰");
IAcsClient client = new DefaultAcsClient(profile);
CompareFaceRequest request = new CompareFaceRequest();
request.setRegionId("cn-shanghai");
request.setImageURLA(imageURLA);
request.setImageURLB(imageURLB);
Float confidence = 0f;
try {
CompareFaceResponse response = client.getAcsResponse(request);
System.out.println(new Gson().toJson(response));
confidence = response.getData().getConfidence();
} 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());
}
return confidence;
}
效果圖
以上僅是個人看法,有意見歡迎留言交流