阿里云 人脸识别工具类

识别本地图片

通过本地上传的图片路径【E:\Burner-1080.jpg】,找到这张Burner-1080.jpg图片,进行分析判断,如果图片没有人脸将会对图片进行删除,否则返回true

java依赖

		<!-- 以下是人脸识别依赖 -->
		<dependency>
			<groupId>com.aliyun</groupId>
			<artifactId>aliyun-java-sdk-core</artifactId>
			<version>4.1.1</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.52</version>
		</dependency>
		<dependency>
			<groupId>com.aliyun</groupId>
			<artifactId>aliyun-java-sdk-green</artifactId>
			<version>3.6.1</version>
		</dependency>
		<dependency>
			<groupId>com.aliyun.oss</groupId>
			<artifactId>aliyun-sdk-oss</artifactId>
			<version>2.8.3</version>
		</dependency>
		<dependency>
			<groupId>com.aliyun</groupId>
			<artifactId>facebody20191230</artifactId>
			<version>0.2.9</version>
		</dependency>
		<!-- 以上是人脸识别依赖 -->
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.facebody20191230.Client;
import com.aliyun.facebody20191230.models.*;
import com.aliyun.tearpc.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.google.gson.Gson;

public class DetectFace {
	@SuppressWarnings("resource")
	public static boolean jianceface(String imgPath) throws Exception {

		// accessKeyId、accessKeySecret 获取参考:https://yq.aliyun.com/articles/693979
		Config config = new Config();
		config.accessKeyId = "**********";
		// 你的 accessKeySecret
		config.accessKeySecret = "************";
		config.regionId = "cn-shanghai";
		config.protocol = "https";
		config.endpoint = "facebody.cn-shanghai.aliyuncs.com";

		Client client = null;
		client = new Client(config);
		String filePath = imgPath;
		RecognizeFaceAdvanceRequest recognizeFaceAdvanceRequest = new RecognizeFaceAdvanceRequest();
		InputStream inputStream = null;
		inputStream = new FileInputStream(new File(filePath));
		recognizeFaceAdvanceRequest.imageURLObject = inputStream;
		RecognizeFaceResponse recognizeFaceResponse = client.recognizeFaceAdvance(recognizeFaceAdvanceRequest,
				new RuntimeOptions());
		String result = new Gson().toJson(recognizeFaceResponse);
		System.out.println("人脸属性识别::" + result);
		JSONObject object = JSONObject.parseObject(result);
		int facenum = Integer.parseInt(JSONObject.parseObject(object.getString("data")).getString("faceCount"));
		System.out.println("人脸数量::" + facenum);
		if (facenum <= 0) {
			return true;
		} else {
			File file = new File(imgPath);
            //判断文件是否存在
            if (file.exists() == true){
                System.out.println("图片存在,可执行删除操作");
                Boolean flag = false;
                flag = file.delete();
                if (flag){
                    System.out.println("成功删除图片"+file.getName());
                }else {
                    System.out.println("删除失败");
                }
            }else {
                System.out.println("图片不存在,终止操作");
            }
			return false;
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值