修改 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

@@ -4,6 +4,7 @@ import com.ai.da.common.response.Response;
import com.ai.da.model.dto.GenerateLikeDTO; import com.ai.da.model.dto.GenerateLikeDTO;
import com.ai.da.model.dto.GenerateModifyDTO; import com.ai.da.model.dto.GenerateModifyDTO;
import com.ai.da.model.dto.GenerateThroughImageTextDTO; import com.ai.da.model.dto.GenerateThroughImageTextDTO;
import com.ai.da.model.dto.ImageToSketchDTO;
import com.ai.da.model.vo.*; import com.ai.da.model.vo.*;
import com.ai.da.service.GenerateService; import com.ai.da.service.GenerateService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
@@ -86,17 +87,15 @@ public class GenerateController {
@ApiOperation(value = "imageToSketch") @ApiOperation(value = "imageToSketch")
@PostMapping("/imageToSketch") @PostMapping("/imageToSketch")
public Response<String> imageToSketch(@Valid @RequestBody GenerateThroughImageTextDTO generateThroughImageTextDTO) { public Response<GenerateResultVO> imageToSketch(@Valid @RequestBody ImageToSketchDTO imageToSketchDTO) {
String generateResult = generateService.imageToSketch(generateThroughImageTextDTO); return Response.success(generateService.imageToSketch(imageToSketchDTO));
return Response.success(generateResult);
} }
// modifySketch // modifySketch
@ApiOperation(value = "modifySketch") @ApiOperation(value = "modifySketch")
@PostMapping("/modifySketch") @PostMapping("/modifySketch")
public Response<String> modifySketch(@Valid @RequestBody GenerateModifyDTO generateModifyDTO) { public Response<CollectionElementVO> modifySketch(@Valid @RequestBody GenerateModifyDTO generateModifyDTO) {
generateService.modifySketch(generateModifyDTO); return Response.success(generateService.modifySketch(generateModifyDTO));
return Response.success("success");
} }
} }

View File

@@ -72,6 +72,19 @@ public class Generate {
*/ */
private String seed; private String seed;
/**
* 1 -> 粗
* 2 -> 中
* 3 -> 细
* custom -> 自定义
*/
private String sketchStyle;
/**
* sketch 风格参考图的collection_element_id
*/
private Long styleImageElementId;
/** /**
* 创建时间 * 创建时间
*/ */

View File

@@ -0,0 +1,22 @@
package com.ai.da.model.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@ApiModel("image to sketch")
@Data
public class ImageToSketchDTO {
@ApiModelProperty("上传图片的collection_element_id")
private Long elementId;
@ApiModelProperty("sketch线条风格 1->粗, 2->中, 3->细,传数字,自定义风格时,传空 ")
private String style;
@ApiModelProperty("自定义线条风格上传图片的collection_element_id")
private Long styleImageId;
@ApiModelProperty("性别")
private String gender;
}

View File

@@ -0,0 +1,20 @@
package com.ai.da.model.enums;
public enum SketchStyle implements IEnumDisplay{
THICK("Thick"),
MEDIUM("Medium"),
THIN("Thin");
private String value;
SketchStyle(String value) {
this.value = value;
}
@Override
public String getValue() {
return value;
}
}

View File

@@ -28,9 +28,10 @@ public class GenerateResultVO {
this.status = status; this.status = status;
} }
public GenerateResultVO(Long id, String url, String status) { public GenerateResultVO(Long id, String url, String status, String category) {
this.id = id; this.id = id;
this.url = url; this.url = url;
this.status = status; this.status = status;
this.category = category;
} }
} }

View File

@@ -63,8 +63,6 @@ public class PythonService {
private String accessPythonIp; private String accessPythonIp;
@Value("${access.python.port:''}") @Value("${access.python.port:''}")
private String accessPythonPort; private String accessPythonPort;
@Value("${access.python.address}")
private String fastApiPythonAddress;
@Value("${minio.bucketName.gradient}") @Value("${minio.bucketName.gradient}")
private String gradientBucketName; private String gradientBucketName;
@Value("${access.python.generate_sr_port}") @Value("${access.python.generate_sr_port}")
@@ -3449,7 +3447,7 @@ public class PythonService {
throw new BusinessException("relightImage.interface.exception"); throw new BusinessException("relightImage.interface.exception");
} }
public String imageToSketch(String imagePath, String bucket, String objectName){ public String imageToSketch(String imagePath, String bucket, String objectName, String styleCode, String styleImageUrl){
OkHttpClient client = new OkHttpClient().newBuilder() OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(30, TimeUnit.SECONDS) .connectTimeout(30, TimeUnit.SECONDS)
.pingInterval(5, TimeUnit.SECONDS)//websocket轮训间隔(单位:秒) .pingInterval(5, TimeUnit.SECONDS)//websocket轮训间隔(单位:秒)
@@ -3460,6 +3458,8 @@ public class PythonService {
//关闭FastJson的引用检测 防止出现$ref 现象 //关闭FastJson的引用检测 防止出现$ref 现象
Map<String, Object> map = new HashMap<>(); Map<String, Object> map = new HashMap<>();
map.put("image_url", imagePath); map.put("image_url", imagePath);
map.put("style_image_url", styleImageUrl);
map.put("default_style", styleCode);
map.put("sketch_bucket", bucket); map.put("sketch_bucket", bucket);
map.put("sketch_name", objectName); map.put("sketch_name", objectName);

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.GenerateLikeDTO;
import com.ai.da.model.dto.GenerateModifyDTO; import com.ai.da.model.dto.GenerateModifyDTO;
import com.ai.da.model.dto.GenerateThroughImageTextDTO; import com.ai.da.model.dto.GenerateThroughImageTextDTO;
import com.ai.da.model.dto.ImageToSketchDTO;
import com.ai.da.model.vo.*; import com.ai.da.model.vo.*;
import com.baomidou.mybatisplus.extension.service.IService; 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); 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); void updateDate(Long designItemId,String timeZone);
List<UserLike> getUserLikeList(Long id); 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.common.utils.*;
import com.ai.da.mapper.primary.*; import com.ai.da.mapper.primary.*;
import com.ai.da.mapper.primary.entity.*; import com.ai.da.mapper.primary.entity.*;
import com.ai.da.model.dto.GenerateLikeDTO; import com.ai.da.model.dto.*;
import com.ai.da.model.dto.GenerateModifyDTO; import com.ai.da.model.enums.SketchStyle;
import com.ai.da.model.dto.GenerateThroughImageTextDTO;
import com.ai.da.model.dto.GenerateToPythonDTO;
import com.ai.da.model.vo.*; import com.ai.da.model.vo.*;
import com.ai.da.python.PythonService; import com.ai.da.python.PythonService;
import com.ai.da.service.*; import com.ai.da.service.*;
@@ -773,25 +771,28 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public String imageToSketch(GenerateThroughImageTextDTO generateThroughImageTextDTO) { public GenerateResultVO imageToSketch(ImageToSketchDTO imageToSketchDTO) {
String bucket = userBucket; String bucket = userBucket;
Long accountId = UserContext.getUserHolder().getId(); Long accountId = UserContext.getUserHolder().getId();
String imagePath; CollectionElement collectionElement = collectionElementService.getById(imageToSketchDTO.getElementId());
if (generateThroughImageTextDTO.getDesignType().equals("collection")) { String imagePath = collectionElement.getUrl();
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");
}
log.info(minioUtil.getPreSignedUrl(imagePath, CommonConstant.MINIO_IMAGE_EXPIRE_TIME)); log.info(minioUtil.getPreSignedUrl(imagePath, CommonConstant.MINIO_IMAGE_EXPIRE_TIME));
String imageName = imagePath.substring(imagePath.lastIndexOf("/") + 1); String imageName = imagePath.substring(imagePath.lastIndexOf("/") + 1);
String objectName = accountId + "/imageToSketch/" + imageName; 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 // 存DB
Generate generate = new Generate(); Generate generate = new Generate();
@@ -799,21 +800,33 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
generate.setUniqueId(String.valueOf(0)); generate.setUniqueId(String.valueOf(0));
generate.setLevel1Type(SKETCH_BOARD.getRealName()); generate.setLevel1Type(SKETCH_BOARD.getRealName());
generate.setLevel2Type("ImageToSketch"); generate.setLevel2Type("ImageToSketch");
generate.setElementSource(generateThroughImageTextDTO.getDesignType()); generate.setElementSource("collection");
generate.setElementId(generateThroughImageTextDTO.getCollectionElementId()); generate.setElementId(imageToSketchDTO.getElementId());
generate.setGenerateType("image"); generate.setGenerateType("image");
generate.setSketchStyle(styleCode);
generate.setStyleImageElementId(imageToSketchDTO.getElementId());
generate.setCreateDate(new Date()); generate.setCreateDate(new Date());
baseMapper.insert(generate); baseMapper.insert(generate);
// 返回 // 将生成结果存入DB
return minioUtil.getPreSignedUrl(sketchPath, CommonConstant.MINIO_IMAGE_EXPIRE_TIME); 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做调整 // 对提取出来的sketch做调整
// 输入 base64以及 性别 分类将图片添加到library // 输入 base64以及 性别 分类将图片添加到library
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void modifySketch(GenerateModifyDTO generateModifyDTO) { public CollectionElementVO modifySketch(GenerateModifyDTO generateModifyDTO) {
log.info("修改提取出的sketch,并加入到library"); log.info("修改提取出的sketch,并加入到library");
Long accountId = UserContext.getUserHolder().getId(); Long accountId = UserContext.getUserHolder().getId();
String base64 = generateModifyDTO.getBase64(); String base64 = generateModifyDTO.getBase64();
@@ -826,17 +839,23 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
log.info("修改后的图片 {}", minioPath); log.info("修改后的图片 {}", minioPath);
// 存入db // 存入db 保存到t_collection_element
Library library = new Library(); CollectionElement collectionElement = new CollectionElement();
library.setAccountId(accountId); collectionElement.setAccountId(accountId);
library.setLevel1Type(SKETCH_BOARD.getRealName()); collectionElement.setCollectionId(0L);
library.setLevel2Type(category); collectionElement.setLevel1Type(SKETCH_BOARD.getRealName());
library.setLevel3Type(gender); collectionElement.setLevel2Type(generateModifyDTO.getCategory());
library.setName(LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"))); collectionElement.setName(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")));
library.setUrl(minioPath); collectionElement.setUrl(minioPath);
library.setMd5(MD5Utils.encryptFile(minioUtil.getPreSignedUrl(minioPath, 24 * 60), Boolean.FALSE)); collectionElement.setHasPin((byte)0);
library.setCreateDate(new Date()); 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); 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());
}
} }

View File

@@ -209,3 +209,7 @@ BELT=Belt
CORSAGE=Corsage CORSAGE=Corsage
ZIPPER=Zipper ZIPPER=Zipper
POCKET=Pocket POCKET=Pocket
THICK=Thick Lines
MEDIUM=Medium Lines
THIN=Thin lines

View File

@@ -202,3 +202,7 @@ BELT=腰带
CORSAGE=胸花 CORSAGE=胸花
ZIPPER=拉链 ZIPPER=拉链
POCKET=口袋 POCKET=口袋
THICK=粗线条
MEDIUM=中线条
THIN=细线条