main
HuangHai 2 months ago
parent 754bfd8750
commit b572799d89

@ -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());
}
}

@ -76,4 +76,8 @@ GPTNB:
# 人工智能生成PPT
AIPPT:
BASE_URL: https://open.docmee.cn
API_KEY: ak_uKoKrRF63333E2lcBq
API_KEY: ak_uKoKrRF63333E2lcBq
# 阿里云API KEY
aliyun:
API_KEY: sk-01d13a39e09844038322108ecdbd1bbc

@ -76,4 +76,8 @@ GPTNB:
# 人工智能生成PPT
AIPPT:
BASE_URL: https://open.docmee.cn
API_KEY: ak_uKoKrRF63333E2lcBq
API_KEY: ak_uKoKrRF63333E2lcBq
# 阿里云API KEY
aliyun:
API_KEY: sk-01d13a39e09844038322108ecdbd1bbc
Loading…
Cancel
Save