修改 imageToSketch

This commit is contained in:
2024-09-23 11:13:44 +08:00
parent 93e9c61943
commit 5a86f2e649
12 changed files with 130 additions and 66 deletions

View File

@@ -5,6 +5,7 @@ import com.ai.da.mapper.primary.entity.GenerateDetail;
import com.ai.da.model.dto.GenerateLikeDTO;
import com.ai.da.model.dto.GenerateModifyDTO;
import com.ai.da.model.dto.GenerateThroughImageTextDTO;
import com.ai.da.model.dto.ImageToSketchDTO;
import com.ai.da.model.vo.*;
import com.baomidou.mybatisplus.extension.service.IService;
@@ -43,7 +44,7 @@ public interface GenerateService extends IService<Generate> {
List<Map<String, Object>> getCountByUserAndTime(String startTime, String endTime, List<Long> accountIdList);
String imageToSketch(GenerateThroughImageTextDTO generateThroughImageTextDTO);
GenerateResultVO imageToSketch(ImageToSketchDTO imageToSketchDTO);
void modifySketch(GenerateModifyDTO generateModifyDTO);
CollectionElementVO modifySketch(GenerateModifyDTO generateModifyDTO);
}

View File

@@ -26,6 +26,4 @@ public interface UserLikeService extends IService<UserLike> {
void updateDate(Long designItemId,String timeZone);
List<UserLike> getUserLikeList(Long id);
List<Map<String, Long>> getHistoryLikeWithGradient();
}

View File

@@ -7,10 +7,8 @@ import com.ai.da.common.enums.*;
import com.ai.da.common.utils.*;
import com.ai.da.mapper.primary.*;
import com.ai.da.mapper.primary.entity.*;
import com.ai.da.model.dto.GenerateLikeDTO;
import com.ai.da.model.dto.GenerateModifyDTO;
import com.ai.da.model.dto.GenerateThroughImageTextDTO;
import com.ai.da.model.dto.GenerateToPythonDTO;
import com.ai.da.model.dto.*;
import com.ai.da.model.enums.SketchStyle;
import com.ai.da.model.vo.*;
import com.ai.da.python.PythonService;
import com.ai.da.service.*;
@@ -773,25 +771,28 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
@Override
@Transactional(rollbackFor = Exception.class)
public String imageToSketch(GenerateThroughImageTextDTO generateThroughImageTextDTO) {
public GenerateResultVO imageToSketch(ImageToSketchDTO imageToSketchDTO) {
String bucket = userBucket;
Long accountId = UserContext.getUserHolder().getId();
String imagePath;
if (generateThroughImageTextDTO.getDesignType().equals("collection")) {
CollectionElement collectionElement = collectionElementService.getById(generateThroughImageTextDTO.getCollectionElementId());
imagePath = collectionElement.getUrl();
} else if (generateThroughImageTextDTO.getDesignType().equals("productImage")) {
ToProductImageResult productImage = toProductImageResultMapper.selectById(generateThroughImageTextDTO.getCollectionElementId());
imagePath = productImage.getUrl();
} else {
log.error("unknown designType");
throw new BusinessException("unknown designType");
}
CollectionElement collectionElement = collectionElementService.getById(imageToSketchDTO.getElementId());
String imagePath = collectionElement.getUrl();
log.info(minioUtil.getPreSignedUrl(imagePath, CommonConstant.MINIO_IMAGE_EXPIRE_TIME));
String imageName = imagePath.substring(imagePath.lastIndexOf("/") + 1);
String objectName = accountId + "/imageToSketch/" + imageName;
String sketchPath = pythonService.imageToSketch(imagePath, bucket, objectName);
String style = imageToSketchDTO.getStyle();
String styleCode = style.equals(SketchStyle.THICK.getValue()) ? "1" :
style.equals(SketchStyle.MEDIUM.getValue()) ? "2" :
style.equals(SketchStyle.THIN.getValue()) ? "3" : "Custom";
String styleImage;
if (!Objects.isNull(imageToSketchDTO.getStyleImageId())){
CollectionElement styleElement = collectionElementService.getById(imageToSketchDTO.getElementId());
styleImage = styleElement.getUrl();
} else {
styleImage = null;
}
String sketchPath = pythonService.imageToSketch(imagePath, bucket, objectName, styleCode, styleImage);
// 存DB
Generate generate = new Generate();
@@ -799,21 +800,33 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
generate.setUniqueId(String.valueOf(0));
generate.setLevel1Type(SKETCH_BOARD.getRealName());
generate.setLevel2Type("ImageToSketch");
generate.setElementSource(generateThroughImageTextDTO.getDesignType());
generate.setElementId(generateThroughImageTextDTO.getCollectionElementId());
generate.setElementSource("collection");
generate.setElementId(imageToSketchDTO.getElementId());
generate.setGenerateType("image");
generate.setSketchStyle(styleCode);
generate.setStyleImageElementId(imageToSketchDTO.getElementId());
generate.setCreateDate(new Date());
baseMapper.insert(generate);
// 返回
return minioUtil.getPreSignedUrl(sketchPath, CommonConstant.MINIO_IMAGE_EXPIRE_TIME);
// 将生成结果存入DB
GenerateDetail generateDetail = new GenerateDetail();
generateDetail.setGenerateId(generate.getId());
generateDetail.setUrl(sketchPath);
generateDetail.setIsLike((byte)0);
generateDetail.setMd5(MD5Utils.encryptFile(minioUtil.getPreSignedUrl(sketchPath, 24 * 60), Boolean.FALSE));
generateDetail.setCreateDate(LocalDateTime.now());
generateDetailMapper.insert(generateDetail);
String clothCategory = pythonService.getClothCategory(sketchPath, imageToSketchDTO.getGender());
return new GenerateResultVO(generateDetail.getId(), minioUtil.getPreSignedUrl(sketchPath, CommonConstant.MINIO_IMAGE_EXPIRE_TIME), "Success", clothCategory);
}
// 对提取出来的sketch做调整
// 输入 base64以及 性别 分类将图片添加到library
@Override
@Transactional(rollbackFor = Exception.class)
public void modifySketch(GenerateModifyDTO generateModifyDTO) {
public CollectionElementVO modifySketch(GenerateModifyDTO generateModifyDTO) {
log.info("修改提取出的sketch,并加入到library");
Long accountId = UserContext.getUserHolder().getId();
String base64 = generateModifyDTO.getBase64();
@@ -826,17 +839,23 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
log.info("修改后的图片 {}", minioPath);
// 存入db
Library library = new Library();
library.setAccountId(accountId);
library.setLevel1Type(SKETCH_BOARD.getRealName());
library.setLevel2Type(category);
library.setLevel3Type(gender);
library.setName(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")));
library.setUrl(minioPath);
library.setMd5(MD5Utils.encryptFile(minioUtil.getPreSignedUrl(minioPath, 24 * 60), Boolean.FALSE));
library.setCreateDate(new Date());
// 存入db 保存到t_collection_element
CollectionElement collectionElement = new CollectionElement();
collectionElement.setAccountId(accountId);
collectionElement.setCollectionId(0L);
collectionElement.setLevel1Type(SKETCH_BOARD.getRealName());
collectionElement.setLevel2Type(generateModifyDTO.getCategory());
collectionElement.setName(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")));
collectionElement.setUrl(minioPath);
collectionElement.setHasPin((byte)0);
collectionElement.setMd5(MD5Utils.encryptFile(minioUtil.getPreSignedUrl(minioPath, CommonConstant.MINIO_IMAGE_EXPIRE_TIME), Boolean.FALSE));
collectionElement.setCreateDate(new Date());
collectionElementService.save(collectionElement);
libraryService.save(library);
CollectionElementVO collectionElementVO = CopyUtil.copyObject(collectionElement, CollectionElementVO.class);
collectionElementVO.setMinIOPath(collectionElementVO.getUrl());
collectionElementVO.setUrl(minioUtil.getPreSignedUrl(collectionElementVO.getUrl(), CommonConstant.MINIO_IMAGE_EXPIRE_TIME));
collectionElementVO.setDesignType(DesignTypeEnum.COLLECTION.getRealName());
return collectionElementVO;
}
}

View File

@@ -94,21 +94,4 @@ public class UserLikeServiceImpl extends ServiceImpl<UserLikeMapper, UserLike> i
return userLikeMapper.selectList(qw);
}
@Override
public List<Map<String, Long>> getHistoryLikeWithGradient(){
List<Map<String, Long>> historyLikeWithGradient = baseMapper.getHistoryLikeWithGradient();
return historyLikeWithGradient.stream()
.map(map -> {
Map<String, Long> newMap = new HashMap<>();
if (map.containsKey("design_item_id")) {
newMap.put("designItemId", map.get("design_item_id"));
}
if (map.containsKey("design_outfit_id")) {
newMap.put("designPythonOutfitId", map.get("design_outfit_id"));
}
return newMap;
})
.collect(Collectors.toList());
}
}