PoseTransformation-初版

This commit is contained in:
2025-03-20 17:42:16 +08:00
parent 6a625ed4ea
commit 6b62cf7299
17 changed files with 425 additions and 4 deletions

View File

@@ -59,6 +59,8 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
private RedisUtil redisUtil;
@Resource
private GenerateCancelMapper generateCancelMapper;
@Resource
private SketchReconstructionMapper sketchReconstructionMapper;
@Value("${redis.key.orderForGenerate}")
private String consumptionOrderKey;
@@ -715,6 +717,8 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
String path;
if (type.equals("Logo")) {
path = CommonConstant.GENERATE_LOGO_SINGLE_CANCEL;
} else if(type.equals("PoseTransformation")){
path =CommonConstant.POSE_TRANSFORMATION_CANCEL;
} else {
path = CommonConstant.GENERATE_CANCEL;
}
@@ -920,4 +924,138 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper, Generate> i
return new GenerateResultVO(generateDetailId, minioUtil.getPreSignedUrl(minioPath, CommonConstant.MINIO_IMAGE_EXPIRE_TIME, true), "Success", category);
}
public String poseTransform(Long projectId, String productImage, int poseId){
Long accountId = UserContext.getUserHolder().getId();
// 1、判断用户当前积分是否够本次生成消耗
CreditsEventsEnum creditsEventsEnum = CreditsEventsEnum.POSE_TRANSFORMATION;
Boolean preDeduction = creditsService.creditsPreDeduction(creditsEventsEnum, 1);
if (!preDeduction) {
throw new BusinessException("remaining.credits.insufficient", ResultEnum.WARNING.getCode());
}
// 3、生成唯一id 使用uuid,由于uuid重复的几率很小故取消对uuid重复性的校验
String uuid = UUID.randomUUID().toString();
String taskId = uuid + "-" + accountId;
PoseTransformation poseTransformation = new PoseTransformation();
poseTransformation.setProjectId(projectId);
poseTransformation.setAccountId(accountId);
poseTransformation.setUniqueId(taskId);
poseTransformation.setProductImage(productImage);
poseTransformation.setPoseId(poseId);
poseTransformation.setCreateTime(LocalDateTime.now());
poseTransformationMapper.insert(poseTransformation);
Boolean b = pythonService.poseTransformation(productImage, poseId, taskId);
if (b){
// 6、添加预扣除积分到redis
creditsService.addRecordToCreditsDeduction(accountId, uuid, creditsEventsEnum);
// 6.1 添加积分扣除记录到db
creditsService.preInsert(accountId, creditsEventsEnum.getName(), uuid, Boolean.TRUE, null);
return taskId;
}
throw new BusinessException("pose transformation error", ResultEnum.ERROR.getCode());
}
@Resource
private PoseTransformationMapper poseTransformationMapper;
public void processPoseTransformResult(String taskId, String gifUrl, String videoUrl, String imageUrl){
// 1、存储模型返回的数据
PoseTransformation poseTransformation;
QueryWrapper<PoseTransformation> qw = new QueryWrapper<>();
qw.eq("unique_id", taskId);
List<PoseTransformation> poseTransformations = poseTransformationMapper.selectList(qw);
if (poseTransformations != null && poseTransformations.size() > 1){
log.warn("通过taskId {} 查询到的PoseTransformation的结果不止一条", taskId);
}else if (poseTransformations == null || poseTransformations.isEmpty()){
return ;
}
poseTransformation = poseTransformations.get(0);
poseTransformation.setGifUrl(gifUrl);
poseTransformation.setVideoUrl(videoUrl);
poseTransformation.setImageUrl(imageUrl);
poseTransformation.setUpdateTime(LocalDateTime.now());
poseTransformationMapper.updateById(poseTransformation);
String key = generateResultKey + ":" + taskId;
PoseTransformationVO poseTransformationVO = new PoseTransformationVO(
poseTransformation.getId(), taskId, gifUrl, videoUrl, imageUrl, (byte) 0, "Success");
// 2、更新redis
redisUtil.addToString(key, new Gson().toJson(poseTransformationVO), CommonConstant.GENERATE_RESULT_EXPIRE_TIME);
// 3、执行积分扣除
String accountId = taskId.substring(taskId.lastIndexOf("-") + 1);
String uuid = taskId.substring(0, taskId.lastIndexOf("-"));
Boolean flag = creditsService.taskCreditsDeduction(Long.parseLong(accountId), uuid);
if (flag) creditsService.updateChangedCredits(accountId, uuid);
}
public PoseTransformationVO getPoseTransformationResult(String taskId){
String key = generateResultKey + ":" + taskId;
String resultJson = redisUtil.getFromString(key);
if (!StringUtil.isNullOrEmpty(resultJson)){
PoseTransformationVO poseTransformationVO = new Gson().fromJson(redisUtil.getFromString(key), PoseTransformationVO.class);
if (poseTransformationVO.getStatus().equals("Success")){
poseTransformationVO.setGifUrl(minioUtil.getPreSignedUrl(poseTransformationVO.getGifUrl(), CommonConstant.MINIO_IMAGE_EXPIRE_TIME));
poseTransformationVO.setVideoUrl(minioUtil.getPreSignedUrl(poseTransformationVO.getVideoUrl(), CommonConstant.MINIO_IMAGE_EXPIRE_TIME));
poseTransformationVO.setImageUrl(minioUtil.getPreSignedUrl(poseTransformationVO.getImageUrl(), CommonConstant.MINIO_IMAGE_EXPIRE_TIME));
}
return poseTransformationVO;
}else {
return new PoseTransformationVO();
}
}
/**
* String collagePicture(Base64)
* List<DTO(id, type)> elements
* File file
* @return
*/
public String sketchReconstruction(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);
});
// 将画布文件上传到minio,地址保存到project表中
String canvasPath = minioUtil.upload("aida-users", accountId + "/CollagePicture/CanvasFile", sketchReconstructionDTO.getFile());
}
// 需要返回哪些信息呢?
return null;
}
}