diff --git a/dsAiSupport/src/main/java/com/dsideal/aiSupport/Util/DashScope/DsFaceDetect.java b/dsAiSupport/src/main/java/com/dsideal/aiSupport/Util/DashScope/DsFaceDetect.java new file mode 100644 index 00000000..541d4aad --- /dev/null +++ b/dsAiSupport/src/main/java/com/dsideal/aiSupport/Util/DashScope/DsFaceDetect.java @@ -0,0 +1,109 @@ +package com.dsideal.aiSupport.Util.DashScope; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.dsideal.aiSupport.Plugin.YamlProp; +import com.jfinal.kit.Prop; +import lombok.SneakyThrows; +import okhttp3.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.concurrent.TimeUnit; + +import static com.dsideal.aiSupport.AiSupportApplication.getEnvPrefix; + +/** + * 阿里云达摩院人脸检测API工具类 + */ +public class DsFaceDetect { + private static final Logger log = LoggerFactory.getLogger(DsFaceDetect.class); + private static final String API_URL = "https://dashscope.aliyuncs.com/api/v1/services/aigc/image2video/face-detect"; + private static final String API_KEY; + + public static Prop PropKit; // 配置文件工具 + + static { + //加载配置文件 + String configFile = "application_{?}.yaml".replace("{?}", getEnvPrefix()); + PropKit = new YamlProp(configFile); + API_KEY = PropKit.get("aliyun.API_KEY"); + } + + /** + * 调用人脸检测API + * + * @param imageUrl 图片URL + * @param ratio 比例,例如"1:1" + * @return 检测结果JSON对象 + * @throws Exception 异常信息 + */ + @SneakyThrows + public static JSONObject detectFace(String imageUrl, String ratio) { + // 创建OkHttpClient,设置超时时间 + OkHttpClient client = new OkHttpClient().newBuilder() + .connectTimeout(30, TimeUnit.SECONDS) + .readTimeout(30, TimeUnit.SECONDS) + .writeTimeout(30, TimeUnit.SECONDS) + .build(); + + // 构建请求体 + JSONObject requestBody = new JSONObject(); + requestBody.put("model", "emo-detect-v1"); + + // 设置输入参数 + JSONObject input = new JSONObject(); + input.put("image_url", imageUrl); + requestBody.put("input", input); + + // 设置其他参数 + JSONObject parameters = new JSONObject(); + parameters.put("ratio", ratio); + requestBody.put("parameters", parameters); + + // 创建请求 + MediaType mediaType = MediaType.parse("application/json"); + RequestBody body = RequestBody.create(mediaType, requestBody.toJSONString()); + Request request = new Request.Builder() + .url(API_URL) + .method("POST", body) + .addHeader("Content-Type", "application/json") + .addHeader("Authorization", "Bearer " + API_KEY) + .build(); + + // 发送请求并获取响应 + log.info("发送人脸检测请求: {}", requestBody.toJSONString()); + Response response = client.newCall(request).execute(); + + // 检查响应状态 + if (!response.isSuccessful()) { + String errorMsg = "人脸检测API请求失败,状态码: " + response.code(); + log.error(errorMsg); + throw new Exception(errorMsg); + } + + // 解析响应 + String responseBody = response.body().string(); + log.info("人脸检测响应: {}", responseBody); + + return JSON.parseObject(responseBody); + } + + /** + * 使用示例 + */ + @SneakyThrows + public static void main(String[] args) { + //EMO-detect模型,用于确认输入的人物肖像图片是否符合EMO视频生成模型的输入规范。本文档介绍了该模型提供的图像检测能力的API调用方法。 + // 图片URL + String imageUrl = "https://dsideal.obs.cn-north-1.myhuaweicloud.com/HuangHai/BlogImages/202505131058596.png"; + // 比例 + String ratio = "1:1"; + + // 调用人脸检测API + JSONObject result = detectFace(imageUrl, ratio); + + // 处理结果 + System.out.println("检测结果: " + result.toJSONString()); + } +} \ No newline at end of file diff --git a/dsAiSupport/target/classes/application_dev.yaml b/dsAiSupport/target/classes/application_dev.yaml index 2a53c16a..139241e7 100644 --- a/dsAiSupport/target/classes/application_dev.yaml +++ b/dsAiSupport/target/classes/application_dev.yaml @@ -76,4 +76,8 @@ GPTNB: # 人工智能生成PPT AIPPT: BASE_URL: https://open.docmee.cn - API_KEY: ak_uKoKrRF63333E2lcBq \ No newline at end of file + API_KEY: ak_uKoKrRF63333E2lcBq + +# 阿里云API KEY +aliyun: + API_KEY: sk-01d13a39e09844038322108ecdbd1bbc diff --git a/dsAiSupport/target/classes/application_pro.yaml b/dsAiSupport/target/classes/application_pro.yaml index cfdc9ea1..feb800d7 100644 --- a/dsAiSupport/target/classes/application_pro.yaml +++ b/dsAiSupport/target/classes/application_pro.yaml @@ -76,4 +76,8 @@ GPTNB: # 人工智能生成PPT AIPPT: BASE_URL: https://open.docmee.cn - API_KEY: ak_uKoKrRF63333E2lcBq \ No newline at end of file + API_KEY: ak_uKoKrRF63333E2lcBq + +# 阿里云API KEY +aliyun: + API_KEY: sk-01d13a39e09844038322108ecdbd1bbc \ No newline at end of file