|
|
|
@ -4,7 +4,7 @@ import com.alibaba.fastjson.JSON;
|
|
|
|
|
import com.alibaba.fastjson.JSONArray;
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.dsideal.aiSupport.Plugin.YamlProp;
|
|
|
|
|
import com.dsideal.aiSupport.Util.Liblib.Kit.SignUtil;
|
|
|
|
|
import com.dsideal.aiSupport.Util.Liblib.Kit.LibLibUtil;
|
|
|
|
|
import com.jfinal.kit.Prop;
|
|
|
|
|
import okhttp3.*;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
@ -15,79 +15,39 @@ import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
import static com.dsideal.aiSupport.AiSupportApplication.getEnvPrefix;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* LibLib 图生图API工具类
|
|
|
|
|
* 使用ControlNet技术将一个源图像作为参考来生成新图像
|
|
|
|
|
*/
|
|
|
|
|
public class ImageToImageUltra {
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(ImageToImageUltra.class);
|
|
|
|
|
|
|
|
|
|
// API基础URL
|
|
|
|
|
private static final String API_BASE_URL = "https://openapi.liblibai.cloud";
|
|
|
|
|
public class UltraImg2Img extends LibLibUtil {
|
|
|
|
|
//日志
|
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(UltraImg2Img.class);
|
|
|
|
|
// 图生图API路径
|
|
|
|
|
private static final String IMG_TO_IMG_PATH = "/api/generate/webui/img2img/ultra";
|
|
|
|
|
|
|
|
|
|
// API访问凭证
|
|
|
|
|
protected static String accessKey;
|
|
|
|
|
protected static String secretKey;
|
|
|
|
|
public static Prop PropKit;
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
//加载配置文件
|
|
|
|
|
String configFile = "application_{?}.yaml".replace("{?}", getEnvPrefix());
|
|
|
|
|
PropKit = new YamlProp(configFile);
|
|
|
|
|
accessKey = PropKit.get("LIBLIB.accessKey");
|
|
|
|
|
secretKey = PropKit.get("LIBLIB.secretKey");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建OkHttpClient实例
|
|
|
|
|
* @return OkHttpClient实例
|
|
|
|
|
*/
|
|
|
|
|
private static OkHttpClient createHttpClient() {
|
|
|
|
|
return new OkHttpClient.Builder()
|
|
|
|
|
.connectTimeout(30, TimeUnit.SECONDS)
|
|
|
|
|
.readTimeout(60, TimeUnit.SECONDS)
|
|
|
|
|
.writeTimeout(60, TimeUnit.SECONDS)
|
|
|
|
|
.build();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 提交图生图任务
|
|
|
|
|
* @param templateUuid 模板UUID
|
|
|
|
|
* @param prompt 提示词
|
|
|
|
|
* @param sourceImageUrl 源图片URL
|
|
|
|
|
* @param width 宽度
|
|
|
|
|
* @param height 高度
|
|
|
|
|
* @param steps 步数
|
|
|
|
|
* @param cfgScale 提示词引导系数
|
|
|
|
|
* @param seed 随机种子,-1表示随机
|
|
|
|
|
*
|
|
|
|
|
* @param templateUuid 模板UUID
|
|
|
|
|
* @param prompt 提示词
|
|
|
|
|
* @param sourceImageUrl 源图片URL
|
|
|
|
|
* @param width 宽度
|
|
|
|
|
* @param height 高度
|
|
|
|
|
* @param steps 步数
|
|
|
|
|
* @param cfgScale 提示词引导系数
|
|
|
|
|
* @param seed 随机种子,-1表示随机
|
|
|
|
|
* @param controlNetModel ControlNet模型ID
|
|
|
|
|
* @param controlWeight 控制权重
|
|
|
|
|
* @param controlWeight 控制权重
|
|
|
|
|
* @return 生成任务UUID
|
|
|
|
|
* @throws IOException 异常信息
|
|
|
|
|
*/
|
|
|
|
|
public static String submitImageToImageTask(
|
|
|
|
|
String templateUuid,
|
|
|
|
|
String prompt,
|
|
|
|
|
String sourceImageUrl,
|
|
|
|
|
int width,
|
|
|
|
|
int height,
|
|
|
|
|
int steps,
|
|
|
|
|
double cfgScale,
|
|
|
|
|
long seed,
|
|
|
|
|
String controlNetModel,
|
|
|
|
|
String templateUuid, String prompt, String sourceImageUrl, int width,
|
|
|
|
|
int height, int steps, double cfgScale, long seed, String controlNetModel,
|
|
|
|
|
double controlWeight) throws IOException {
|
|
|
|
|
|
|
|
|
|
// 创建OkHttpClient
|
|
|
|
|
OkHttpClient client = createHttpClient();
|
|
|
|
|
|
|
|
|
|
// 构建请求体
|
|
|
|
|
JSONObject requestBody = new JSONObject();
|
|
|
|
|
|
|
|
|
|
// 添加模板UUID
|
|
|
|
|
requestBody.put("templateUuid", templateUuid);
|
|
|
|
|
|
|
|
|
|
// 构建生成参数
|
|
|
|
|
JSONObject generateParams = new JSONObject();
|
|
|
|
|
generateParams.put("prompt", prompt);
|
|
|
|
@ -102,59 +62,18 @@ public class ImageToImageUltra {
|
|
|
|
|
generateParams.put("steps", steps);
|
|
|
|
|
generateParams.put("restoreFaces", 0);
|
|
|
|
|
generateParams.put("sourceImage", sourceImageUrl);
|
|
|
|
|
|
|
|
|
|
// 构建ControlNet参数
|
|
|
|
|
JSONArray controlNetArray = new JSONArray();
|
|
|
|
|
JSONObject controlNet = new JSONObject();
|
|
|
|
|
controlNet.put("unitOrder", 0);
|
|
|
|
|
/*
|
|
|
|
|
1. controlType:
|
|
|
|
|
1. line:线稿轮廓
|
|
|
|
|
2. depth:空间关系
|
|
|
|
|
3. pose:人物姿态
|
|
|
|
|
4. IPAdapter:风格迁移
|
|
|
|
|
2. controlImage:参考图可公网访问的完整URL
|
|
|
|
|
*/
|
|
|
|
|
controlNet.put("controlImage", sourceImageUrl);
|
|
|
|
|
controlNet.put("width", width);
|
|
|
|
|
controlNet.put("height", height);
|
|
|
|
|
controlNet.put("preprocessor", 68);
|
|
|
|
|
|
|
|
|
|
// 构建注释参数
|
|
|
|
|
JSONObject annotationParameters = new JSONObject();
|
|
|
|
|
annotationParameters.put("entityControl", new JSONObject());
|
|
|
|
|
controlNet.put("annotationParameters", annotationParameters);
|
|
|
|
|
|
|
|
|
|
// 设置ControlNet模型和权重
|
|
|
|
|
controlNet.put("model", controlNetModel);
|
|
|
|
|
controlNet.put("controlWeight", controlWeight);
|
|
|
|
|
controlNet.put("startingControlStep", 0);
|
|
|
|
|
controlNet.put("endingControlStep", 1);
|
|
|
|
|
controlNet.put("pixelPerfect", 1);
|
|
|
|
|
controlNet.put("controlMode", 0);
|
|
|
|
|
controlNet.put("resizeMode", 1);
|
|
|
|
|
|
|
|
|
|
// 添加ControlNet到数组
|
|
|
|
|
// 暂不加载ControlNet
|
|
|
|
|
//controlNetArray.add(controlNet);
|
|
|
|
|
//generateParams.put("controlNet", controlNetArray);
|
|
|
|
|
|
|
|
|
|
// 将生成参数添加到请求体
|
|
|
|
|
requestBody.put("generateParams", generateParams);
|
|
|
|
|
|
|
|
|
|
// 获取API路径
|
|
|
|
|
String uri = IMG_TO_IMG_PATH;
|
|
|
|
|
|
|
|
|
|
// 生成签名信息
|
|
|
|
|
SignUtil.SignatureInfo signInfo = SignUtil.makeSignInfo(uri);
|
|
|
|
|
|
|
|
|
|
LibLibUtil.SignatureInfo signInfo = LibLibUtil.sign(uri);
|
|
|
|
|
// 构建带签名的URL
|
|
|
|
|
HttpUrl.Builder urlBuilder = HttpUrl.parse(API_BASE_URL + uri).newBuilder()
|
|
|
|
|
.addQueryParameter("AccessKey", accessKey)
|
|
|
|
|
.addQueryParameter("Signature", signInfo.getSignature())
|
|
|
|
|
.addQueryParameter("Timestamp", String.valueOf(signInfo.getTimestamp()))
|
|
|
|
|
.addQueryParameter("SignatureNonce", signInfo.getSignatureNonce());
|
|
|
|
|
|
|
|
|
|
// 创建请求
|
|
|
|
|
MediaType mediaType = MediaType.parse("application/json");
|
|
|
|
|
RequestBody body = RequestBody.create(mediaType, requestBody.toJSONString());
|
|
|
|
@ -163,36 +82,34 @@ public class ImageToImageUltra {
|
|
|
|
|
.method("POST", body)
|
|
|
|
|
.addHeader("Content-Type", "application/json")
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 执行请求
|
|
|
|
|
log.info("提交图生图任务: {}", requestBody.toJSONString());
|
|
|
|
|
log.info("请求URL: {}", urlBuilder.build());
|
|
|
|
|
Response response = client.newCall(request).execute();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 处理响应
|
|
|
|
|
if (!response.isSuccessful()) {
|
|
|
|
|
String errorMsg = "图生图任务提交失败,状态码: " + response.code();
|
|
|
|
|
log.error(errorMsg);
|
|
|
|
|
throw new IOException(errorMsg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 解析响应
|
|
|
|
|
String responseBody = response.body().string();
|
|
|
|
|
log.info("图生图任务提交响应: {}", responseBody);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JSONObject responseJson = JSON.parseObject(responseBody);
|
|
|
|
|
int code = responseJson.getIntValue("code");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (code != 0) {
|
|
|
|
|
String errorMsg = "图生图任务提交失败,错误码: " + code + ", 错误信息: " + responseJson.getString("msg");
|
|
|
|
|
log.error(errorMsg);
|
|
|
|
|
throw new IOException(errorMsg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取生成任务UUID
|
|
|
|
|
String generateUuid = responseJson.getJSONObject("data").getString("generateUuid");
|
|
|
|
|
log.info("图生图任务已提交,任务UUID: {}", generateUuid);
|
|
|
|
|
|
|
|
|
|
return generateUuid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -220,7 +137,7 @@ public class ImageToImageUltra {
|
|
|
|
|
String controlNetModel = "6f1767b5f9eb47289525d06ae882a0e5";
|
|
|
|
|
// 控制权重
|
|
|
|
|
double controlWeight = 0.9;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 提交图生图任务
|
|
|
|
|
String generateUuid = submitImageToImageTask(
|
|
|
|
|
templateUuid,
|
|
|
|
@ -234,11 +151,11 @@ public class ImageToImageUltra {
|
|
|
|
|
controlNetModel,
|
|
|
|
|
controlWeight
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 输出生成任务UUID
|
|
|
|
|
log.info("图生图任务已提交,任务UUID: {}", generateUuid);
|
|
|
|
|
log.info("请使用此UUID查询任务进度和结果");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
log.error("图生图任务执行失败", e);
|
|
|
|
|
}
|