|
|
|
@ -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,45 +15,16 @@ 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
|
|
|
|
@ -68,26 +39,15 @@ public class ImageToImageUltra {
|
|
|
|
|
* @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());
|
|
|
|
@ -188,11 +107,9 @@ public class ImageToImageUltra {
|
|
|
|
|
log.error(errorMsg);
|
|
|
|
|
throw new IOException(errorMsg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取生成任务UUID
|
|
|
|
|
String generateUuid = responseJson.getJSONObject("data").getString("generateUuid");
|
|
|
|
|
log.info("图生图任务已提交,任务UUID: {}", generateUuid);
|
|
|
|
|
|
|
|
|
|
return generateUuid;
|
|
|
|
|
}
|
|
|
|
|
|