新增接口:图片分割

This commit is contained in:
2025-04-14 13:26:56 +08:00
parent cbc760ebaf
commit 7f05bb2f7d
8 changed files with 1158 additions and 955 deletions

View File

@@ -490,6 +490,7 @@ public class RedisUtil {
return maxLikeCount != null ? Integer.parseInt(maxLikeCount) : 0;
}
public final static String IMAGE_SEGMENTATION = "ImageSegmentation:";
public final static String STRIPE_EXCEPTION_LOG = "StripeException:";

View File

@@ -18,7 +18,10 @@ import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Pattern;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
@Api(tags = "collection模块")
@@ -106,4 +109,25 @@ public class ElementController {
return Response.success();
}
@ApiOperation(value = "图片分割")
@PostMapping("/imageSegmentation")
public Response<List<CollectionElementVO>> selectedImageSeg(
@RequestPart(value = "files", required = false) MultipartFile[] files,
@RequestParam(value = "type", required = false) @Pattern(regexp = "sketch|product", message = "类型必须是sketch或product") String type,
@RequestParam(value = "id", required = false) Long id) {
// 过滤空文件
List<MultipartFile> nonEmptyFiles = Arrays.stream(files)
.filter(file -> !file.isEmpty())
.collect(Collectors.toList());
// 参数校验
if ((nonEmptyFiles.isEmpty()) && id == null) {
throw new BusinessException("必须提供文件上传或ID");
}
if (!nonEmptyFiles.isEmpty() && id != null) {
throw new BusinessException("不能同时提供文件上传和ID");
}
return Response.success(collectionElementService.selectedImageSeg(nonEmptyFiles, id, type));
}
}

View File

@@ -69,6 +69,8 @@ public class Library implements Serializable {
*/
private Integer width;
private String segmentedData;
/**
* 创建时间
*/

View File

@@ -4,6 +4,8 @@ import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.List;
@Data
@ApiModel("element-响应")
public class CollectionElementVO {
@@ -42,4 +44,6 @@ public class CollectionElementVO {
private String originalUrl;
private List<String> segmentedImages;
}

View File

@@ -21,10 +21,7 @@ import com.ai.da.python.vo.*;
import com.ai.da.service.DesignHistoryService;
import com.ai.da.service.PythonTAllInfoService;
import com.ai.da.service.SysFileService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.*;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
@@ -4188,4 +4185,58 @@ public class PythonService {
//生成失败
throw new BusinessException("brandDNAGenerate.interface.exception");
}
public List<ImageSegmentation.ImageDate> imageSegmentation(ImageSegmentation imageSegmentation) {
OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(30, TimeUnit.SECONDS)
.pingInterval(5, TimeUnit.SECONDS)//websocket轮训间隔(单位:秒)
.readTimeout(60, TimeUnit.SECONDS)//读取超时(单位:秒)
.writeTimeout(60, TimeUnit.SECONDS)//写入超时(单位:秒)
.build();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(imageSegmentation));
log.info("modifyModelProportion 请求地址: {}\n 参数:{}", accessPythonIp + ":" + accessPythonPort + "/api/clothing_seg", JSON.toJSONString(imageSegmentation));
Request request = new Request.Builder()
.url(accessPythonIp + ":" + accessPythonPort + "/api/clothing_seg")
.method("POST", body)
.addHeader("Content-Type", "application/json")
.build();
Response response = null;
try {
response = client.newCall(request).execute();
} catch (IOException ioException) {
log.error("PythonService##imageSegmentation异常###{}", ExceptionUtil.getThrowableList(ioException));
throw new BusinessException("generate.interface.error");
}
int responseCode = response.code();
String bodyString;
try {
bodyString = response.body().string();
if (responseCode != HttpURLConnection.HTTP_OK) {
// 基本不会有除200以外的code
log.info("imageSegmentation 失败。 Response code {}", responseCode);
throw new BusinessException("imageSegmentation 失败。 Response code " + responseCode);
}
JSONObject jsonObject = JSON.parseObject(bodyString);
if (response.isSuccessful() && jsonObject.get("msg").equals("OK!")) {
log.info("imageSegmentation 结果 {}", jsonObject.get("data").toString());
List<ImageSegmentation.ImageDate> seg = JSONObject.parseObject(
jsonObject.get("data").toString(),
new TypeReference<List<ImageSegmentation.ImageDate>>() {}
);
return seg;
}else {
log.info("imageSegmentation 失败。 Response code {}", responseCode);
throw new BusinessException("imageSegmentation 失败。 Response code " + responseCode);
}
} catch (IOException e) {
log.error("imageSegmentation 失败; error message => {}", e.getMessage());
response.close();
throw new BusinessException("generate.interface.error");
} finally {
response.close();
}
}
}

View File

@@ -0,0 +1,20 @@
package com.ai.da.python.vo;
import lombok.Data;
import java.util.List;
@Data
public class ImageSegmentation {
private Long user_id;
private List<ImageDate> image_data;
@Data
public class ImageDate{
public String image_url;
public String image_type;
// 作为入参时一起传入会怎样?
public List<String> clothing_url;
}
}

View File

@@ -5,6 +5,7 @@ import com.ai.da.mapper.primary.entity.LibraryModelPoint;
import com.ai.da.model.dto.*;
import com.ai.da.model.vo.*;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
@@ -136,4 +137,6 @@ public interface CollectionElementService extends IService<CollectionElement> {
*/
CollectionElement editLevel2Type(Long elementId, String level2Type, String designType);
List<CollectionElementVO> selectedImageSeg(List<MultipartFile> files, Long id, String type);
}