换脸响应格式适配
This commit is contained in:
@@ -244,8 +244,6 @@ public class TryOnEffectServiceImpl extends ServiceImpl<TryOnEffectMapper, TryOn
|
||||
|
||||
|
||||
public String AITryOnEffect(String prompt, List<String> urls) {
|
||||
System.setProperty("https.proxyHost", "127.0.0.1");
|
||||
System.setProperty("https.proxyPort", "10809");
|
||||
|
||||
// log.info("开始执行AITryOnEffect - prompt: {}, url: {}", prompt, url);
|
||||
|
||||
@@ -646,34 +644,34 @@ public class TryOnEffectServiceImpl extends ServiceImpl<TryOnEffectMapper, TryOn
|
||||
|
||||
JSONObject jsonResponse = new JSONObject(response);
|
||||
|
||||
// 检查响应状态
|
||||
if (jsonResponse.containsKey("success") && jsonResponse.getBool("success")) {
|
||||
// 成功响应,获取结果图片URL
|
||||
if (jsonResponse.containsKey("result_url")) {
|
||||
String resultUrl = jsonResponse.getStr("result_url");
|
||||
log.info("换脸成功,结果URL: {}", resultUrl);
|
||||
|
||||
// 下载图片并上传到MinIO
|
||||
return downloadAndUploadToMinio(resultUrl);
|
||||
} else if (jsonResponse.containsKey("data") && jsonResponse.getJSONObject("data").containsKey("result_url")) {
|
||||
String resultUrl = jsonResponse.getJSONObject("data").getStr("result_url");
|
||||
log.info("换脸成功,结果URL: {}", resultUrl);
|
||||
|
||||
// 下载图片并上传到MinIO
|
||||
return downloadAndUploadToMinio(resultUrl);
|
||||
// 处理新的响应格式: {"output":["lanecarford/refaced_image/refaced1761809361.530157.png"]}
|
||||
if (jsonResponse.containsKey("output")) {
|
||||
Object outputObj = jsonResponse.get("output");
|
||||
|
||||
if (outputObj instanceof JSONArray) {
|
||||
JSONArray outputArray = (JSONArray) outputObj;
|
||||
if (outputArray.size() > 0) {
|
||||
String imagePath = outputArray.getString(0);
|
||||
log.info("换脸成功,图片路径: {}", imagePath);
|
||||
|
||||
// 构建完整的图片URL(假设需要添加基础URL)
|
||||
String fullImageUrl = buildFullImageUrl(imagePath);
|
||||
log.info("完整图片URL: {}", fullImageUrl);
|
||||
|
||||
// 下载图片并上传到MinIO
|
||||
return downloadAndUploadToMinio(fullImageUrl);
|
||||
} else {
|
||||
log.error("output数组为空");
|
||||
throw new BusinessException("Empty output array", "返回的图片数组为空", ResultEnum.ERROR.getCode());
|
||||
}
|
||||
} else {
|
||||
log.error("output字段不是数组格式: {}", outputObj);
|
||||
throw new BusinessException("Invalid output format", "output字段格式不正确", ResultEnum.ERROR.getCode());
|
||||
}
|
||||
}else {
|
||||
log.error("换脸API响应失败: {}", response);
|
||||
throw new BusinessException("reface error", "换脸失败", ResultEnum.ERROR.getCode());
|
||||
}
|
||||
|
||||
// 检查是否有错误信息
|
||||
if (jsonResponse.containsKey("error")) {
|
||||
String error = jsonResponse.getStr("error");
|
||||
log.error("换脸API返回错误: {}", error);
|
||||
throw new BusinessException("Face swap failed", "换脸失败: " + error, ResultEnum.ERROR.getCode());
|
||||
}
|
||||
|
||||
log.error("换脸API响应格式不正确: {}", response);
|
||||
throw new BusinessException("Invalid response format", "响应格式不正确", ResultEnum.ERROR.getCode());
|
||||
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
@@ -682,6 +680,28 @@ public class TryOnEffectServiceImpl extends ServiceImpl<TryOnEffectMapper, TryOn
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建完整的图片URL
|
||||
*/
|
||||
private String buildFullImageUrl(String imagePath) {
|
||||
// 如果imagePath已经是完整URL,直接返回
|
||||
if (imagePath.startsWith("http://") || imagePath.startsWith("https://")) {
|
||||
return imagePath;
|
||||
}
|
||||
|
||||
// 否则,使用换脸API的基础URL构建完整URL
|
||||
String baseUrl = faceSwapConfig.getBaseUrl();
|
||||
if (baseUrl.endsWith("/")) {
|
||||
baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
|
||||
}
|
||||
|
||||
if (!imagePath.startsWith("/")) {
|
||||
imagePath = "/" + imagePath;
|
||||
}
|
||||
|
||||
return baseUrl + imagePath;
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载图片并上传到MinIO
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user