java阿里云图片检测同/异步sdk调用详解

java阿里云图片检测同/异步sdk调用

sdk调用流程:注册阿里云账号→创建RAM子账号→子账号授权→复制子账号的AccessKey ID&Access Key Secret→sdk&api调用
阿里云RAM子账号创建、授权、api文档详情查看

注意:调用阿里云接口的时候最容易出错的俩个地方

		1.AccessKey ID&Access Key Secret
			因为阿里云是主张接口调用都是给子账号权限然后用子账号的AccessKey ID&Access Key Secret来调用接口,所有我们复制这俩个参数的时候很容易复制成我们主账号的这俩参数去了,切记一定要复制子账号的这俩参数!!
		2.权限添加
			很多时候我们所有步骤都作对了就是连不通的原因,就是子账号的权限没给,如我现在要调文本反垃圾的接口但是我没给子账号这个权限是无论如何都调不通的切记!!

阿里云不仅提供了api还提供了sdk,有了sdk我们就方便多了,阿里云帮我们封装了所有的方法我们只需要复制粘贴就能用这些接口了,但是前提得注意我上面说的一些注意事项切记嗷java阿里云sdk地址
1.同步图片检测:同步检测图片中的违规内容,实时返回检测结果。
支持检测的场景包括:图片智能鉴黄、图片暴恐涉政识别、图片广告识别、图片二维码识别、图片不良场景识别、图片logo识别。
阿里云图片同步检测接口详情及请求、返回参数详情查看
1):同步图片检测代码如下:

/**
     * 图片同步检测
     *
     * @throws Exception
     */
    @org.junit.Test
    public void aliyunImageSyncCheck() throws Exception {
        //请替换成你自己的accessKeyId、accessKeySecret
        IClientProfile profile = DefaultProfile.getProfile(ConfigAliyun.regionId, ConfigAliyun.accessKeyId, ConfigAliyun.accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        ImageSyncScanRequest imageSyncScanRequest = new ImageSyncScanRequest();
        imageSyncScanRequest.setAcceptFormat(FormatType.JSON); // 指定api返回格式
        imageSyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法
        imageSyncScanRequest.setEncoding("utf-8");
        imageSyncScanRequest.setRegionId(ConfigAliyun.regionId);


        List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
        Map<String, Object> task = new LinkedHashMap<String, Object>();
        task.put("dataId", UUID.randomUUID().toString());
        //https://img.alicdn.com/tfs/TB1Xk_qvwmTBuNjy1XbXXaMrVXa-550-407.jpg
        task.put("url", "https://img.alicdn.com/tfs/TB1k_g9l26H8KJjSspmXXb2WXXa-600-600.jpg");
        task.put("time", new Date());

        tasks.add(task);
        JSONObject data = new JSONObject();
        /**
         * porn: 色情
         * terrorism: 暴恐
         * qrcode: 二维码
         * ad: 图片广告
         * ocr: 文字识别
         */
        data.put("scenes", Arrays.asList("porn", "terrorism"));
        data.put("tasks", tasks);

        imageSyncScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);

        /**
         * 请务必设置超时时间
         */
        imageSyncScanRequest.setConnectTimeout(3000);
        imageSyncScanRequest.setReadTimeout(10000);

        try {
            HttpResponse httpResponse = client.doAction(imageSyncScanRequest);

            if (httpResponse.isSuccess()) {
                JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
                System.out.println(JSON.toJSONString(scrResponse, true));
                if (200 == scrResponse.getInteger("code")) {
                    JSONArray taskResults = scrResponse.getJSONArray("data");
                    for (Object taskResult : taskResults) {
                        if (200 == ((JSONObject) taskResult).getInteger("code")) {
                            JSONArray sceneResults = ((JSONObject) taskResult).getJSONArray("results");
                            for (Object sceneResult : sceneResults) {
                                String scene = ((JSONObject) sceneResult).getString("scene");
                                String suggestion = ((JSONObject) sceneResult).getString("suggestion");
                                //根据scene和suggetion做相关的处理
                                //do something
                                System.out.println("scene = [" + scene + "]");
                                System.out.println("suggestion = [" + suggestion + "]");
                            }
                        } else {
                            System.out.println("task process fail:" + ((JSONObject) taskResult).getInteger("code"));
                        }
                    }
                } else {
                    System.out.println("detect not success. code:" + scrResponse.getInteger("code"));
                }
            } else {
                System.out.println("response not success. status:" + httpResponse.getStatus());
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

2):同步图片检测结果如下:

{
	"msg":"OK",
	"code":200,
	"data":[
		{
			"msg":"OK",
			"code":200,
			"dataId":"3d583265-d9cd-4159-83d8-68e8a2616ddf",
			"extras":{},
			"results":[
				{
					"rate":99.700005,
					"suggestion":"block",
					"label":"porn",
					"scene":"porn"
				},
				{
					"rate":99.99,
					"suggestion":"pass",
					"label":"normal",
					"scene":"terrorism"
				}
			],
			"taskId":"imgitaNeo8XlI5fNtKt93rzT-1r9AuE",
			"url":"https://img.alicdn.com/tfs/TB1k_g9l26H8KJjSspmXXb2WXXa-600-600.jpg"
		}
	],
	"requestId":"0C0A9B1C-BB89-47B8-8AB8-7FE7BA65D31D"
}
scene = [porn]
suggestion = [block]
scene = [terrorism]
suggestion = [pass]

2.异步图片检测:提交图片异步检测任务,检测图片违规或识别图片中的不良信息。

支持检测的场景包括:图片智能鉴黄、图片暴恐涉政识别、图片广告识别、图片二维码识别、图片不良场景识别、图片logo识别。

异步检测任务不会实时返回检测结果,您需要通过轮询的方式或者callback的方式获取检测结果。。
阿里云图片异步检测接口详情及请求、返回参数详情查看
1):异步图片检测代码如下:

  @org.junit.Test
    public void aliyunImagecheck()throws  Exception{
        //图片url
        String srcUrl = "http://pic12.nipic.com/20110221/6727421_210944911000_2.jpg";
        System.out.println("srcUrl:"+srcUrl);
        //获取图片异步检测taskId
        String taskId = aliyunImageASyncCheck(srcUrl);
        System.out.println("taskId:"+taskId);
        //根据taskId查询图片异步检测结果
        String imgCheckResult = aliyunImageAsyncResultCheck(taskId);
        System.out.println("imgCheckResult:"+imgCheckResult);

    }
    /**
     * 图片异步检测
     * @throws Exception
     */
    public String aliyunImageASyncCheck(String srcUrl) throws Exception {
        String result = "";
        //请替换成你自己的accessKeyId、accessKeySecret
        IClientProfile profile = DefaultProfile.getProfile("你的regionId", "你的accessKeyId", "你的accessKeySecret");
        IAcsClient client = new DefaultAcsClient(profile);

        ImageAsyncScanRequest imageAsyncScanRequest = new ImageAsyncScanRequest();
        imageAsyncScanRequest.setAcceptFormat(FormatType.JSON); // 指定api返回格式
        imageAsyncScanRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法
        imageAsyncScanRequest.setEncoding("utf-8");
        imageAsyncScanRequest.setRegionId(ConfigAliyun.regionId);


        List<Map<String, Object>> tasks = new ArrayList<Map<String, Object>>();
        Map<String, Object> task1 = new LinkedHashMap<String, Object>();
        task1.put("dataId", UUID.randomUUID().toString());
        //"http://pic12.nipic.com/20110221/6727421_210944911000_2.jpg"
        task1.put("url", srcUrl);
        task1.put("time", new Date());

        tasks.add(task1);
        JSONObject data = new JSONObject();
        /**
         * porn: 色情
         * terrorism: 暴恐
         * qrcode: 二维码
         * ad: 图片广告
         * ocr: 文字识别
         */
        data.put("scenes", Arrays.asList("porn", "ocr", "qrcode", "sface"));
        data.put("tasks", tasks);

        imageAsyncScanRequest.setHttpContent(data.toJSONString().getBytes("UTF-8"), "UTF-8", FormatType.JSON);

        /**
         * 请务必设置超时时间
         */
        imageAsyncScanRequest.setConnectTimeout(3000);
        imageAsyncScanRequest.setReadTimeout(6000);

        try {
            HttpResponse httpResponse = client.doAction(imageAsyncScanRequest);

            if (httpResponse.isSuccess()) {
                JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
                System.out.println(JSON.toJSONString(scrResponse, true));
                if (200 == scrResponse.getInteger("code")) {
                    JSONArray taskResults = scrResponse.getJSONArray("data");
                    for (Object taskResult : taskResults) {
                        if (200 == ((JSONObject) taskResult).getInteger("code")) {
                            String taskId = ((JSONObject) taskResult).getString("taskId");
                            // 将taskId 保存下来,间隔一段时间来轮询结果, 参照ImageAsyncScanResultsRequest
                            System.out.println("args = [" + taskId + "]");
                            result = taskId;

                        } else {
                            System.out.println("task process fail:" + ((JSONObject) taskResult).getInteger("code"));
                        }
                    }
                } else {
                    System.out.println("detect not success. code:" + scrResponse.getInteger("code"));
                }
            } else {
                System.out.println("response not success. status:" + httpResponse.getStatus());
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  result;
    }

    /**
     * 图片异步检测结果查询
     * @throws Exception
     */
    public static String aliyunImageAsyncResultCheck(String taskId)throws  Exception{
        String result = "";
        //请替换成你自己的accessKeyId、accessKeySecret
        IClientProfile profile = DefaultProfile.getProfile("你的regionId", "你的accessKeyId", "你的accessKeySecret");
        IAcsClient client = new DefaultAcsClient(profile);

        ImageAsyncScanResultsRequest imageAsyncScanResultsRequest = new ImageAsyncScanResultsRequest();
        imageAsyncScanResultsRequest.setAcceptFormat(FormatType.JSON); // 指定api返回格式
        imageAsyncScanResultsRequest.setMethod(com.aliyuncs.http.MethodType.POST); // 指定请求方法
        imageAsyncScanResultsRequest.setEncoding("utf-8");
        imageAsyncScanResultsRequest.setRegionId(ConfigAliyun.regionId);


        List<String> taskIds = new ArrayList<String>();
        taskIds.add(taskId);
        imageAsyncScanResultsRequest.setHttpContent(JSON.toJSONString(taskIds).getBytes("UTF-8"), "UTF-8", FormatType.JSON);

        /**
         * 请务必设置超时时间
         */
        imageAsyncScanResultsRequest.setConnectTimeout(3000);
        imageAsyncScanResultsRequest.setReadTimeout(6000);

        try {
            HttpResponse httpResponse = client.doAction(imageAsyncScanResultsRequest);

            if(httpResponse.isSuccess()){
                JSONObject scrResponse = JSON.parseObject(new String(httpResponse.getHttpContent(), "UTF-8"));
//                System.out.println(JSON.toJSONString(scrResponse, true));
                if (200 == scrResponse.getInteger("code")) {
                    JSONArray taskResults = scrResponse.getJSONArray("data");
                    for (Object taskResult : taskResults) {
                        if(200 == ((JSONObject)taskResult).getInteger("code")){
                            JSONArray sceneResults = ((JSONObject)taskResult).getJSONArray("results");
                            for (Object sceneResult : sceneResults) {
                                String scene = ((JSONObject)sceneResult).getString("scene");
                                String suggestion = ((JSONObject)sceneResult).getString("suggestion");
                                result = suggestion;
                                //根据scene和suggetion做相关的处理
                                //do something
                            }
                        }else{
                            System.out.println("task process fail:" + ((JSONObject)taskResult).getInteger("code"));
                        }
                    }
                } else {
                    System.out.println("detect not success. code:" + scrResponse.getInteger("code"));
                }
            }else{
                System.out.println("response not success. status:" + httpResponse.getStatus());
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }
        return  result;
    }

2):异步图片检测结果如下:

srcUrl:http://pic12.nipic.com/20110221/6727421_210944911000_2.jpg
{
	"msg":"OK",
	"code":200,
	"data":[
		{
			"msg":"OK",
			"code":200,
			"dataId":"a0285d1c-d423-4fd3-a9ed-3005725b88e4",
			"taskId":"img3QGf$fM4AzB4UUroFJI2GF-1r9AKR",
			"url":"http://pic12.nipic.com/20110221/6727421_210944911000_2.jpg"
		}
	],
	"requestId":"DF984416-28D6-4088-B4BE-7552481DCB4E"
}
args = [img3QGf$fM4AzB4UUroFJI2GF-1r9AKR]
taskId:img3QGf$fM4AzB4UUroFJI2GF-1r9AKR
imgCheckResult:review
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 阿里云人脸活体检测是一种基于人工智能技术的人脸识别技术,通过对用户的面部进行实时检测和分析,以验证用户的真实性和活体性。在H5示例中,我们可以使用阿里云人脸活体检测的API接口,将其嵌入到网页中,实现人脸活体检测的功能。 具体步骤如下: 1. 在阿里云开发者平台上创建自己的应用,获取对应的Access Key和Secret Key。 2. 引入阿里云人脸活体检测SDK,在html文件中添加相应的引用。 3. 在需要进行人脸活体检测的地方,添加一个用于显示摄像头画面的画布元素。 4. 编写JavaScript代码,调用阿里云人脸活体检测的API接口。可以使用ajax等方式将用户的摄像头画面上传给阿里云,同时接收阿里云返回的检测结果。 5. 根据API返回的结果,进行相应的处理,如判断用户是否为真实人脸,是否存在动作等。 6. 根据具体需求,可自定义提示语、显示结果等,例如显示“活体检测通过”或“请眨眼”等文字提示。 7. 给予用户适当的反馈,如成功通过活体检测后跳转到另一页,或在检测失败时显示错误提示信息等。 8. 最后,在应用结束时,可调用相关API接口释放资源,避免资源占用。 通过以上步骤,可以实现将阿里云人脸活体检测接入H5示例,使网页能够进行人脸活体检测功能的展示。这样可以帮助开发者更好地应用人脸活体检测技术,提升用户的安全性和用户体验。 ### 回答2: 阿里云人脸活体检测是一种通过识别用户的面部特征以确定其真实性和活跃度的技术。阿里云提供了人脸活体检测接入H5示例,以方便开发者将此功能集成到自己的网页应用中。 首先,开发者需要在阿里云人脸识别控制台创建一个人脸活体检测API实例,并获取相应的API密钥和密钥对应的API密钥ID。 其次,开发者需要在网页应用的代码中引入阿里云人脸识别的JS SDK。开发者可以通过CDN引入阿里云人脸识别的JS SDK,也可以将SDK下载到本地并引入。 接着,开发者需要在网页应用的代码中创建一个用于展示人脸活体检测结果的HTML元素,例如一个div元素。 然后,开发者需要初始化阿里云人脸识别JS SDK,并设置密钥和密钥ID。 最后,开发者需要编写代码来调用阿里云人脸活体检测API,并将检测结果展示在之前创建的HTML元素中。开发者可以通过监听用户在网页中进行的操作(例如点击按钮)来触发人脸活体检测操作,然后将用户的面部特征传递给阿里云人脸识别API进行验证。 总体来说,通过引入阿里云人脸识别的JS SDK,并按照官方文档提供的示例代码进行配置和调用,开发者可以轻松地将阿里云人脸活体检测功能接入到自己的网页应用中。这样,开发者就可以在网页上实时验证用户的真实性和活跃度,提高网站的安全性和用户体验。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值