rambo пре 11 месеци
родитељ
комит
ba78165581
2 измењених фајлова са 19 додато и 28 уклоњено
  1. 7 16
      python/api.py
  2. 12 12
      python/services/SegmentService.py

+ 7 - 16
python/api.py

@@ -20,6 +20,7 @@ async def checkSelect(params: CheckSelectImages):
     file_path = params.path
     path_type = params.path_type
     image_list = params.image_list
+    need_cutout_images = []
     if path_type == 0:
         need_cutout_images = service.initImages(image_list=image_list)
     else:
@@ -27,7 +28,6 @@ async def checkSelect(params: CheckSelectImages):
     # print("need_cutout_images",need_cutout_images)
     if len([x for x in need_cutout_images if x["need_cutout"]]) == 0:
         raise UnicornException("您所选文件夹下没有jpg/png图片,或对应图片已扣图")
-    service.need_cutout_images = []
     return success(need_cutout_images)
 
 @app.post("/api/segment_images", description="执行抠图操作")
@@ -40,11 +40,8 @@ async def segmentImages(params: SegmentImages):
     output_type = params.output_type
     need_cutout_images = params.need_cutout_images
     result = None
-    try:
-        if len([x for x in need_cutout_images if x["need_cutout"]]) == 0:
-            raise UnicornException("您所选文件夹下没有jpg/png图片,或对应图片已扣图")
-    except Exception as e:
-        raise UnicornException(repr(e))
+    if len([x for x in need_cutout_images if x["need_cutout"]]) == 0:
+        raise UnicornException("您所选文件夹下没有jpg/png图片,或对应图片已扣图")
     if image_type == 1 and segment_type == 1:
         raise UnicornException("服装暂不支持精细化抠图")
     elif image_type == 1 and segment_type == 0:
@@ -71,16 +68,10 @@ async def segmentImages(params: SegmentImages):
 @app.post("/api/model_form_segment", description="人台抠图")
 def model_form_segment(params:ModelFormModel):
     need_cutout_images = params.need_cutout_images
-    try:
-        if len([x for x in need_cutout_images if x["need_cutout"]]) == 0:
-            raise UnicornException("您所选文件夹下没有jpg/png图片,或对应图片已扣图")
-    except Exception as e:
-        raise UnicornException(repr(e))
-    try:
-        if len([x for x in need_cutout_images if x["need_cutout"]]) > 30:
-            raise UnicornException("最多允许上传30张图片")
-    except Exception as e:
-        raise UnicornException(repr(e))
+    if len([x for x in need_cutout_images if x["need_cutout"]]) == 0:
+        raise UnicornException("您所选文件夹下没有jpg/png图片,或对应图片已扣图")
+    if len([x for x in need_cutout_images if x["need_cutout"]]) > 30:
+        raise UnicornException("最多允许上传30张图片")
     modelFormClazz = DealModelForm(token=params.token,params=params)
     modelFormClazz.need_cutout_images = need_cutout_images
     result ,save_root_path,generate_ids=modelFormClazz.startDispose()

+ 12 - 12
python/services/SegmentService.py

@@ -5,12 +5,11 @@ from middleware import UnicornException
 class SegmentService:
     is_fall_file = ["扣图", "已扣图"]
     is_fall_dir = ["微信"]
-    need_cutout_images = []
-    def initImages(self, image_list):
+    def initImages(self, image_list, need_cutout_images):
         for image_path in image_list:
             root_path, file = os.path.split(image_path)
             file_name, file_e = os.path.splitext(file)
-            self.need_cutout_images.append(
+            need_cutout_images.append(
                 {
                     "file_name": file_name,
                     "file_e": file_e,
@@ -21,19 +20,20 @@ class SegmentService:
                 }
             )
 
-        for index, image_data in enumerate(self.need_cutout_images):
+        for index, image_data in enumerate(need_cutout_images):
             root_path = image_data["root_path"]
             file_name = image_data["file_name"]
             _path_1 = "{}/{}.png".format(root_path, file_name)
             _path_2 = "{}/已扣图/{}.png".format(root_path, file_name)
             if os.path.exists(_path_1):
-                self.need_cutout_images[index]["need_cutout"] = False
+                need_cutout_images[index]["need_cutout"] = False
                 continue
             if os.path.exists(_path_2):
-                self.need_cutout_images[index]["need_cutout"] = False
+                need_cutout_images[index]["need_cutout"] = False
                 continue
-        return self.need_cutout_images
-    def check_need_cutout_images(self, root_path):
+        return need_cutout_images
+
+    def check_need_cutout_images(self, root_path, need_cutout_images):
         _n_c = 0
         _Type = [
             ".jpg",
@@ -49,7 +49,7 @@ class SegmentService:
         for file in os.listdir(root_path):
             _n_c += 1
             if _n_c > 500:
-                self.need_cutout_images = []
+                need_cutout_images = []
                 raise UnicornException("目录下文件过多,请检查目录是否正确")
             file_path = "{}/{}".format(root_path, file)
             if os.path.isdir(file_path):
@@ -73,7 +73,7 @@ class SegmentService:
                         break
                 if f:
                     print("遍历文件夹", file_path)
-                    self.check_need_cutout_images(file_path)
+                    self.check_need_cutout_images(file_path, need_cutout_images)
 
             file_name, file_e = os.path.splitext(file)
             if file_e not in _Type:
@@ -92,7 +92,7 @@ class SegmentService:
             #     need_cutout = False
 
             # 图片进行处理
-            self.need_cutout_images.append(
+            need_cutout_images.append(
                 {
                     "file_name": file_name,
                     "file_e": file_e,
@@ -102,4 +102,4 @@ class SegmentService:
                     "need_cutout": need_cutout,
                 }
             )
-        return self.need_cutout_images
+        return need_cutout_images