package com.ai.da.python; import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.exceptions.ExceptionUtil; import com.ai.da.common.config.FileProperties; import com.ai.da.common.config.exception.BusinessException; import com.ai.da.common.enums.*; import com.ai.da.common.utils.*; import com.ai.da.mapper.entity.Collection; import com.ai.da.mapper.entity.CollectionElement; import com.ai.da.mapper.entity.DesignHistory; import com.ai.da.model.dto.*; import com.ai.da.model.vo.*; import com.ai.da.python.vo.*; import com.ai.da.service.DesignHistoryService; import com.ai.da.service.SysFileService; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.serializer.SerializerFeature; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import lombok.extern.slf4j.Slf4j; import okhttp3.*; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import java.io.File; import java.io.IOException; import java.math.BigDecimal; import java.util.*; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; import java.util.stream.Stream; @Slf4j @Service public class PythonService { @Resource private FileProperties fileProperties; @Resource private SysFileService sysFileService; @Resource private DesignHistoryService designHistoryService; @Value("${access.python.ip:''}") private String accessPythonIp; /** * 生成打印的图片 二合一 * * @param printPath * @return */ public String generatePrint(List printPath) { //限流校验 AccessLimitUtils.validate("generatePrint",2); 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"); Map content = Maps.newHashMap(); content.put("fusion_content_path", printPath.get(0)); content.put("fusion_style_path", printPath.get(1)); content.put("fusion_output_path", getPythonOutputPath( printPath.get(0), PythonToJavaApiOperationTypeEnum.GENERATE_PRINT)); RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(content)); Request request = new Request.Builder() .url(accessPythonIp + ":9999/aifda/api/v1.0/fusion_test") .method("POST", body) .addHeader("Authorization", "Basic YWlkbGFiOjEyMw==") .addHeader("Content-Type", "application/json") .build(); Response response = null; try { log.info("生成印花请求入参content###{}", JSON.toJSONString(content)); response = client.newCall(request).execute(); } catch (IOException ioException) { log.error("PythonService##generatePrint异常###{}", ExceptionUtil.getThrowableList(ioException)); } //去除限流 AccessLimitUtils.validateOut("generatePrint"); if (Objects.isNull(response)) { log.error("PythonService##generatePrint异常###{}", "response or body is empty!"); throw new BusinessException("generate print exception!"); } JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(response)); Boolean result = jsonObject.getBoolean("successful"); if (result) { return content.get("fusion_output_path"); } log.info("生成印花失败###{}", jsonObject); //生成失败 throw new BusinessException("generate print exception!"); } /** * 生成python要保存的文件 * * @param picturePath * @return */ private String getPythonOutputPath(String picturePath, PythonToJavaApiOperationTypeEnum pythonOperationType) { String linuxDomain = fileProperties.getLinuxDomain(); String day = DateUtil.dateToStr(new Date(), DateUtil.YYYYMM); //获取图片后缀 String suffix = picturePath.substring(picturePath.lastIndexOf(".")); String generateFileName = pythonOperationType.getRealName() + "-" + UUID.randomUUID().toString() + suffix; return linuxDomain + day + File.separator + "pythonFile" + File.separator + pythonOperationType.getRealName() + File.separator + generateFileName; } /** * python生成的文件上传到java服务器统一维护 * * @param file * @param operateType * @return */ @Transactional public String upload(MultipartFile file, String operateType) { //用户信息 PythonToJavaApiOperationTypeEnum operationType = PythonToJavaApiOperationTypeEnum.uploadOf(operateType); Assert.notNull(operationType, "unknown operateType " + operateType + "!"); String path = calculateFileUrl(operationType); File generateFile = FileUtil.upload2(file, path); String linuxDomain = fileProperties.getLinuxDomain(); if (!StringUtils.isEmpty(linuxDomain)) { //linux 系统 String oldPath = fileProperties.getSys().getPath(); return generateFile.getAbsolutePath().replace(oldPath, linuxDomain); } return null; } private String calculateFileUrl(PythonToJavaApiOperationTypeEnum operationType) { String rootPath = fileProperties.getSys().getPath(); String day = DateUtil.dateToStr(new Date(), DateUtil.YYYYMM); return rootPath + day + File.separator + "pythonFile" + File.separator + operationType.getRealName() + File.separator; } /** * 组装和计算design参数 * * @param systemScale * @param singleOverall * @param switchCategory * @param elementVO * @return */ public DesignPythonObjects covertDesignParam(BigDecimal systemScale, String singleOverall, String switchCategory, ValidateElementVO elementVO) { DesignPythonObjects designPythonObjects = new DesignPythonObjects(); List objects = Lists.newArrayList(); designPythonObjects.setObjects(objects); long pinPrintNum = calculateDesignPinPrintNum(elementVO.getPrintBoardElements()); long noPinPrintNum = calculateDesignNoPinPrintNum(elementVO.getPrintBoardElements()); //没有print的 long noPrintNum = 8 - pinPrintNum - noPinPrintNum; elementVO.setNoPinPrintNum(noPinPrintNum); //系统比列 BigDecimal sysRatio = systemScale; int pinPictureNum = calculatePinPictureNum(elementVO.getSketchBoardElements(), elementVO.getHasUseMd5List()); int sysPictureNum = calculateSysPictureNum(sysRatio, pinPictureNum); int userPictureNum = calculateUserLibraryPictureNum(sysPictureNum, pinPictureNum); int noPinPictureNum = 8 - pinPictureNum - sysPictureNum - userPictureNum; for (int i = 0; i < 8; i++) { //sketch计算 CurrentDesignPictureTypeEnum designPictureType = calculateCurrentDesignPictureType(pinPictureNum, sysPictureNum, userPictureNum, noPinPictureNum); if (Objects.isNull(designPictureType)) { break; } switch (designPictureType) { case PIN: pinPictureNum--; break; case USER_LIBRARY: userPictureNum--; break; case SYS_FILE: sysPictureNum--; break; case NO_PIN: noPinPictureNum--; break; default: } //print计算 CurrentDesignPrintPictureTypeEnum designPrintPictureType = calculateCurrentDesignPintPictureType(pinPrintNum, noPinPrintNum, noPrintNum); if (Objects.isNull(designPrintPictureType)) { break; } switch (designPrintPictureType) { case PIN: pinPrintNum--; break; case NO_PIN: noPinPrintNum--; break; case NO: noPrintNum--; break; default: } //确定本次designSingle是否print DesignPythonItemPrint designPythonItemPrint = getRandomPrint(elementVO, designPrintPictureType); elementVO.setDesignPythonItemPrint(designPythonItemPrint); //参数透传 确定本次designSingle如果需要print对应的种类 elementVO.setDesignPrintPictureTypeLayoutList(calculateCurrentDesignPintPictureTypeLayout()); //designSingle具体参数组装 DesignPythonObject pythonObject = new DesignPythonObject(); pythonObject.setItems(coverToDesignPythonItem(elementVO, designPictureType)); pythonObject.setBasic(coverToBasic(pythonObject.getItems().get(0), singleOverall, switchCategory, elementVO.getDesignLibraryModelPoint())); objects.add(pythonObject); } return designPythonObjects; } //计算当前的图片类型 private CurrentDesignPictureTypeEnum calculateCurrentDesignPictureType(int pinPictureNum, int sysPictureNum, int userPictureNum, int noPinPictureNum) { List codes = Lists.newArrayList(); if (pinPictureNum > 0) { //pin默认优先选择 不参与计算 后续有调整再说 // codes.add(pinPictureNum); return CurrentDesignPictureTypeEnum.PIN; } if (sysPictureNum > 0) { codes.add(CurrentDesignPictureTypeEnum.SYS_FILE.getCode()); } if (userPictureNum > 0) { codes.add(CurrentDesignPictureTypeEnum.USER_LIBRARY.getCode()); } if (noPinPictureNum > 0) { codes.add(CurrentDesignPictureTypeEnum.NO_PIN.getCode()); } List pictureTypeEnums = CurrentDesignPictureTypeEnum.ofList(codes); if (CollectionUtils.isEmpty(pictureTypeEnums)) { return null; } int max = pictureTypeEnums.size(); int min = 0; Long randomIndex = RandomsUtil.randomSysFile((long) min, (long) max); return pictureTypeEnums.get(randomIndex.intValue()); } //计算系统图片张数 private static int calculateSysPictureNum(BigDecimal sysRatio, int pinPictureNum) { if (pinPictureNum > 4) { int poolSize = 8 - pinPictureNum; if (poolSize == 0) { return 0; } return BigDecimal.valueOf(poolSize).multiply(sysRatio) .setScale(0, BigDecimal.ROUND_HALF_UP).intValue(); } else { return BigDecimal.valueOf(4).multiply(sysRatio) .setScale(0, BigDecimal.ROUND_HALF_UP).intValue(); } } //计算用户库图片张数 private static int calculateUserLibraryPictureNum(int sysPictureNum, int pinPictureNum) { if (pinPictureNum > 4) { return 8 - pinPictureNum - sysPictureNum; } else { return 4 - sysPictureNum; } } //计算Pin图片张数 private int calculatePinPictureNum(List sketchBoardElements, List hasUseMd5List) { List pinData = getPinData(sketchBoardElements, hasUseMd5List); if (CollectionUtil.isEmpty(pinData)) { return 0; } long topNum = sketchBoardElements.stream() .filter(skecth -> skecth.getHasPin() == 1 && DesignPythonItem.OUTWEAR_DRESS_BLOUSE.contains(skecth.getLevel2Type())).count(); long bottomNum = sketchBoardElements.stream() .filter(skecth -> skecth.getHasPin() == 1 && DesignPythonItem.SKIRT_TROUSERS.contains(skecth.getLevel2Type())).count(); int num = Arrays.asList(topNum, bottomNum).stream().max(Comparator.comparing(Long::valueOf)).get().intValue(); if (num > 8) { return 8; } return num; } //计算当前的Print图片类型 private CurrentDesignPrintPictureTypeEnum calculateCurrentDesignPintPictureType(long pinPrintNum, long noPinPrintNum, long noPrintNum) { List codes = Lists.newArrayList(); if (pinPrintNum > 0) { //pin默认优先选择 不参与计算 后续有调整再说 // codes.add(pinPictureNum); return CurrentDesignPrintPictureTypeEnum.PIN; } if (noPinPrintNum > 0) { codes.add(CurrentDesignPrintPictureTypeEnum.NO_PIN.getCode()); } if (noPrintNum > 0) { codes.add(CurrentDesignPrintPictureTypeEnum.NO.getCode()); } List pictureTypeEnums = CurrentDesignPrintPictureTypeEnum.ofList(codes); if (CollectionUtils.isEmpty(pictureTypeEnums)) { return null; } int max = pictureTypeEnums.size(); int min = 0; Long randomIndex = RandomsUtil.randomSysFile((long) min, (long) max); return pictureTypeEnums.get(randomIndex.intValue()); } //计算当前的Print图片类型具体分布位置 0. 上衣 1.下衣 2.上衣和下衣都print private List calculateCurrentDesignPintPictureTypeLayout() { Long randomIndex = RandomsUtil.randomSysFile(0L, 3L); if (randomIndex == 0) { return DesignPythonItem.OUTWEAR_DRESS_BLOUSE; } if (randomIndex == 1) { return DesignPythonItem.SKIRT_TROUSERS; } if (randomIndex == 2) { List all = new ArrayList<>(DesignPythonItem.OUTWEAR_DRESS_BLOUSE); all.addAll(new ArrayList<>(DesignPythonItem.SKIRT_TROUSERS)); return all; } return null; } //计算print 非Pin图片剩余张数 private long calculateDesignPinPrintNum(List printBoardElements) { if (CollectionUtils.isEmpty(printBoardElements)) { return 0; } return printBoardElements.stream().filter(f -> f.getHasPin() == 1).count(); } //计算print 非Pin图片剩余张数 private long calculateDesignNoPinPrintNum(List printBoardElements) { if (CollectionUtils.isEmpty(printBoardElements)) { return 0; } long totalNum = printBoardElements.size() / 2; long pinNum = printBoardElements.stream().filter(f -> f.getHasPin() == 1).count(); if (pinNum >= totalNum) { return 0; } return totalNum - pinNum; } private List coverToDesignPythonItem(ValidateElementVO elementVO, CurrentDesignPictureTypeEnum designPictureType) { //Pin的数据 List pinData = getPinData(elementVO.getSketchBoardElements(), elementVO.getHasUseMd5List()); //计算填充PythonItemBlouse List items = Lists.newArrayList(); List blouseList = calculatePythonItemBlouse(pinData, elementVO, designPictureType); if (!CollectionUtils.isEmpty(blouseList)) { items.addAll(blouseList); } //计算填充PythonItemSkirt DesignPythonItem skirt = calculatePythonItemSkirt(pinData, elementVO, designPictureType); if (Objects.nonNull(skirt)) { items.add(skirt); } //计算填充Hairstyle Earring Shoes Body items.addAll(calculatePythonItemHairstyleShoes(elementVO, elementVO.getDesignLibraryModelPoint())); return items; } private List calculatePythonItemHairstyleShoes(ValidateElementVO elementVO, DesignLibraryModelPointVO designLibraryModelPoint) { List items = Lists.newArrayList(); DesignPythonItem.SYS_HAIRSTYLE_SHOES_BODY.forEach(type -> { if (SingleOverallEnum.SINGLE.getRealName().equals(elementVO.getSingleOverall()) || Objects.nonNull(designLibraryModelPoint)){ //single和models模特时候不传耳环 首饰,鞋子等 if(!SysFileLevel2TypeEnum.BODY.getRealName().equals(type)){ return; } } DesignPythonItem pythonItem = new DesignPythonItem(); items.add(pythonItem); //类型 pythonItem.setType(type); if (SysFileLevel2TypeEnum.BODY.getRealName().equals(type)) { if (Objects.nonNull(designLibraryModelPoint)) { pythonItem.setBody_path(designLibraryModelPoint.getTemplateUrl()); } else { // pythonItem.setBody_path("/home/pangkaicheng/python_code/Multi-layer-Virtual-Try-on/dataset_for_test/Img_model.png"); pythonItem.setBody_path("/workspace/python_code/Multi-layer-Virtual-Try-on/dataset_for_test/Img_model.png"); } } else { SysFileVO sysFileVO = getRandomSysFileByLevel2Type(type, Lists.newArrayList()); pythonItem.setPath(sysFileVO.getUrl()); pythonItem.setBusinessId(sysFileVO.getId()); if (SysFileLevel2TypeEnum.SHOES.getRealName().equals(type)) { pythonItem.setColor(getRandomColor(elementVO.getColorBoards())); } } }); return items; } private DesignPythonItem calculatePythonItemSkirt(List pinData, ValidateElementVO elementVO, CurrentDesignPictureTypeEnum designPictureType) { // skirt, trousers 只选择一个 DesignPythonItem skirt = null; //剩余的的PinData List existPinDataIds = elementVO.getExistPinDataIds(); List residuePinData = residuePinData(pinData, existPinDataIds, elementVO.getHasUseMd5List()); if (CollectionUtil.isNotEmpty(residuePinData)) { //single模式 if (SingleOverallEnum.SINGLE.getRealName().equals(elementVO.getSingleOverall())) { if (DesignPythonItem.SKIRT_TROUSERS.contains(elementVO.getSwitchCategory())) { //是否包含的Pin Boolean isPin = residuePinData.stream().filter(residue -> elementVO.getSwitchCategory().equals(residue.getLevel2Type())).findFirst().isPresent(); //如果包含Pin只传一个 if (isPin) { //Pin的数据 pinData = getPinDataByLevel2Type(pinData, elementVO.getSwitchCategory()); //剩余的的PinData List residueInnerPinDataByType = residuePinData(pinData, existPinDataIds, elementVO.getHasUseMd5List()); CollectionElement elementNew = CollectionUtils.isEmpty(residueInnerPinDataByType) ? null : residueInnerPinDataByType.get(0); if (Objects.nonNull(elementNew)) { skirt = coverToDesignPythonItem(elementNew.getId(), elementVO.getSwitchCategory(), elementNew.getUrl(), elementVO); //去重用 existPinDataIds.add(elementNew.getId()); //添加已使用的md5 elementVO.getHasUseMd5List().add(elementNew.getMd5()); } } } } else { //是否包含skirt的Pin Boolean isPin = residuePinData.stream().filter(residue -> DesignPythonItem.SKIRT_TROUSERS.contains(residue.getLevel2Type())).findFirst().isPresent(); //如果包含Pin只传一个 if (isPin) { for (String type : DesignPythonItem.SKIRT_TROUSERS) { //剩余的的PinData List residueInnerPinData = residuePinData(pinData, existPinDataIds, elementVO.getHasUseMd5List()); CollectionElement elementNew = residueInnerPinData.stream() .filter(element -> element.getLevel2Type().equals(type)).findFirst().orElse(null); if (Objects.nonNull(elementNew)) { skirt = coverToDesignPythonItem(elementNew.getId(), type, elementNew.getUrl(), elementVO); //去重用 existPinDataIds.add(elementNew.getId()); //添加已使用的md5 elementVO.getHasUseMd5List().add(elementNew.getMd5()); break; } } } } } if (Objects.nonNull(skirt)) { return skirt; } switch (designPictureType) { case PIN: if (Objects.isNull(skirt)) { //single模式 if (SingleOverallEnum.SINGLE.getRealName().equals(elementVO.getSingleOverall())) { if (DesignPythonItem.SKIRT_TROUSERS.contains(elementVO.getSwitchCategory())) { //系统获取 SysFileVO sysFileVO = getRandomSysFileByLevel2Type(elementVO.getSwitchCategory(), elementVO.getSysFileIds()); skirt = coverToDesignPythonItem(null, sysFileVO.getLevel2Type(), sysFileVO.getUrl(), elementVO); skirt.setBusinessId(sysFileVO.getId()); return skirt; } } else { //系统获取 SysFileVO sysFileVO = getRandomSysFileSkirt(elementVO.getSysFileVo(),elementVO.getSysFileIds()); skirt = coverToDesignPythonItem(null, sysFileVO.getLevel2Type(), sysFileVO.getUrl(), elementVO); skirt.setBusinessId(sysFileVO.getId()); //添加已使用的md5 return skirt; } } break; case USER_LIBRARY: //single模式 if (SingleOverallEnum.SINGLE.getRealName().equals(elementVO.getSingleOverall())) { if (DesignPythonItem.SKIRT_TROUSERS.contains(elementVO.getSwitchCategory())) { LibraryVo libraryVo = getRandomLibrary(elementVO.getLibraryVos(), Arrays.asList(elementVO.getSwitchCategory()), elementVO.getHasUseMd5List()); if (StringUtils.isEmpty(libraryVo)) { //系统获取 SysFileVO sysFileVO = getRandomSysFileByLevel2Type(elementVO.getSwitchCategory(), elementVO.getSysFileIds()); skirt = coverToDesignPythonItem(null, sysFileVO.getLevel2Type(), sysFileVO.getUrl(), elementVO); skirt.setBusinessId(sysFileVO.getId()); return skirt; } skirt = coverToDesignPythonItem(null, libraryVo.getLevel2Type(), libraryVo.getUrl(), elementVO); elementVO.getHasUseMd5List().add(libraryVo.getMd5()); skirt.setBusinessId(libraryVo.getId()); } } else { LibraryVo libraryVo = getRandomLibrary(elementVO.getLibraryVos(), DesignPythonItem.SKIRT_TROUSERS, elementVO.getHasUseMd5List()); if (StringUtils.isEmpty(libraryVo)) { //系统获取 SysFileVO sysFileVO = getRandomSysFileSkirt(elementVO.getSysFileVo(),elementVO.getSysFileIds()); skirt = coverToDesignPythonItem(null, sysFileVO.getLevel2Type(), sysFileVO.getUrl(), elementVO); skirt.setBusinessId(sysFileVO.getId()); //添加已使用的md5 return skirt; } skirt = coverToDesignPythonItem(null, libraryVo.getLevel2Type(), libraryVo.getUrl(), elementVO); skirt.setBusinessId(libraryVo.getId()); elementVO.getHasUseMd5List().add(libraryVo.getMd5()); } break; case SYS_FILE: //single模式 if (SingleOverallEnum.SINGLE.getRealName().equals(elementVO.getSingleOverall())) { if (DesignPythonItem.SKIRT_TROUSERS.contains(elementVO.getSwitchCategory())) { //系统获取 SysFileVO sysFileVO = getRandomSysFileByLevel2Type(elementVO.getSwitchCategory(), elementVO.getSysFileIds()); skirt = coverToDesignPythonItem(null, sysFileVO.getLevel2Type(), sysFileVO.getUrl(), elementVO); skirt.setBusinessId(sysFileVO.getId()); return skirt; } } else { //系统获取 SysFileVO sysFileVO = getRandomSysFileSkirt(elementVO.getSysFileVo(),elementVO.getSysFileIds()); skirt = coverToDesignPythonItem(null, sysFileVO.getLevel2Type(), sysFileVO.getUrl(), elementVO); skirt.setBusinessId(sysFileVO.getId()); //添加已使用的md5 return skirt; } break; case NO_PIN: //single模式 if (SingleOverallEnum.SINGLE.getRealName().equals(elementVO.getSingleOverall())) { if (DesignPythonItem.SKIRT_TROUSERS.contains(elementVO.getSwitchCategory())) { //非Pin的数据 List noPinDataByType = getNoPinData(elementVO.getSketchBoardElements(), Collections.singletonList(elementVO.getSwitchCategory())); CollectionElement collectionElement = getRandomSketchLibrary(noPinDataByType, elementVO.getHasUseMd5List()); if (Objects.isNull(collectionElement)) { //系统获取 SysFileVO sysFileVO = getRandomSysFileByLevel2Type(elementVO.getSwitchCategory(), elementVO.getSysFileIds()); skirt = coverToDesignPythonItem(null, sysFileVO.getLevel2Type(), sysFileVO.getUrl(), elementVO); skirt.setBusinessId(sysFileVO.getId()); } else { elementVO.getHasUseMd5List().add(collectionElement.getMd5()); skirt = coverToDesignPythonItem(collectionElement.getId(), collectionElement.getLevel2Type(), collectionElement.getUrl(), elementVO); } } } else { //非Pin的数据 List noPinData = getNoPinData(elementVO.getSketchBoardElements(), DesignPythonItem.SKIRT_TROUSERS); CollectionElement collectionElement = getRandomSketchLibrary(noPinData, elementVO.getHasUseMd5List()); if (Objects.isNull(collectionElement)) { //系统获取 SysFileVO sysFileVO = getRandomSysFileSkirt(elementVO.getSysFileVo(),elementVO.getSysFileIds()); skirt = coverToDesignPythonItem(null, sysFileVO.getLevel2Type(), sysFileVO.getUrl(), elementVO); skirt.setBusinessId(sysFileVO.getId()); } else { elementVO.getHasUseMd5List().add(collectionElement.getMd5()); skirt = coverToDesignPythonItem(collectionElement.getId(), collectionElement.getLevel2Type(), collectionElement.getUrl(), elementVO); } } break; default: } return skirt; } private List calculatePythonItemBlouse(List pinData, ValidateElementVO elementVO, CurrentDesignPictureTypeEnum designPictureType) { List items = Lists.newArrayList(); //剩余的的PinData List existPinDataIds = elementVO.getExistPinDataIds(); List residuePinData = residuePinData(pinData, existPinDataIds, elementVO.getHasUseMd5List()); if (CollectionUtil.isNotEmpty(residuePinData)) { //single模式 if (SingleOverallEnum.SINGLE.getRealName().equals(elementVO.getSingleOverall())) { if (DesignPythonItem.OUTWEAR_DRESS_BLOUSE.contains(elementVO.getSwitchCategory())) { //是否包含的Pin Boolean isPin = residuePinData.stream().filter(residue -> elementVO.getSwitchCategory().equals(residue.getLevel2Type())).findFirst().isPresent(); //如果包含Pin只传一个 if (isPin) { //Pin的数据 pinData = getPinDataByLevel2Type(pinData, elementVO.getSwitchCategory()); //剩余的的PinData List residueInnerPinDataByType = residuePinData(pinData, existPinDataIds, elementVO.getHasUseMd5List()); CollectionElement elementNew = CollectionUtils.isEmpty(residueInnerPinDataByType) ? null : residueInnerPinDataByType.get(0); if (Objects.nonNull(elementNew)) { items.add(coverToDesignPythonItem(elementNew.getId(), elementVO.getSwitchCategory(), elementNew.getUrl(), elementVO)); //去重用 existPinDataIds.add(elementNew.getId()); //添加MD5 elementVO.getHasUseMd5List().add(elementNew.getMd5()); } } } } else { //是否包含blouse的Pin Boolean isPin = residuePinData.stream().filter(residue -> DesignPythonItem.OUTWEAR_DRESS_BLOUSE.contains(residue.getLevel2Type())).findFirst().isPresent(); //如果包含Pin只传一个 if (isPin) { List finalPinData = pinData; DesignPythonItem.OUTWEAR_DRESS_BLOUSE.forEach(type -> { //剩余的的PinData List residueInnerPinData = residuePinData(finalPinData, existPinDataIds, elementVO.getHasUseMd5List()); CollectionElement elementNew = residueInnerPinData.stream() .filter(element -> element.getLevel2Type().equals(type)).findFirst().orElse(null); if (Objects.nonNull(elementNew)) { items.add(coverToDesignPythonItem(elementNew.getId(), type, elementNew.getUrl(), elementVO)); //去重用 existPinDataIds.add(elementNew.getId()); //添加md5 elementVO.getHasUseMd5List().add(elementNew.getMd5()); } }); } } } if (CollectionUtil.isNotEmpty(items)) { return items; } switch (designPictureType) { case PIN: if (CollectionUtil.isEmpty(items)) { //single模式 if (SingleOverallEnum.SINGLE.getRealName().equals(elementVO.getSingleOverall())) { if (DesignPythonItem.OUTWEAR_DRESS_BLOUSE.contains(elementVO.getSwitchCategory())) { //系统获取 SysFileVO sysFileVO = getRandomSysFileByLevel2Type(elementVO.getSwitchCategory(), elementVO.getSysFileIds()); DesignPythonItem item = coverToDesignPythonItem(null, sysFileVO.getLevel2Type(), sysFileVO.getUrl(), elementVO); item.setBusinessId(sysFileVO.getId()); items.add(item); return items; } } else { //系统获取 SysFileVO sysFileVO = getRandomSysFileOutwear(elementVO.getSysFileVo(),elementVO.getSysFileIds()); DesignPythonItem item = coverToDesignPythonItem(null, sysFileVO.getLevel2Type(), sysFileVO.getUrl(), elementVO); item.setBusinessId(sysFileVO.getId()); items.add(item); return items; } } break; case USER_LIBRARY: //single模式 if (SingleOverallEnum.SINGLE.getRealName().equals(elementVO.getSingleOverall())) { if (DesignPythonItem.OUTWEAR_DRESS_BLOUSE.contains(elementVO.getSwitchCategory())) { LibraryVo libraryVo = getRandomLibrary(elementVO.getLibraryVos(), Arrays.asList(elementVO.getSwitchCategory()), elementVO.getHasUseMd5List()); if (StringUtils.isEmpty(libraryVo)) { //系统获取 SysFileVO sysFileVO = getRandomSysFileByLevel2Type(elementVO.getSwitchCategory(), elementVO.getSysFileIds()); DesignPythonItem item = coverToDesignPythonItem(null, sysFileVO.getLevel2Type(), sysFileVO.getUrl(), elementVO); item.setBusinessId(sysFileVO.getId()); items.add(item); return items; } DesignPythonItem item = coverToDesignPythonItem(null, libraryVo.getLevel2Type(), libraryVo.getUrl(), elementVO); item.setBusinessId(libraryVo.getId()); elementVO.getHasUseMd5List().add(libraryVo.getMd5()); items.add(item); } } else { LibraryVo libraryVo = getRandomLibrary(elementVO.getLibraryVos(), DesignPythonItem.OUTWEAR_DRESS_BLOUSE, elementVO.getHasUseMd5List()); if (StringUtils.isEmpty(libraryVo)) { //系统获取 SysFileVO sysFileVO = getRandomSysFileOutwear(elementVO.getSysFileVo(),elementVO.getSysFileIds()); DesignPythonItem item = coverToDesignPythonItem(null, sysFileVO.getLevel2Type(), sysFileVO.getUrl(), elementVO); item.setBusinessId(sysFileVO.getId()); items.add(item); return items; } DesignPythonItem item = coverToDesignPythonItem(null, libraryVo.getLevel2Type(), libraryVo.getUrl(), elementVO); item.setBusinessId(libraryVo.getId()); elementVO.getHasUseMd5List().add(libraryVo.getMd5()); items.add(item); } break; case SYS_FILE: //single模式 if (SingleOverallEnum.SINGLE.getRealName().equals(elementVO.getSingleOverall())) { if (DesignPythonItem.OUTWEAR_DRESS_BLOUSE.contains(elementVO.getSwitchCategory())) { //系统获取 SysFileVO sysFileVO = getRandomSysFileByLevel2Type(elementVO.getSwitchCategory(), elementVO.getSysFileIds()); DesignPythonItem item = coverToDesignPythonItem(null, sysFileVO.getLevel2Type(), sysFileVO.getUrl(), elementVO); item.setBusinessId(sysFileVO.getId()); items.add(item); return items; } } else { //系统获取 SysFileVO sysFileVO = getRandomSysFileOutwear(elementVO.getSysFileVo(),elementVO.getSysFileIds()); DesignPythonItem item = coverToDesignPythonItem(null, sysFileVO.getLevel2Type(), sysFileVO.getUrl(), elementVO); item.setBusinessId(sysFileVO.getId()); items.add(item); return items; } break; case NO_PIN: //single模式 if (SingleOverallEnum.SINGLE.getRealName().equals(elementVO.getSingleOverall())) { if (DesignPythonItem.OUTWEAR_DRESS_BLOUSE.contains(elementVO.getSwitchCategory())) { //非Pin的数据 List noPinDataByType = getNoPinData( elementVO.getSketchBoardElements(), Collections.singletonList(elementVO.getSwitchCategory())); CollectionElement collectionElement = getRandomSketchLibrary(noPinDataByType, elementVO.getHasUseMd5List()); if (Objects.isNull(collectionElement)) { //系统获取 SysFileVO sysFileVO = getRandomSysFileByLevel2Type(elementVO.getSwitchCategory(), elementVO.getSysFileIds()); DesignPythonItem item = coverToDesignPythonItem(null, sysFileVO.getLevel2Type(), sysFileVO.getUrl(), elementVO); item.setBusinessId(sysFileVO.getId()); items.add(item); } else { elementVO.getHasUseMd5List().add(collectionElement.getMd5()); items.add(coverToDesignPythonItem(collectionElement.getId(), collectionElement.getLevel2Type(), collectionElement.getUrl(), elementVO)); } } } else { //非Pin的数据 List noPinData = getNoPinData(elementVO.getSketchBoardElements(), DesignPythonItem.OUTWEAR_DRESS_BLOUSE); CollectionElement collectionElement = getRandomSketchLibrary(noPinData, elementVO.getHasUseMd5List()); if (Objects.isNull(collectionElement)) { //系统获取 SysFileVO sysFileVO = getRandomSysFileOutwear(elementVO.getSysFileVo(),elementVO.getSysFileIds()); DesignPythonItem item = coverToDesignPythonItem(null, sysFileVO.getLevel2Type(), sysFileVO.getUrl(), elementVO); item.setBusinessId(sysFileVO.getId()); items.add(item); } else { elementVO.getHasUseMd5List().add(collectionElement.getMd5()); items.add(coverToDesignPythonItem(collectionElement.getId(), collectionElement.getLevel2Type(), collectionElement.getUrl(), elementVO)); } } break; default: } return items; } private DesignPythonItem coverToDesignPythonItem(Long elementId, String type, String path, ValidateElementVO elementVO) { DesignPythonItem designPythonItemBlouse = new DesignPythonItem(); if (Objects.nonNull(elementId)) { designPythonItemBlouse.setElementId(elementId); designPythonItemBlouse.setBusinessId(elementId); } designPythonItemBlouse.setType(type); designPythonItemBlouse.setPath(path); //所有的icon都是none designPythonItemBlouse.setIcon("none"); designPythonItemBlouse.setColor(getRandomColor(elementVO.getColorBoards())); if (!elementVO.getDesignPythonItemPrint().getPath().equals("none") && elementVO.getDesignPrintPictureTypeLayoutList().contains(type)) { designPythonItemBlouse.setPrint(CopyUtil.copyObject(elementVO.getDesignPythonItemPrint(), DesignPythonItemPrint.class)); } else { designPythonItemBlouse.setPrint(new DesignPythonItemPrint("none")); } return designPythonItemBlouse; } private List residuePinData(List pinData, List existPinDataIds, List hasUseMd5List) { if (CollectionUtils.isEmpty(pinData)) { return null; } return pinData.stream().filter(v -> (!existPinDataIds.contains(v.getId())) && (!hasUseMd5List.contains(v.getMd5()))).collect(Collectors.toList()); } private DesignPythonBasic coverToBasic(DesignPythonItem designPythonItem, String singleOverall, String switchCategory, DesignLibraryModelPointVO designLibraryModelPoint) { DesignPythonBasic basic = new DesignPythonBasic(); basic.setSingle_overall(singleOverall); basic.setSwitch_category(switchCategory); basic.setSave_name(getPythonOutputPath( StringUtils.isEmpty(designPythonItem.getPath()) ? designPythonItem.getBody_path() : designPythonItem.getPath(), PythonToJavaApiOperationTypeEnum.DESIGN_COLLECTION)); basic.setScale_bag(0.7); basic.setSelf_template(Boolean.FALSE); if (Objects.nonNull(designLibraryModelPoint)) { basic.setSelf_template(Boolean.TRUE); } basic.setScale_earrings(0.16); basic.setBody_point_test(getMap(designLibraryModelPoint)); return basic; } private Map> getMap(DesignLibraryModelPointVO designLibraryModelPointOld) { Map> body_point = Maps.newHashMap(); if (Objects.nonNull(designLibraryModelPointOld)) { DesignLibraryModelPointVO designLibraryModelPoint = CopyUtil.copyObject(designLibraryModelPointOld,DesignLibraryModelPointVO.class); //自己的template是6个点 body_point.put("shoulder_left", resolve(designLibraryModelPoint.getShoulderLeft())); body_point.put("shoulder_right", resolve(designLibraryModelPoint.getShoulderRight())); body_point.put("waistband_left", resolve(designLibraryModelPoint.getWaistbandLeft())); body_point.put("waistband_right", resolve(designLibraryModelPoint.getWaistbandRight())); body_point.put("hand_point_left", resolve(designLibraryModelPoint.getHandLeft())); body_point.put("hand_point_right", resolve(designLibraryModelPoint.getHandRight())); } else { //默认是13个点 body_point.put("shoulder_left", Arrays.asList(755, 519)); body_point.put("shoulder_right", Arrays.asList(912, 519)); body_point.put("waistband_left", Arrays.asList(777, 725)); body_point.put("waistband_right", Arrays.asList(891, 725)); body_point.put("hand_point_left", Arrays.asList(754, 886)); body_point.put("hand_point_right", Arrays.asList(915, 886)); body_point.put("head_point_up", Arrays.asList(834, 390)); body_point.put("head_point_left", Arrays.asList(804, 424)); body_point.put("head_point_right", Arrays.asList(864, 424)); body_point.put("toe_left", Arrays.asList(817, 1355)); body_point.put("toe_right", Arrays.asList(850, 1355)); body_point.put("ear_point_left", Arrays.asList(802, 439)); body_point.put("ear_point_right", Arrays.asList(865, 438)); body_point.put("foot_length", Arrays.asList(784, 1336, 825, 1336)); } return body_point; } private List resolve(List list) { List integerList = Lists.newArrayList(); list.forEach(l ->{ integerList.add(new Integer(l.intValue())); }); return integerList; } private List getPinData(List sketchBoardElements, List hasUseMd5List) { if (CollectionUtils.isEmpty(sketchBoardElements)) { return null; } List sketchBoardPins = sketchBoardElements .stream().filter(v -> v.getHasPin() == 1 && (!hasUseMd5List.contains(v.getMd5()))).collect(Collectors.toList()); if (CollectionUtils.isEmpty(sketchBoardPins)) { return null; } List response = resolveElementFilterHistoryMd5(sketchBoardPins); if (CollectionUtils.isEmpty(response)) { return null; } return CopyUtil.copyList(sketchBoardPins, CollectionElement.class); } private List resolveElementFilterHistoryMd5(List elemet) { //查询历史design数据 List md5List = elemet.stream().map(CollectionElement::getMd5).collect(Collectors.toList()); List designHistories = designHistoryService.getByMD5List(md5List); if (CollectionUtils.isEmpty(designHistories)) { return elemet; } List existMd5List = designHistories.stream().map(DesignHistory::getMd5).collect(Collectors.toList()); return elemet.stream().filter(f -> !existMd5List.contains(f.getMd5())).collect(Collectors.toList()); } private List getPinDataByLevel2Type(List sketchBoardElements, String level2Type) { if (CollectionUtils.isEmpty(sketchBoardElements)) { return null; } List sketchBoardPins = sketchBoardElements .stream().filter(v -> v.getHasPin() == 1 && v.getLevel2Type().equals(level2Type)).collect(Collectors.toList()); if (CollectionUtils.isEmpty(sketchBoardPins)) { return null; } return CopyUtil.copyList(sketchBoardPins, CollectionElement.class); } private List getNoPinData(List sketchBoardElements, List Level2TypeList) { if (CollectionUtils.isEmpty(sketchBoardElements)) { return null; } List sketchBoardPins = sketchBoardElements .stream().filter(v -> v.getHasPin() == 0 && Level2TypeList.contains(v.getLevel2Type())).collect(Collectors.toList()); if (CollectionUtils.isEmpty(sketchBoardPins)) { return null; } return CopyUtil.copyList(sketchBoardPins, CollectionElement.class); } private SysFileVO getRandomSysFileOutwear(List sysFileVo,List sysFileIds) { Long randomIndex = RandomsUtil.randomSysFile(0L, 3L); if(CollectionUtils.isEmpty(sysFileVo)){ return getRandomSysFileByLevel2Type(DesignPythonItem.OUTWEAR_DRESS_BLOUSE.get(randomIndex.intValue()), sysFileIds); }else{ //attribute_retrieval 算法,满足blouse + outwear+dress>8,以及 skirt+trousers>8 return getRandomSysFileByAttributeRetrieval(DesignPythonItem.OUTWEAR_DRESS_BLOUSE,sysFileVo, sysFileIds); } } private SysFileVO getRandomSysFileSkirt(List sysFileVo,List sysFileIds) { Long randomIndex = RandomsUtil.randomSysFile(0L, 2L); if(CollectionUtils.isEmpty(sysFileVo)){ return getRandomSysFileByLevel2Type(DesignPythonItem.SKIRT_TROUSERS.get(randomIndex.intValue()), sysFileIds); }else{ //attribute_retrieval 算法,满足blouse + outwear+dress>8,以及 skirt+trousers>8 return getRandomSysFileByAttributeRetrieval(DesignPythonItem.SKIRT_TROUSERS,sysFileVo, sysFileIds); } } private SysFileVO getRandomSysFileByLevel2Type(String level2Type, List sysFileIds) { Long maxId = sysFileService.getMaxIdByLevel2Type(level2Type); Long minId = sysFileService.getMinIdByLevel2Type(level2Type); Long sysFileId = null; do { sysFileId = RandomsUtil.randomSysFile(minId, maxId + 1); } while (sysFileIds.contains(sysFileId)); sysFileIds.add(sysFileId); return sysFileService.getById(sysFileId); } private SysFileVO getRandomSysFileByAttributeRetrieval(List level2TypeList,List sysFileVo,List sysFileIds) { sysFileVo = sysFileVo.stream().filter( f ->level2TypeList.contains(f.getLevel2Type()) && ( !sysFileIds.contains(f.getId())) ) .collect(Collectors.toList()); Long maxId = (long) sysFileVo.size(); Long minId = 0L; Long key = null; Long sysFileId = null; do { key = RandomsUtil.randomSysFile(minId, maxId); sysFileId = sysFileVo.get(key.intValue()).getId(); } while (sysFileIds.contains(sysFileId)); sysFileIds.add(sysFileId); return sysFileVo.get(key.intValue()); } private String getRandomColor(List colorBoards) { if (colorBoards.size() == 1) { return colorBoards.get(0).getRgbValue(); } long maxColor = colorBoards.size(); long minColor = 0; Long randomIndex = RandomsUtil.randomSysFile(minColor, maxColor); return colorBoards.get(randomIndex.intValue()).getRgbValue(); } private CollectionElement getRandomSketchLibrary(List sketchBoardElementOlds, List hasUseMd5List) { if (CollectionUtil.isEmpty(sketchBoardElementOlds)) { return null; } List sketchBoardElements = sketchBoardElementOlds.stream().filter(f -> !hasUseMd5List.contains(f.getMd5())).collect(Collectors.toList()); if (CollectionUtils.isEmpty(sketchBoardElements)) { return null; } if (sketchBoardElements.size() == 1) { return sketchBoardElements.get(0); } long maxSketch = sketchBoardElements.size(); long minSketch = 0; Long randomIndex = RandomsUtil.randomSysFile(minSketch, maxSketch); return sketchBoardElements.get(randomIndex.intValue()); } private DesignPythonItemPrint getRandomPrint(ValidateElementVO elementVO, CurrentDesignPrintPictureTypeEnum designPrintPictureType) { //print 不取library的 只取element的 DesignPythonItemPrint print = new DesignPythonItemPrint(); if (CollectionUtil.isEmpty(elementVO.getPrintBoardElements())) { print.setPath("none"); return print; } List printBoardElements = elementVO.getPrintBoardElements() .stream() .filter(f -> !elementVO.getHasUseMd5List().contains(f.getMd5())).collect(Collectors.toList()); if (CollectionUtil.isEmpty(printBoardElements)) { print.setPath("none"); return print; } switch (designPrintPictureType) { case NO: //print 已用完 print.setPath("none"); return print; case NO_PIN: printBoardElements = printBoardElements.stream().filter(f -> f.getHasPin() == 0).collect(Collectors.toList()); break; case PIN: printBoardElements = printBoardElements.stream().filter(f -> f.getHasPin() == 1).collect(Collectors.toList()); break; default: } if (printBoardElements.size() == 1) { print.setPath(printBoardElements.get(0).getUrl()); elementVO.getHasUseMd5List().add(printBoardElements.get(0).getMd5()); return print; } long maxPrint = printBoardElements.size(); long minPrint = 0; Long randomIndex = RandomsUtil.randomSysFile(minPrint, maxPrint); print.setPath(printBoardElements.get(randomIndex.intValue()).getUrl()); elementVO.getHasUseMd5List().add(printBoardElements.get(randomIndex.intValue()).getMd5()); return print; } private LibraryVo getRandomLibrary(List libraryOldVos, List containLevel2Type, List hasUseMd5List) { if (CollectionUtils.isEmpty(libraryOldVos)) { return null; } List libraryVos = libraryOldVos.stream().filter(f -> (!hasUseMd5List.contains(f.getMd5())) && CollectionLevel1TypeEnum.SKETCH_BOARD.getRealName().equals(f.getLevel1Type())).collect(Collectors.toList()); if (CollectionUtils.isEmpty(libraryVos)) { return null; } if (StringUtils.isEmpty(containLevel2Type)) { if (libraryVos.size() == 1) { return libraryVos.get(0); } long maxLibrary = libraryVos.size(); long minLibrary = 0; Long randomIndex = RandomsUtil.randomSysFile(minLibrary, maxLibrary); return libraryVos.get(randomIndex.intValue()); } else { List filter = libraryVos.stream().filter(f -> containLevel2Type.contains(f.getLevel2Type())).collect(Collectors.toList()); if (CollectionUtils.isEmpty(filter)) { return null; } if (filter.size() == 1) { return filter.get(0); } long maxLibrary = filter.size(); long minLibrary = 0; Long randomIndex = RandomsUtil.randomSysFile(minLibrary, maxLibrary); return filter.get(randomIndex.intValue()); } } /** * 生成design服装 * * @param designPythonObjects * @return */ public Boolean design(DesignPythonObjects designPythonObjects) { //限流校验 AccessLimitUtils.validate("design",5); 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"); //关闭FastJson的引用检测 防止出现$ref 现象 String param = JSON.toJSONString(designPythonObjects, SerializerFeature.DisableCircularReferenceDetect); log.info("design请求python 参数:####{}", param); RequestBody body = RequestBody.create(mediaType, param); Request request = new Request.Builder() .url("http://18.167.251.121:9991") // .url(accessPythonIp + ":10200/aifda/api/v1.0/generate") .method("POST", body) .addHeader("Authorization", "Basic YWlkbGFiOjEyMw==") .addHeader("Content-Type", "application/json") .build(); Response response = null; try { response = client.newCall(request).execute(); log.info("PythonService##response###{}", response); log.info("PythonService##responseBodyStr###{}", response.body().string()); log.info("PythonService##responseBodyJson###{}", JSON.toJSONString(response.body())); } catch (IOException ioException) { log.error("PythonService##design异常###{}", ExceptionUtil.getThrowableList(ioException)); } //去除限流 AccessLimitUtils.validateOut("design"); if (Objects.isNull(response)) { log.error("PythonService##design异常###{}", "response or body is empty!"); throw new BusinessException("system error!"); } JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(response)); log.info("PythonService##jsonObject###{}", jsonObject); Boolean result = jsonObject.getBoolean("successful"); if (result) { return Boolean.TRUE; } log.info("PythonService##design异常jsonObject###{}", jsonObject); //生成失败 throw new BusinessException("generate design exception!"); } /** * 生成高级图片 * * @param designPath * @return */ public String generateHighDesign(String designPath) { //限流校验 AccessLimitUtils.validate("generateHighDesign",2); OkHttpClient client = new OkHttpClient().newBuilder() .connectTimeout(30, TimeUnit.SECONDS) .pingInterval(5, TimeUnit.SECONDS)//websocket轮训间隔(单位:秒) .readTimeout(60, TimeUnit.SECONDS)//读取超时(单位:秒) .writeTimeout(60, TimeUnit.SECONDS)//写入超时(单位:秒) .build(); Map content = Maps.newHashMap(); content.put("img_path", designPath); content.put("img_path_out_put", getPythonOutputPath( designPath, PythonToJavaApiOperationTypeEnum.GENERATE_ADVANCED_DESIGN)); MediaType mediaType = MediaType.parse("application/json"); RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(content)); Request request = new Request.Builder() .url(accessPythonIp + ":11111/aifda/api/v1.0/sketch_to_real") .method("POST", body) .addHeader("Authorization", "Basic YWlkbGFiOjEyMw==") .addHeader("Content-Type", "application/json") .build(); Response response = null; try { log.info("生成高级design请求入参content###{}", JSON.toJSONString(content)); response = client.newCall(request).execute(); } catch (IOException ioException) { log.error("PythonService##generateHighDesign异常###{}", ExceptionUtil.getThrowableList(ioException)); } //去除限流 AccessLimitUtils.validateOut("generateHighDesign"); if (Objects.isNull(response)) { log.error("PythonService##generateHighDesign异常###{}", "response or body is empty!"); throw new BusinessException("system error!"); } JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(response)); Boolean result = jsonObject.getBoolean("successful"); if (result) { return content.get("img_path_out_put"); } log.info("生成高级design失败###{}", jsonObject); //生成失败 throw new BusinessException("system error!"); } /** * 生成 attribute_retrieval * * @param sketchUrlList * @return */ public DesignAttributeRetrievalDTO generateAttributeRetrieval(List sketchUrlList, Long userId) { //限流校验 AccessLimitUtils.validate("generateAttributeRetrieval",4); OkHttpClient client = new OkHttpClient().newBuilder() .connectTimeout(30, TimeUnit.SECONDS) .pingInterval(5, TimeUnit.SECONDS)//websocket轮训间隔(单位:秒) .readTimeout(60, TimeUnit.SECONDS)//读取超时(单位:秒) .writeTimeout(60, TimeUnit.SECONDS)//写入超时(单位:秒) .build(); Map content = Maps.newHashMap(); content.put("sketch_upload_img_path", sketchUrlList); content.put("userid", userId); MediaType mediaType = MediaType.parse("application/json"); RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(content)); Request request = new Request.Builder() .url(accessPythonIp+":11112/aifda/api/v1.0/attribute_retrieval") .method("POST", body) .addHeader("Authorization", "Basic YWlkbGFiOjEyMw==") .addHeader("Content-Type", "application/json") .build(); Response response = null; String bodyStr = null; try { log.info("generateAttributeRetrieval请求入参content###{}", JSON.toJSONString(content)); response = client.newCall(request).execute(); bodyStr = response.body().string(); } catch (IOException ioException) { log.error("generateAttributeRetrieval异常###{}", ExceptionUtil.getThrowableList(ioException)); } //去除限流 AccessLimitUtils.validateOut("generateAttributeRetrieval"); if (Objects.isNull(response)) { log.error("generateAttributeRetrieval异常###{}", "response or body is empty!"); throw new BusinessException("system error!"); } JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(response)); Boolean result = jsonObject.getBoolean("successful"); if (result) { return resolveDesignAttributeRetrievalDTO(bodyStr); } log.info("attribute_retrieval失败###{}", bodyStr); //生成失败 throw new BusinessException("system error!"); } private static DesignAttributeRetrievalDTO resolveDesignAttributeRetrievalDTO(String bodyStr){ DesignAttributeRetrievalDTO response = new DesignAttributeRetrievalDTO(); JSONObject jsonObject = JSON.parseObject(bodyStr); JSONObject jsonObjectSys = jsonObject.getJSONObject("sys_lib_dict"); if(null == jsonObjectSys || jsonObjectSys.size() == 0){ log.error("generateAttributeRetrieval异常###{}", "jsonObjectSys is empty!"); throw new BusinessException("system error!"); } setUrls(jsonObjectSys,response.getSysFileUrlS()); JSONObject jsonObjectLibrary = jsonObject.getJSONObject("user_lib_dict"); if(null == jsonObjectLibrary || jsonObjectLibrary.size() == 0){ log.error("generateAttributeRetrieval异常###{}", "jsonObjectLibrary is empty!"); throw new BusinessException("system error!"); } setUrls(jsonObjectLibrary,response.getLibraryUrls()); return response; } private static void setUrls(JSONObject jsonObjectSys,List urls){ List finalUrls = urls; SysFileLevel2TypeEnum.ofPythonPath().forEach(path ->{ JSONArray array = jsonObjectSys.getJSONArray(path); List urlList = JSONObject.parseArray(JSON.toJSONString(array),String.class); if(CollectionUtil.isNotEmpty(urlList)){ finalUrls.addAll(urlList); } }); } /** * 组装和计算designSingle参数 * * @param designSingleDTO * @return */ public DesignPythonObjects covertDesignSingleParam(DesignSingleDTO designSingleDTO, String singleOverall, String switchCategory, DesignLibraryModelPointVO designLibraryModelPoint) { DesignPythonObjects designPythonObjects = new DesignPythonObjects(); List objects = Lists.newArrayList(); designPythonObjects.setObjects(objects); DesignPythonObject pythonObject = new DesignPythonObject(); pythonObject.setItems(coverToDesignSinglePythonItem(designSingleDTO, designLibraryModelPoint)); pythonObject.setBasic(coverToSingleBasic(pythonObject.getItems().get(0), singleOverall, switchCategory, designSingleDTO.getPriority(), designLibraryModelPoint)); objects.add(pythonObject); return designPythonObjects; } private List coverToDesignSinglePythonItem(DesignSingleDTO designSingleDTO, DesignLibraryModelPointVO designLibraryModelPoint) { List designPythonItemDto = designSingleDTO.getClothes(); if (!CollectionUtils.isEmpty(designSingleDTO.getOthers())) { designPythonItemDto.addAll(designSingleDTO.getOthers()); } List response = CopyUtil.copyList(designPythonItemDto, DesignPythonItem.class, (o, d) -> { d.setBusinessId(o.getId()); d.setIcon("none"); d.setPrint(resolveDesignSinglePrint(o.getPrintObject(), o.getPath())); }); DesignPythonItem body = new DesignPythonItem(); body.setType(SysFileLevel2TypeEnum.BODY.getRealName()); if (Objects.nonNull(designLibraryModelPoint)) { body.setBody_path(designLibraryModelPoint.getTemplateUrl()); } else { // body.setBody_path("/home/pangkaicheng/python_code/Multi-layer-Virtual-Try-on/dataset_for_test/Img_model.png"); body.setBody_path("/workspace/python_code/Multi-layer-Virtual-Try-on/dataset_for_test/Img_model.png"); } response.add(body); return response; } private DesignPythonItemPrint resolveDesignSinglePrint(DesignPythonItemPrint printObject, String clothesPath) { if (Objects.isNull(printObject)) { return null; } DesignPythonItemPrint print = CopyUtil.copyObject(printObject, DesignPythonItemPrint.class); if(StringUtils.isEmpty(printObject.getPath())){ print.setPath("none"); } if (StringUtils.isEmpty(clothesPath) || "none".equals(clothesPath) || CollectionUtils.isEmpty(printObject.getLocation())) { return print; } //图片宽 高 FileVO fileVO = FileUtil.getFileSize(FileUtil.getOriginFile(clothesPath)); List> locations = printObject.getLocation(); locations.forEach(location -> { location.set(0, location.get(0) * fileVO.getWidth()); location.set(1, location.get(1) * fileVO.getHigh()); }); log.info("本次print打点locations###{}###fileVO{}", locations, JSON.toJSONString(fileVO)); return print; } private DesignPythonBasic coverToSingleBasic(DesignPythonItem designPythonItem, String singleOverall, String switchCategory, List priority, DesignLibraryModelPointVO designLibraryModelPoint) { DesignPythonBasic basic = new DesignPythonBasic(); basic.setSingle_overall(singleOverall); basic.setSwitch_category(switchCategory); basic.setSave_name(getPythonOutputPath( StringUtils.isEmpty(designPythonItem.getPath()) ? designPythonItem.getBody_path() : designPythonItem.getPath(), PythonToJavaApiOperationTypeEnum.DESIGN_COLLECTION)); basic.setPriority(priority); basic.setSelf_template(Boolean.FALSE); if (Objects.nonNull(designLibraryModelPoint)) { basic.setSelf_template(Boolean.TRUE); } basic.setScale_bag(0.7); basic.setScale_earrings(0.16); basic.setBody_point_test(getMap(designLibraryModelPoint)); return basic; } /** * 组装和计算designSingle参数 * * @param modelsDotDTO * @return */ public DesignPythonObjects covertModelsDotParam(ModelsDotDTO modelsDotDTO, String singleOverall, String switchCategory) { DesignPythonObjects designPythonObjects = new DesignPythonObjects(); List objects = Lists.newArrayList(); designPythonObjects.setObjects(objects); DesignPythonObject pythonObject = new DesignPythonObject(); pythonObject.setItems(coverToModelsDotPythonItem(modelsDotDTO)); pythonObject.setBasic(coverToModelsDotBasic(pythonObject.getItems().get(0), modelsDotDTO)); objects.add(pythonObject); return designPythonObjects; } private List coverToModelsDotPythonItem(ModelsDotDTO modelsDotDTO) { List response = Lists.newArrayList(); DesignPythonItem dress = new DesignPythonItem(); dress.setType(SysFileLevel2TypeEnum.DRESS.getRealName()); dress.setColor("none"); dress.setIcon("none"); dress.setPrint(new DesignPythonItemPrint("none", CollectionLevel1TypeEnum.PRINT_BOARD.getRealName(), 0.3f, Boolean.FALSE)); dress.setPath("https://www.aida.com.hk/download/sys/images/blouse/blouse_p5_817.jpg"); response.add(dress); DesignPythonItem skirt = new DesignPythonItem(); skirt.setType(SysFileLevel2TypeEnum.SKIRT.getRealName()); skirt.setColor("none"); skirt.setIcon("none"); skirt.setPrint(new DesignPythonItemPrint("none", CollectionLevel1TypeEnum.PRINT_BOARD.getRealName(), 0.3f, Boolean.FALSE)); skirt.setPath("https://www.aida.com.hk/download/sys/images/trousers/trousers_974.jpg"); response.add(skirt); DesignPythonItem body = new DesignPythonItem(); body.setType(SysFileLevel2TypeEnum.BODY.getRealName()); body.setBody_path(modelsDotDTO.getTemplateUrl()); response.add(body); return response; } private DesignPythonBasic coverToModelsDotBasic(DesignPythonItem designPythonItem, ModelsDotDTO modelsDotDTO) { DesignPythonBasic basic = new DesignPythonBasic(); basic.setSingle_overall(SingleOverallEnum.OVERALL.getRealName()); basic.setSwitch_category(""); basic.setSave_name(getPythonOutputPath( StringUtils.isEmpty(designPythonItem.getPath()) ? designPythonItem.getBody_path() : designPythonItem.getPath(), PythonToJavaApiOperationTypeEnum.DESIGN_COLLECTION)); basic.setSelf_template(Boolean.TRUE); basic.setScale_bag(0.7); basic.setScale_earrings(0.16); basic.setBody_point_test(getMap(calculateTemplatePoint(modelsDotDTO, modelsDotDTO.getHigh(), modelsDotDTO.getWidth()))); return basic; } public DesignLibraryModelPointVO calculateTemplatePoint(ModelsDotDTO modelsDotDTO, Integer high, Integer width) { DesignLibraryModelPointVO libraryModelPoint = new DesignLibraryModelPointVO(); libraryModelPoint.setHandLeft(calculateTemplatePointOne(modelsDotDTO.getHandLeft(), high, width)); libraryModelPoint.setHandRight(calculateTemplatePointOne(modelsDotDTO.getHandRight(), high, width)); libraryModelPoint.setShoulderLeft(calculateTemplatePointOne(modelsDotDTO.getShoulderLeft(), high, width)); libraryModelPoint.setShoulderRight(calculateTemplatePointOne(modelsDotDTO.getShoulderRight(), high, width)); libraryModelPoint.setWaistbandLeft(calculateTemplatePointOne(modelsDotDTO.getWaistbandLeft(), high, width)); libraryModelPoint.setWaistbandRight(calculateTemplatePointOne(modelsDotDTO.getWaistbandRight(), high, width)); return libraryModelPoint; } private List calculateTemplatePointOne(List originRatioList, Integer high, Integer width) { originRatioList.set(0, originRatioList.get(0).multiply(BigDecimal.valueOf(width))); originRatioList.set(1, originRatioList.get(1).multiply(BigDecimal.valueOf(high))); return originRatioList; } public JSONObject designNew(DesignPythonObjects designPythonObjects) { //限流校验 AccessLimitUtils.validate("design",5); 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"); //关闭FastJson的引用检测 防止出现$ref 现象 // String param = JSON.toJSONString(designPythonObjects, SerializerFeature.DisableCircularReferenceDetect); String param = "{\"objects\":[{\"basic\":{\"body_point_test\":{\"shoulder_left\":[755,519],\"toe_right\":[850,1355],\"ear_point_left\":[802,439],\"shoulder_right\":[912,519],\"waistband_right\":[891,725],\"hand_point_right\":[915,886],\"waistband_left\":[777,725],\"hand_point_left\":[754,886],\"head_point_left\":[804,424],\"head_point_right\":[864,424],\"ear_point_right\":[865,438],\"toe_left\":[817,1355],\"foot_length\":[784,1336,825,1336],\"head_point_up\":[834,390]},\"scale_bag\":0.7,\"scale_earrings\":0.16,\"self_template\":false,\"single_overall\":\"overall\",\"switch_category\":\"\"},\"items\":[{\"color\":\"50 187 147\",\"icon\":\"none\",\"path\":\"https://www.aida.com.hk/download/202308/userFile/collection/Sketchboard/42/048419db-38c4-4aee-85a7-33f0666b18d2.jpg\",\"print\":{\"path\":\"https://www.aida.com.hk/download/202304/pythonFile/generatePrint/generatePrint-d7a67a96-83f9-429f-9028-3e1697501088.jpg\"},\"type\":\"Outwear\",\"image_id\":53547},{\"color\":\"137 50 50\",\"icon\":\"none\",\"path\":\"https://www.aida.com.hk/download/sys/images/skirt/0628001153.jpg\",\"print\":{\"path\":\"none\"},\"type\":\"Skirt\",\"image_id\":53548},{\"path\":\"https://www.aida.com.hk/download/sys/accessories/hairstyle/hairstyle_242.jpg\",\"type\":\"Hairstyle\",\"image_id\":411},{\"color\":\"31 76 171\",\"path\":\"https://www.aida.com.hk/download/sys/accessories/shoes/shoes_11.jpg\",\"type\":\"Shoes\",\"image_id\":167},{\"path\":\"https://www.aida.com.hk/download/sys/accessories/earring/earrings_12.png\",\"type\":\"Earring\",\"image_id\":93},{\"body_path\":\"model_1693218345.2714431.png\",\"type\":\"Body\",\"image_id\":0}]},{\"basic\":{\"body_point_test\":{\"shoulder_left\":[755,519],\"toe_right\":[850,1355],\"ear_point_left\":[802,439],\"shoulder_right\":[912,519],\"waistband_right\":[891,725],\"hand_point_right\":[915,886],\"waistband_left\":[777,725],\"hand_point_left\":[754,886],\"head_point_left\":[804,424],\"head_point_right\":[864,424],\"ear_point_right\":[865,438],\"toe_left\":[817,1355],\"foot_length\":[784,1336,825,1336],\"head_point_up\":[834,390]},\"scale_bag\":0.7,\"scale_earrings\":0.16,\"self_template\":false,\"single_overall\":\"overall\",\"switch_category\":\"\"},\"items\":[{\"color\":\"103 74 74\",\"icon\":\"none\",\"path\":\"https://www.aida.com.hk/download/sys/images/dress/0902005000.jpg\",\"print\":{\"path\":\"https://www.aida.com.hk/download/202304/userFile/library/Printboard/42/4ba535c2-a08b-4114-826b-a87885fd3623Fabric-03.png\"},\"type\":\"Dress\",\"image_id\":53549},{\"color\":\"204 182 182\",\"icon\":\"none\",\"path\":\"https://www.aida.com.hk/download/sys/images/skirt/0825000582.jpg\",\"print\":{\"path\":\"https://www.aida.com.hk/download/202304/userFile/library/Printboard/42/4ba535c2-a08b-4114-826b-a87885fd3623Fabric-03.png\"},\"type\":\"Skirt\",\"image_id\":53550},{\"path\":\"https://www.aida.com.hk/download/sys/accessories/hairstyle/hairstyle_p2529.jpg\",\"type\":\"Hairstyle\",\"image_id\":898},{\"color\":\"137 50 50\",\"path\":\"https://www.aida.com.hk/download/sys/accessories/shoes/shoes_15.png\",\"type\":\"Shoes\",\"image_id\":171},{\"path\":\"https://www.aida.com.hk/download/sys/accessories/earring/earrings_15.png\",\"type\":\"Earring\",\"image_id\":96},{\"body_path\":\"model_1693218345.2714431.png\",\"type\":\"Body\",\"image_id\":0}]},{\"basic\":{\"body_point_test\":{\"shoulder_left\":[755,519],\"toe_right\":[850,1355],\"ear_point_left\":[802,439],\"shoulder_right\":[912,519],\"waistband_right\":[891,725],\"hand_point_right\":[915,886],\"waistband_left\":[777,725],\"hand_point_left\":[754,886],\"head_point_left\":[804,424],\"head_point_right\":[864,424],\"ear_point_right\":[865,438],\"toe_left\":[817,1355],\"foot_length\":[784,1336,825,1336],\"head_point_up\":[834,390]},\"scale_bag\":0.7,\"scale_earrings\":0.16,\"self_template\":false,\"single_overall\":\"overall\",\"switch_category\":\"\"},\"items\":[{\"color\":\"49 127 162\",\"icon\":\"none\",\"path\":\"https://www.aida.com.hk/download/sys/images/outwear/0628001185.jpg\",\"print\":{\"path\":\"https://www.aida.com.hk/download/202308/pythonFile/generatePrint/generatePrint-487e93e6-1e21-46b8-b821-3188251dbdbc.jpg\"},\"type\":\"Outwear\",\"image_id\":53551},{\"color\":\"103 74 74\",\"icon\":\"none\",\"path\":\"https://www.aida.com.hk/download/sys/images/trousers/0628000635.jpg\",\"print\":{\"path\":\"https://www.aida.com.hk/download/202308/pythonFile/generatePrint/generatePrint-487e93e6-1e21-46b8-b821-3188251dbdbc.jpg\"},\"type\":\"Trousers\",\"image_id\":53552},{\"path\":\"https://www.aida.com.hk/download/sys/accessories/hairstyle/hairstyle_p2102.jpg\",\"type\":\"Hairstyle\",\"image_id\":471},{\"color\":\"137 50 50\",\"path\":\"https://www.aida.com.hk/download/sys/accessories/shoes/shoes_41.jpg\",\"type\":\"Shoes\",\"image_id\":344},{\"path\":\"https://www.aida.com.hk/download/sys/accessories/earring/earrings_5.png\",\"type\":\"Earring\",\"image_id\":87},{\"body_path\":\"model_1693218345.2714431.png\",\"type\":\"Body\",\"image_id\":0}]},{\"basic\":{\"body_point_test\":{\"shoulder_left\":[755,519],\"toe_right\":[850,1355],\"ear_point_left\":[802,439],\"shoulder_right\":[912,519],\"waistband_right\":[891,725],\"hand_point_right\":[915,886],\"waistband_left\":[777,725],\"hand_point_left\":[754,886],\"head_point_left\":[804,424],\"head_point_right\":[864,424],\"ear_point_right\":[865,438],\"toe_left\":[817,1355],\"foot_length\":[784,1336,825,1336],\"head_point_up\":[834,390]},\"scale_bag\":0.7,\"scale_earrings\":0.16,\"self_template\":false,\"single_overall\":\"overall\",\"switch_category\":\"\"},\"items\":[{\"color\":\"31 76 171\",\"icon\":\"none\",\"path\":\"https://www.aida.com.hk/download/sys/images/outwear/0902002198.jpg\",\"print\":{\"path\":\"none\"},\"type\":\"Outwear\",\"image_id\":53553},{\"color\":\"50 187 147\",\"icon\":\"none\",\"path\":\"https://www.aida.com.hk/download/sys/images/skirt/0916000791.jpg\",\"print\":{\"path\":\"none\"},\"type\":\"Skirt\",\"image_id\":53554},{\"path\":\"https://www.aida.com.hk/download/sys/accessories/hairstyle/hairstyle_p2518.jpg\",\"type\":\"Hairstyle\",\"image_id\":887},{\"color\":\"137 50 50\",\"path\":\"https://www.aida.com.hk/download/sys/accessories/shoes/shoes_15.png\",\"type\":\"Shoes\",\"image_id\":171},{\"path\":\"https://www.aida.com.hk/download/sys/accessories/earring/earrings_11.png\",\"type\":\"Earring\",\"image_id\":92},{\"body_path\":\"model_1693218345.2714431.png\",\"type\":\"Body\",\"image_id\":0}]},{\"basic\":{\"body_point_test\":{\"shoulder_left\":[755,519],\"toe_right\":[850,1355],\"ear_point_left\":[802,439],\"shoulder_right\":[912,519],\"waistband_right\":[891,725],\"hand_point_right\":[915,886],\"waistband_left\":[777,725],\"hand_point_left\":[754,886],\"head_point_left\":[804,424],\"head_point_right\":[864,424],\"ear_point_right\":[865,438],\"toe_left\":[817,1355],\"foot_length\":[784,1336,825,1336],\"head_point_up\":[834,390]},\"scale_bag\":0.7,\"scale_earrings\":0.16,\"self_template\":false,\"single_overall\":\"overall\",\"switch_category\":\"\"},\"items\":[{\"color\":\"204 182 182\",\"icon\":\"none\",\"path\":\"https://www.aida.com.hk/download/sys/images/outwear/0902000892.jpg\",\"print\":{\"path\":\"none\"},\"type\":\"Outwear\",\"image_id\":53555},{\"color\":\"204 182 182\",\"icon\":\"none\",\"path\":\"https://www.aida.com.hk/download/sys/images/trousers/0916000460.jpg\",\"print\":{\"path\":\"none\"},\"type\":\"Trousers\",\"image_id\":53556},{\"path\":\"https://www.aida.com.hk/download/sys/accessories/hairstyle/hairstyle_204.jpg\",\"type\":\"Hairstyle\",\"image_id\":294},{\"color\":\"49 127 162\",\"path\":\"https://www.aida.com.hk/download/sys/accessories/shoes/shoes_15.png\",\"type\":\"Shoes\",\"image_id\":171},{\"path\":\"https://www.aida.com.hk/download/sys/accessories/earring/earrings_5.png\",\"type\":\"Earring\",\"image_id\":87},{\"body_path\":\"model_1693218345.2714431.png\",\"type\":\"Body\",\"image_id\":0}]},{\"basic\":{\"body_point_test\":{\"shoulder_left\":[755,519],\"toe_right\":[850,1355],\"ear_point_left\":[802,439],\"shoulder_right\":[912,519],\"waistband_right\":[891,725],\"hand_point_right\":[915,886],\"waistband_left\":[777,725],\"hand_point_left\":[754,886],\"head_point_left\":[804,424],\"head_point_right\":[864,424],\"ear_point_right\":[865,438],\"toe_left\":[817,1355],\"foot_length\":[784,1336,825,1336],\"head_point_up\":[834,390]},\"scale_bag\":0.7,\"scale_earrings\":0.16,\"self_template\":false,\"single_overall\":\"overall\",\"switch_category\":\"\"},\"items\":[{\"color\":\"204 182 182\",\"icon\":\"none\",\"path\":\"https://www.aida.com.hk/download/sys/images/blouse/0902002193.jpg\",\"print\":{\"path\":\"none\"},\"type\":\"Blouse\",\"image_id\":53557},{\"color\":\"50 187 147\",\"icon\":\"none\",\"path\":\"https://www.aida.com.hk/download/sys/images/skirt/0902002112.jpg\",\"print\":{\"path\":\"none\"},\"type\":\"Skirt\",\"image_id\":53558},{\"path\":\"https://www.aida.com.hk/download/sys/accessories/hairstyle/hairstyle_p2186.jpg\",\"type\":\"Hairstyle\",\"image_id\":555},{\"color\":\"204 182 182\",\"path\":\"https://www.aida.com.hk/download/sys/accessories/shoes/shoes_11.jpg\",\"type\":\"Shoes\",\"image_id\":167},{\"path\":\"https://www.aida.com.hk/download/sys/accessories/earring/earrings_12.png\",\"type\":\"Earring\",\"image_id\":93},{\"body_path\":\"model_1693218345.2714431.png\",\"type\":\"Body\",\"image_id\":0}]},{\"basic\":{\"body_point_test\":{\"shoulder_left\":[755,519],\"toe_right\":[850,1355],\"ear_point_left\":[802,439],\"shoulder_right\":[912,519],\"waistband_right\":[891,725],\"hand_point_right\":[915,886],\"waistband_left\":[777,725],\"hand_point_left\":[754,886],\"head_point_left\":[804,424],\"head_point_right\":[864,424],\"ear_point_right\":[865,438],\"toe_left\":[817,1355],\"foot_length\":[784,1336,825,1336],\"head_point_up\":[834,390]},\"scale_bag\":0.7,\"scale_earrings\":0.16,\"self_template\":false,\"single_overall\":\"overall\",\"switch_category\":\"\"},\"items\":[{\"color\":\"204 182 182\",\"icon\":\"none\",\"path\":\"https://www.aida.com.hk/download/sys/images/blouse/0902002193.jpg\",\"print\":{\"path\":\"none\"},\"type\":\"Blouse\",\"image_id\":53557},{\"color\":\"137 50 50\",\"icon\":\"none\",\"path\":\"https://www.aida.com.hk/download/sys/images/skirt/0902002861.jpg\",\"print\":{\"path\":\"none\"},\"type\":\"Skirt\",\"image_id\":53560},{\"path\":\"https://www.aida.com.hk/download/sys/accessories/hairstyle/hairstyle_p2128.jpg\",\"type\":\"Hairstyle\",\"image_id\":497},{\"color\":\"49 127 162\",\"path\":\"https://www.aida.com.hk/download/sys/accessories/shoes/shoes_39.jpg\",\"type\":\"Shoes\",\"image_id\":192},{\"path\":\"https://www.aida.com.hk/download/sys/accessories/earring/earrings_4.png\",\"type\":\"Earring\",\"image_id\":86},{\"body_path\":\"model_1693218345.2714431.png\",\"type\":\"Body\",\"image_id\":0}]},{\"basic\":{\"body_point_test\":{\"shoulder_left\":[755,519],\"toe_right\":[850,1355],\"ear_point_left\":[802,439],\"shoulder_right\":[912,519],\"waistband_right\":[891,725],\"hand_point_right\":[915,886],\"waistband_left\":[777,725],\"hand_point_left\":[754,886],\"head_point_left\":[804,424],\"head_point_right\":[864,424],\"ear_point_right\":[865,438],\"toe_left\":[817,1355],\"foot_length\":[784,1336,825,1336],\"head_point_up\":[834,390]},\"scale_bag\":0.7,\"scale_earrings\":0.16,\"self_template\":false,\"single_overall\":\"overall\",\"switch_category\":\"\"},\"items\":[{\"color\":\"204 182 182\",\"icon\":\"none\",\"path\":\"https://www.aida.com.hk/download/sys/images/blouse/0902002193.jpg\",\"print\":{\"path\":\"none\"},\"type\":\"Blouse\",\"image_id\":53557},{\"color\":\"49 127 162\",\"icon\":\"none\",\"path\":\"https://www.aida.com.hk/download/sys/images/skirt/0902000567.jpg\",\"print\":{\"path\":\"none\"},\"type\":\"Skirt\",\"image_id\":53562},{\"path\":\"https://www.aida.com.hk/download/sys/accessories/hairstyle/hairstyle_p2357.jpg\",\"type\":\"Hairstyle\",\"image_id\":726},{\"color\":\"137 50 50\",\"path\":\"https://www.aida.com.hk/download/sys/accessories/shoes/shoes_6.jpg\",\"type\":\"Shoes\",\"image_id\":163},{\"path\":\"https://www.aida.com.hk/download/sys/accessories/earring/earrings_15.png\",\"type\":\"Earring\",\"image_id\":96},{\"body_path\":\"model_1693218345.2714431.png\",\"type\":\"Body\",\"image_id\":0}]}]}"; log.info("design请求python 参数:####{}", param); RequestBody body = RequestBody.create(mediaType, param); Request request = new Request.Builder() .url("http://18.167.251.121:9991") // .url(accessPythonIp + ":10200/aifda/api/v1.0/generate") .method("POST", body) .addHeader("Authorization", "Basic YWlkbGFiOjEyMw==") .addHeader("Content-Type", "application/json") .build(); Response response = null; try { response = client.newCall(request).execute(); // log.info("PythonService##response###{}", response); // log.info("PythonService##responseBodyStr###{}", response.body().string()); // log.info("PythonService##responseBodyJson###{}", JSON.toJSONString(response.body())); String responseBody = response.body().string(); JSONObject responseObject = JSON.parseObject(responseBody); return responseObject; } catch (IOException ioException) { log.error("PythonService##design异常###{}", ExceptionUtil.getThrowableList(ioException)); } //去除限流 AccessLimitUtils.validateOut("design"); if (Objects.isNull(response)) { log.error("PythonService##design异常###{}", "response or body is empty!"); throw new BusinessException("system error!"); } JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(response)); log.info("PythonService##jsonObject###{}", jsonObject); Boolean result = jsonObject.getBoolean("successful"); if (response.isSuccessful()) { try { String responseBody = response.body().string(); JSONObject responseObject = JSON.parseObject(responseBody); return responseObject; }catch (IOException e) { throw new RuntimeException(e); } } log.info("PythonService##design异常jsonObject###{}", jsonObject); //生成失败 throw new BusinessException("generate design exception!"); } }