|
@@ -0,0 +1,173 @@
|
|
|
+package shop.alien.util.common.safe;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.aliyun.green20220302.Client;
|
|
|
+import com.aliyun.green20220302.models.ImageBatchModerationResponse;
|
|
|
+import com.aliyun.green20220302.models.ImageModerationRequest;
|
|
|
+import com.aliyun.green20220302.models.ImageModerationResponse;
|
|
|
+import com.aliyun.green20220302.models.ImageBatchModerationRequest;
|
|
|
+import com.aliyun.green20220302.models.ImageBatchModerationResponse;
|
|
|
+import com.aliyun.green20220302.models.ImageBatchModerationResponseBody;
|
|
|
+import com.aliyun.green20220302.models.ImageModerationResponseBody;
|
|
|
+import com.aliyun.green20220302.models.ImageModerationResponseBody.ImageModerationResponseBodyData;
|
|
|
+import com.aliyun.green20220302.models.ImageModerationResponseBody.ImageModerationResponseBodyDataResult;
|
|
|
+import com.aliyun.teaopenapi.models.Config;
|
|
|
+import com.aliyun.teautil.models.RuntimeOptions;
|
|
|
+import jdk.nashorn.internal.runtime.logging.Logger;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.UUID;
|
|
|
+
|
|
|
+@Logger
|
|
|
+public class ImageModerationUtil {
|
|
|
+
|
|
|
+ @Value("${ali.yundun.accessKeyID}")
|
|
|
+ private String accessKeyId;
|
|
|
+
|
|
|
+ @Value("${ali.yundun.secret}")
|
|
|
+ private String accessKeySecret;
|
|
|
+
|
|
|
+ @Value("${ali.yundun.imgEndpoint}")
|
|
|
+ private String ImgEndpoint;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建请求客户端
|
|
|
+ * @return Client
|
|
|
+ * @throws Exception 创建客户端异常
|
|
|
+ */
|
|
|
+ public Client createClient() throws Exception {
|
|
|
+ Config config = new Config();
|
|
|
+ config.setAccessKeyId(accessKeyId);
|
|
|
+ config.setAccessKeySecret(accessKeySecret);
|
|
|
+ // 设置http代理。
|
|
|
+ //config.setHttpProxy("http://10.10.xx.xx:xxxx");
|
|
|
+ // 设置https代理。
|
|
|
+ //config.setHttpsProxy("https://10.10.xx.xx:xxxx");
|
|
|
+ // 接入区域和地址请根据实际情况修改
|
|
|
+ // 接入地址列表:https://help.aliyun.com/document_detail/467828.html?#section-uib-qkw-0c8
|
|
|
+ config.setEndpoint(ImgEndpoint);
|
|
|
+ return new Client(config);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 提供给service调用的方法
|
|
|
+ *
|
|
|
+ * @param imageUrl 图片地址
|
|
|
+ * @param serviceEnum Service类型
|
|
|
+ * @return 检测结果
|
|
|
+ * @throws Exception 检测异常
|
|
|
+ */
|
|
|
+ public ImageModerationResponse invokeFunction(String imageUrl,String serviceEnum) throws Exception {
|
|
|
+ //注意,此处实例化的client请尽可能重复使用,避免重复建立连接,提升检测性能。
|
|
|
+ Client client = createClient();
|
|
|
+
|
|
|
+ // 创建RuntimeObject实例并设置运行参数
|
|
|
+ RuntimeOptions runtime = new RuntimeOptions();
|
|
|
+
|
|
|
+ // 检测参数构造。
|
|
|
+ Map<String, String> serviceParameters = new HashMap<>();
|
|
|
+ //公网可访问的URL。
|
|
|
+ serviceParameters.put("imageUrl", imageUrl);
|
|
|
+ //待检测数据唯一标识
|
|
|
+ serviceParameters.put("dataId", UUID.randomUUID().toString());
|
|
|
+
|
|
|
+ ImageModerationRequest request = new ImageModerationRequest();
|
|
|
+ // 图片检测service:内容安全控制台图片增强版规则配置的serviceCode,示例:baselineCheck
|
|
|
+ // 支持service请参考:https://help.aliyun.com/document_detail/467826.html?0#p-23b-o19-gff
|
|
|
+ request.setService(serviceEnum);
|
|
|
+ request.setServiceParameters(JSON.toJSONString(serviceParameters));
|
|
|
+
|
|
|
+ ImageModerationResponse response = null;
|
|
|
+ try {
|
|
|
+ response = client.imageModerationWithOptions(request, runtime);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new Exception("调用服务失败" + e.getMessage());
|
|
|
+ }
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量图片检测
|
|
|
+ *
|
|
|
+ * @param imageUrl 图片地址
|
|
|
+ * @param serviceEnum Service类型 服务名逗号拼接
|
|
|
+ * @return 检测结果
|
|
|
+ * @throws Exception 检测异常
|
|
|
+ */
|
|
|
+ public ImageBatchModerationResponse invokeBeachFunction(String imageUrl,String serviceEnum) throws Exception {
|
|
|
+ Client client = createClient();
|
|
|
+ RuntimeOptions runtime = new RuntimeOptions();
|
|
|
+ Map<String, String> serviceParameters = new HashMap<>();
|
|
|
+ serviceParameters.put("imageUrl", imageUrl);
|
|
|
+ serviceParameters.put("dataId", UUID.randomUUID().toString());
|
|
|
+ ImageBatchModerationRequest imageBatchModerationRequest = new ImageBatchModerationRequest();
|
|
|
+ imageBatchModerationRequest.setService(serviceEnum);
|
|
|
+ imageBatchModerationRequest.setServiceParameters(JSON.toJSONString(serviceParameters));
|
|
|
+ ImageBatchModerationResponse response = null;
|
|
|
+ try {
|
|
|
+ response = client.imageBatchModerationWithOptions(imageBatchModerationRequest, runtime);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new Exception("调用服务失败" + e.getMessage());
|
|
|
+ }
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * 发布商品场景检测
|
|
|
+ * 顺序调用内容治理检测、AIGC图片风险检测、图片万物识别
|
|
|
+ *
|
|
|
+ * @param imageUrl 图片地址
|
|
|
+ * @return 检测结果
|
|
|
+ * @throws Exception 检测异常
|
|
|
+ */
|
|
|
+ public ImageBatchModerationResponse productPublishCheck(String imageUrl) throws Exception {
|
|
|
+
|
|
|
+ // 批量服务key
|
|
|
+ String serviceEnum = ImageReviewServiceEnum.TONALITY_IMPROVE.getService() + "," + ImageReviewServiceEnum.AIGC_CHECK.getService() + "," + ImageReviewServiceEnum.GENERAL_RECOGNITION.getService();
|
|
|
+
|
|
|
+ // 批量图片检测
|
|
|
+ ImageBatchModerationResponse response = invokeBeachFunction(imageUrl,serviceEnum);
|
|
|
+ if (response != null) {
|
|
|
+ if (response.getStatusCode() == 200) {
|
|
|
+ ImageBatchModerationResponseBody body = response.getBody();
|
|
|
+ System.out.println("requestId=" + body.getRequestId());
|
|
|
+ System.out.println("code=" + body.getCode());
|
|
|
+ System.out.println("msg=" + body.getMsg());
|
|
|
+ if (body.getCode() == 200) {
|
|
|
+ ImageBatchModerationResponseBody.ImageBatchModerationResponseBodyData data = body.getData();
|
|
|
+ System.out.println("dataId=" + data.getDataId());
|
|
|
+ List<ImageBatchModerationResponseBody.ImageBatchModerationResponseBodyDataResult> results = data.getResult();
|
|
|
+ for (ImageBatchModerationResponseBody.ImageBatchModerationResponseBodyDataResult result : results) {
|
|
|
+ System.out.println("label=" + result.getLabel());
|
|
|
+ System.out.println("confidence=" + result.getConfidence());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ System.out.println("image moderation not success. code:" + body.getCode());
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ System.out.println("response not success. status:" + response.getStatusCode());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 聊一聊场景检测
|
|
|
+ * 顺序调用图片万物识别、内容治理检测
|
|
|
+ *
|
|
|
+ * @param imageUrl 图片地址
|
|
|
+ * @return 检测结果
|
|
|
+ * @throws Exception 检测异常
|
|
|
+ */
|
|
|
+ public ImageBatchModerationResponse chatCheck(String imageUrl) throws Exception {
|
|
|
+ // 批量服务key
|
|
|
+ String serviceEnum = ImageReviewServiceEnum.GENERAL_RECOGNITION.getService() + "," + ImageReviewServiceEnum.TONALITY_IMPROVE.getService();
|
|
|
+
|
|
|
+ // 批量图片检测
|
|
|
+ ImageBatchModerationResponse response = invokeBeachFunction(imageUrl,serviceEnum);
|
|
|
+
|
|
|
+ return response;
|
|
|
+ }
|
|
|
+}
|