模特比例修改、sketch拼贴

This commit is contained in:
2025-03-25 11:19:55 +08:00
parent 6b62cf7299
commit 078a0c0dfb
10 changed files with 371 additions and 79 deletions

View File

@@ -14,6 +14,8 @@ import com.ai.da.model.vo.*;
import com.ai.da.python.PythonService;
import com.ai.da.service.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
@@ -26,14 +28,17 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
import static com.ai.da.common.enums.CollectionLevel1TypeEnum.*;
import static com.ai.da.service.impl.UserLikeGroupServiceImpl.convert;
@Slf4j
@Service
@@ -814,7 +819,7 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
@Override
@Transactional(rollbackFor = Exception.class)
public GenerateResultVO imageToSketch(ImageToSketchDTO imageToSketchDTO) {
public GenerateResultVO imageToSketch(ImageToSketchDTO imageToSketchDTO, String collagePictureUrl, Long projectId) {
String bucket = userBucket;
Long accountId = UserContext.getUserHolder().getId();
@@ -827,8 +832,13 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
throw new BusinessException("remaining.credits.insufficient", ResultEnum.PROMPT.getCode());
}
CollectionElement collectionElement = collectionElementService.getById(imageToSketchDTO.getElementId());
String imagePath = collectionElement.getUrl();
String imagePath;
if (StringUtil.isNullOrEmpty(collagePictureUrl)){
CollectionElement collectionElement = collectionElementService.getById(imageToSketchDTO.getElementId());
imagePath = collectionElement.getUrl();
}else {
imagePath = collagePictureUrl;
}
log.info(minioUtil.getPreSignedUrl(imagePath, CommonConstant.MINIO_IMAGE_EXPIRE_TIME));
String imageName = UUID.randomUUID().toString();
@@ -856,7 +866,9 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
generate.setElementId(imageToSketchDTO.getElementId());
generate.setGenerateType("image");
generate.setSketchStyle(styleCode);
generate.setStyleImageElementId(imageToSketchDTO.getElementId());
generate.setStyleImageElementId(imageToSketchDTO.getStyleImageId());
generate.setProjectId(projectId);
generate.setInputImageUrl(collagePictureUrl);
generate.setCreateDate(new Date());
baseMapper.insert(generate);
@@ -1011,51 +1023,168 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
}
}
@Resource
private SysFileService sysFileService;
@Resource
private LibraryModelPointService libraryModelPointService;
public String modifyModelProportion(ModifyModelProportionDTO proportionDTO){
log.info("modifyModelProportion params: {}", proportionDTO);
String name;
String gender;
Library model = null;
Long accountId = UserContext.getUserHolder().getId();
String uuid = UUID.randomUUID().toString();
// 所有修改的图片都另存为,不覆盖原图
if (proportionDTO.getType().equals("Library")){
model = libraryService.getById(proportionDTO.getId());
String url = model.getUrl();
name = url.substring(url.indexOf("/") + 1, url.lastIndexOf("/")) + "/" + uuid;
gender = model.getLevel2Type();
}else {
SysFileVO sysModel = sysFileService.getById(proportionDTO.getId());
gender = sysModel.getLevel2Type();
name = accountId + "/models/" + gender.toLowerCase() + "/" + uuid;
}
// 只需要将结果存入library
String modifiedModel = pythonService.modifyModelProportion(proportionDTO.getModelPath(), proportionDTO.getStretch(), name, proportionDTO.getTop(), proportionDTO.getBottom());
List<Integer> imagesWidthAndHeight = minioUtil.getImagesWidthAndHeight(modifiedModel);
// 存储修改后的模特到个人library
model = new Library();
model.setAccountId(accountId);
model.setLevel1Type(LibraryLevel1TypeEnum.MODELS.getRealName());
model.setLevel2Type(gender);
model.setName(uuid);
model.setUrl(modifiedModel);
model.setMd5(MD5Utils.encryptFile(minioUtil.getPreSignedUrl(modifiedModel, 24 * 60),false));
model.setWidth(imagesWidthAndHeight.get(0));
model.setHigh(imagesWidthAndHeight.get(1));
model.setCreateDate(new Date());
libraryService.save(model);
// 新建模特点位信息
LibraryModelPoint libraryModelPoint = new LibraryModelPoint();
libraryModelPoint.setModelType("Library");
libraryModelPoint.setRelationId(model.getId());
libraryModelPoint.setShoulderLeft(Arrays.toString(proportionDTO.getShoulderLeft()));
libraryModelPoint.setShoulderRight(Arrays.toString(proportionDTO.getShoulderRight()));
libraryModelPoint.setWaistbandLeft(Arrays.toString(proportionDTO.getWaistbandLeft()));
libraryModelPoint.setWaistbandRight(Arrays.toString(proportionDTO.getWaistbandRight()));
libraryModelPoint.setHandLeft(Arrays.toString(proportionDTO.getHandLeft()));
libraryModelPoint.setHandRight(Arrays.toString(proportionDTO.getHandRight()));
libraryModelPoint.setCreateDate(new Date());
libraryModelPointService.save(libraryModelPoint);
return minioUtil.getPreSignedUrl(modifiedModel, CommonConstant.MINIO_IMAGE_EXPIRE_TIME);
}
/**
* String collagePicture(Base64)
* List<DTO(id, type)> elements
* File file
* @return
*/
public String sketchReconstruction(SketchReconstructionDTO sketchReconstructionDTO){
@Transactional(rollbackFor = Exception.class)
public GenerateResultVO sketchReconstructionGenerate(SketchReconstructionDTO sketchReconstructionDTO){
Long accountId = UserContext.getUserHolder().getId();
// 1、线稿生成
String collagePictureBase64 = sketchReconstructionDTO.getCollagePicture();
String path = accountId + "/CollagePicture/" + UUID.randomUUID();
String minioPath = minioUtil.base64UploadToPath(collagePictureBase64, userBucket, path);
CollectionElement collectionElement = new CollectionElement();
collectionElement.setAccountId(accountId);
collectionElement.setLevel1Type(SKETCH_BOARD.getRealName());
collectionElement.setUrl(minioPath);
collectionElement.setMd5(MD5Utils.encryptFile(minioPath, false));
collectionElement.setCreateDate(new Date());
collectionElementService.save(collectionElement);
GenerateResultVO generateResultVO = imageToSketch(new ImageToSketchDTO(collectionElement.getId(), "2", sketchReconstructionDTO.getGender()));
// 2、以文件形式保存元素同时还要将使用的元素单独存储
if (sketchReconstructionDTO.isSave() && !sketchReconstructionDTO.getElements().isEmpty()){
// 将使用的元素全部都保存到新建表
// 先判断该project下有没有数据无 --> 直接保存;有 --> 先删除,再保存
QueryWrapper<SketchReconstruction> qw = new QueryWrapper<>();
qw.eq("project_id", sketchReconstructionDTO.getProjectId());
List<SketchReconstruction> sketchReconstructions = sketchReconstructionMapper.selectList(qw);
if (!sketchReconstructions.isEmpty()){
sketchReconstructionMapper.delete(qw);
}
sketchReconstructionDTO.getElements().forEach(element -> {
SketchReconstruction sketchReconstruction = new SketchReconstruction();
sketchReconstruction.setProjectId(sketchReconstructionDTO.getProjectId());
sketchReconstruction.setElementId(element.getElementId());
sketchReconstruction.setElementSource(element.getElementSource());
sketchReconstruction.setPath(element.getPath());
sketchReconstructionMapper.insert(sketchReconstruction);
});
Long projectId = sketchReconstructionDTO.getProjectId();
// 将画布文件上传到minio,地址保存到project表中
String canvasPath = minioUtil.upload("aida-users", accountId + "/CollagePicture/CanvasFile", sketchReconstructionDTO.getFile());
GenerateResultVO generateResultVO = imageToSketch(new ImageToSketchDTO(null, "2", sketchReconstructionDTO.getGender()), minioPath, projectId);
QueryWrapper<SketchReconstruction> qw = new QueryWrapper<>();
qw.eq("project_id", projectId);
SketchReconstruction sketchReconstruction = sketchReconstructionMapper.selectOne(qw);
String url = generateResultVO.getUrl();
// 找到路径的起始位置(从"://"之后查找第一个"/"
int pathStartIndex = url.indexOf("/", url.indexOf("://") + 3);
// 找到查询参数的起始位置("?" 的位置)
int queryStartIndex = url.indexOf("?");
// 截取目标部分
String targetPath = url.substring(pathStartIndex + 1, queryStartIndex);
if (Objects.isNull(sketchReconstruction)){
sketchReconstruction = new SketchReconstruction();
sketchReconstruction.setProjectId(projectId);
sketchReconstruction.setCollageImgSketchUrl(targetPath);
sketchReconstruction.setGenerateDetailId(generateResultVO.getId());
sketchReconstruction.setCreateTime(LocalDateTime.now());
sketchReconstructionMapper.insert(sketchReconstruction);
}else {
sketchReconstruction.setCollageImgSketchUrl(targetPath);
sketchReconstruction.setGenerateDetailId(generateResultVO.getId());
sketchReconstructionMapper.updateById(sketchReconstruction);
}
return generateResultVO;
}
public String sketchReconstructionSave(MultipartFile multipartFile, Long projectId){
Long accountId = UserContext.getUserHolder().getId();
// 元素都在画布上,不用额外保存
String object = accountId + "/CollageSketchFile/" + projectId;
String canvasFilePath = minioUtil.upload("aida-users", object, multipartFile,null);
// 将画布文件上传到minio,地址保存到project表中
QueryWrapper<SketchReconstruction> qw = new QueryWrapper<>();
qw.eq("project_id", projectId).isNotNull("canvas_url").orderByDesc("id");
SketchReconstruction sketchReconstruction = sketchReconstructionMapper.selectOne(qw);
if (Objects.isNull(sketchReconstruction)){
sketchReconstruction = new SketchReconstruction();
sketchReconstruction.setProjectId(projectId);
sketchReconstruction.setCanvasUrl(canvasFilePath);
sketchReconstruction.setCreateTime(LocalDateTime.now());
sketchReconstructionMapper.insert(sketchReconstruction);
}else if (StringUtil.isNullOrEmpty(sketchReconstruction.getCanvasUrl())){
sketchReconstruction.setCanvasUrl(canvasFilePath);
sketchReconstructionMapper.updateById(sketchReconstruction);
}
// 需要返回哪些信息呢?
return null;
}
public SketchReconstructionVO getSketchReconstruction(Long projectId){
QueryWrapper<SketchReconstruction> qw = new QueryWrapper<>();
qw.eq("project_id", projectId);
SketchReconstruction sketchReconstruction = sketchReconstructionMapper.selectOne(qw);
if (Objects.isNull(sketchReconstruction) || StringUtil.isNullOrEmpty(sketchReconstruction.getCanvasUrl())){
return null;
}
try {
InputStream download = minioUtil.download(sketchReconstruction.getCanvasUrl());
String convert = convert(download);
JSONObject jsonObject = JSONObject.parseObject(convert);
JSONArray objects = jsonObject.getJSONArray("objects");
for (int i = 0; i < objects.size(); i++) {
JSONObject jsonObject1 = objects.getJSONObject(i);
String type = jsonObject1.getString("type");
if (type.equals("image")) {
String minioUrl = jsonObject1.getString("minioUrl");
jsonObject1.put("src", minioUtil.getPreSignedUrl(minioUrl, 24 * 60));
}
objects.set(i, jsonObject1);
}
jsonObject.put("objects", objects);
log.info(String.valueOf(jsonObject));
// 除返回jsonObject之外,还要返回最后一次生成的线稿图以及like状态
SketchReconstructionVO vo = new SketchReconstructionVO();
vo.setCanvasFile(jsonObject);
if (Objects.nonNull(sketchReconstruction.getGenerateDetailId())){
GenerateDetail generateDetail = generateDetailMapper.selectById(sketchReconstruction.getGenerateDetailId());
vo.setCollageSketchUrl(minioUtil.getPreSignedUrl(generateDetail.getUrl(), CommonConstant.MINIO_IMAGE_EXPIRE_TIME));
vo.setLiked(generateDetail.getIsLike().equals((byte)1));
}
return vo;
}catch (Exception e){
return null;
}
}
}