浏览代码

v4.7.1分割抠图

dsx 2 年之前
父节点
当前提交
1803277792

+ 22 - 4
src/main/java/com/fdkankan/scene/service/impl/SceneServiceImpl.java

@@ -1,5 +1,6 @@
 package com.fdkankan.scene.service.impl;
 
+import cn.hutool.core.img.ImgUtil;
 import cn.hutool.core.io.FileUtil;
 import cn.hutool.core.lang.UUID;
 import cn.hutool.core.util.StrUtil;
@@ -26,7 +27,9 @@ import org.springframework.stereotype.Service;
 import org.springframework.web.multipart.MultipartFile;
 import sun.rmi.runtime.Log;
 
+import java.awt.image.BufferedImage;
 import java.io.File;
+import java.math.BigDecimal;
 import java.util.Calendar;
 import java.util.HashMap;
 import java.util.Map;
@@ -68,12 +71,28 @@ public class SceneServiceImpl implements ISceneService {
         File tempFile = File.createTempFile(java.util.UUID.randomUUID().toString(), extName);
         file.transferTo(tempFile);
 
+        //校验像素
+        BufferedImage bufferedImage = ImgUtil.read(tempFile.getPath());
+        Float scale = 1F;
+        Float widthScale = 1F;
+        Float heightScale = 1F;
+        int width = bufferedImage.getWidth();
+        int height = bufferedImage.getHeight();
+        if(width > 2000){
+            widthScale = new BigDecimal(2000).divide(new BigDecimal(width)).setScale(5, BigDecimal.ROUND_DOWN).floatValue();
+        }
+        if(height > 2000){
+            heightScale = new BigDecimal(2000).divide(new BigDecimal(width)).setScale(5, BigDecimal.ROUND_DOWN).floatValue();
+        }
+        scale = widthScale > heightScale ? heightScale : widthScale;
+        ImgUtil.scale(new File(tempFile.getPath()), new File(tempFile.getPath()), scale);
+
         String orgImgOssPath = "body_segment/original/" + tempFile.getName();
         ossBodySegmentUtil.uploadOss(tempFile.getPath(), orgImgOssPath);
 //        fYunFileService.uploadFile(bodySegmentBucket, tempFile.getPath(), orgImgOssPath);
 
         BodySegmentStatusBean bodySegmentStatusBean = BodySegmentStatusBean.builder().uuid(uuid).status(CommonOperStatus.WAITING.code()).build();
-        redisUtil.hset(RedisKey.SCENE_BODY_SEGMENT, uuid, JSON.toJSONString(bodySegmentStatusBean));
+        redisUtil.set(String.format(RedisKey.SCENE_BODY_SEGMENT, uuid), JSON.toJSONString(bodySegmentStatusBean), RedisKey.CAMERA_EXPIRE_7_TIME);
 
         Map<String, String> map = new HashMap<>();
         map.put("uuid", uuid);
@@ -107,11 +126,10 @@ public class SceneServiceImpl implements ISceneService {
             fYunFileService.uploadFile(imgPath, targetOssImgPath);
             bodySegmentStatusBean.setStatus(CommonOperStatus.SUCCESS.code());
             bodySegmentStatusBean.setImageUrl(fYunFileConfig.getHost() + targetOssImgPath);
-            redisUtil.hset(RedisKey.SCENE_BODY_SEGMENT, uuid, JSON.toJSONString(bodySegmentStatusBean));
+            redisUtil.set(String.format(RedisKey.SCENE_BODY_SEGMENT, uuid), JSON.toJSONString(bodySegmentStatusBean), RedisKey.CAMERA_EXPIRE_7_TIME);
         } catch (Exception e) {
-            log.error("提取人体分割图片失败,imgUrl: "+imgUrl, e);
             bodySegmentStatusBean.setStatus(CommonOperStatus.FAILD.code());
-            redisUtil.hset(RedisKey.SCENE_BODY_SEGMENT, uuid, JSON.toJSONString(bodySegmentStatusBean));
+            redisUtil.set(String.format(RedisKey.SCENE_BODY_SEGMENT, uuid), JSON.toJSONString(bodySegmentStatusBean), RedisKey.CAMERA_EXPIRE_7_TIME);
         }finally {
             try {
                 //免费版qps不能大于2,故休眠一秒

+ 14 - 14
src/main/java/com/fdkankan/scene/util/OssBodySegmentUtil.java

@@ -57,20 +57,20 @@ public class OssBodySegmentUtil {
     }
 
     public void extracted(String imageUrl, String dir, String fileName) throws Exception {
-        com.aliyun.imageseg20191230.Client client = this.createClient();
-        com.aliyun.imageseg20191230.models.SegmentBodyRequest segmentBodyRequest =
-                new com.aliyun.imageseg20191230.models.SegmentBodyRequest().setImageURL(imageUrl);
-        com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
-        // 复制代码运行请自行打印 API 的返回值
-        SegmentBodyResponse segmentBodyResponse = client.segmentBodyWithOptions(segmentBodyRequest, runtime);
-        String imageURL = segmentBodyResponse.getBody().getData().getImageURL();
-        log.info("人体抠图imageURL:{}", imageURL);
-//        HttpUtil.downloadFile(imageURL, FileUtil.file(file));
-        myClient.downloadFile(imageURL, dir, fileName);
-
-//        FileUtils.downLoadFromUrl(imageURL, "1.jpg","D:\\test");
-//        byte[] aaa = FileUtils.getBytesFromUrl(imageURL);
-//        FileUtil.writeBytes(aaa, file);
+        try {
+            com.aliyun.imageseg20191230.Client client = this.createClient();
+            com.aliyun.imageseg20191230.models.SegmentBodyRequest segmentBodyRequest =
+                    new com.aliyun.imageseg20191230.models.SegmentBodyRequest().setImageURL(imageUrl);
+            com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
+            // 复制代码运行请自行打印 API 的返回值
+            SegmentBodyResponse segmentBodyResponse = client.segmentBodyWithOptions(segmentBodyRequest, runtime);
+            String imageURL = segmentBodyResponse.getBody().getData().getImageURL();
+            log.info("人体抠图imageURL:{}", imageURL);
+            myClient.downloadFile(imageURL, dir, fileName);
+        }catch (Exception e){
+            log.error("人体抠图失败,imageUrl:" + imageUrl, e);
+            myClient.downloadFile(imageUrl, dir, fileName);
+        }
 
     }