From 0d4d464e3f01cc0b3077555970f0b4a342452202 Mon Sep 17 00:00:00 2001 From: zhh Date: Fri, 26 Sep 2025 10:29:39 +0800 Subject: [PATCH] =?UTF-8?q?feat=EF=BC=88=E6=96=B0=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=EF=BC=89:=201=E3=80=81design-print=E4=B8=BA=E8=A7=A3=E5=86=B3s?= =?UTF-8?q?ketch=E5=8E=9F=E5=9B=BE=E5=A4=AA=E7=81=B0=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E5=8D=B0=E8=8A=B1=E9=A2=9C=E8=89=B2=E4=BE=BF=E6=9A=97=202?= =?UTF-8?q?=E3=80=81cv2.resize=20=20=E6=8F=92=E5=80=BC=E7=AE=97=E6=B3=95?= =?UTF-8?q?=E6=9B=B4=E6=8D=A2=EF=BC=8C=E6=8F=90=E5=8D=87resize=E5=90=8E?= =?UTF-8?q?=E5=9B=BE=E7=89=87=E8=B4=A8=E9=87=8F=20fix=EF=BC=88=E4=BF=AE?= =?UTF-8?q?=E5=A4=8Dbug=EF=BC=89:=20refactor=EF=BC=88=E9=87=8D=E6=9E=84?= =?UTF-8?q?=EF=BC=89:=20test(=E5=A2=9E=E5=8A=A0=E6=B5=8B=E8=AF=95):?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/service/design_fast/pipeline/loading.py | 33 ++++++++++++++++++++- app/service/design_fast/pipeline/split.py | 14 ++++----- 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/app/service/design_fast/pipeline/loading.py b/app/service/design_fast/pipeline/loading.py index 5a55d9d..329e14b 100644 --- a/app/service/design_fast/pipeline/loading.py +++ b/app/service/design_fast/pipeline/loading.py @@ -1,5 +1,6 @@ import io import logging +import os import cv2 import numpy as np @@ -38,12 +39,42 @@ class LoadImage: def __call__(self, result): result['image'], result['pre_mask'] = self.read_image(result['path']) - result['gray'] = cv2.cvtColor(result['image'], cv2.COLOR_BGR2GRAY) + # if 'extract_lines' in result.keys(): + # if result['extract_lines']: + # result['gray'] = self.get_lines(cv2.cvtColor(result['image'], cv2.COLOR_BGR2GRAY), result['path']) + # else: + # result['gray'] = cv2.cvtColor(result['image'], cv2.COLOR_BGR2GRAY) + # else: + # result['gray'] = cv2.cvtColor(result['image'], cv2.COLOR_BGR2GRAY) + + result['gray'] = self.get_lines(cv2.cvtColor(result['image'], cv2.COLOR_BGR2GRAY), result['path']) result['keypoint'] = self.get_keypoint(result['name']) result['img_shape'] = result['image'].shape result['ori_shape'] = result['image'].shape return result + def get_lines(self, img, path): + binary = cv2.adaptiveThreshold(img, 255, + cv2.ADAPTIVE_THRESH_GAUSSIAN_C, + cv2.THRESH_BINARY_INV, + 25, 10) + + # 步骤2:细化边缘(可选,让线条更干净) + # kernel = np.ones((1, 1), np.uint8) + # clean = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel) + + thinned = cv2.ximgproc.thinning(binary, thinningType=cv2.ximgproc.THINNING_ZHANGSUEN) # thinning算法细化线条 + mask = thinned > 0 + result = np.ones_like(img) * 255 + result[mask] = img[mask] + + # 步骤3:反转回 白底黑线 + # lines = cv2.bitwise_not(thinned) + # cv2.imwrite(os.path.join('/home/user/PycharmProjects/trinity_client_aida/test/lines_original_result_5', f"Original_{path.replace('/', '-')}.png"), img) + # cv2.imwrite(os.path.join('/home/user/PycharmProjects/trinity_client_aida/test/lines_original_result_5', f"Line_{path.replace('/', '-')}.png"), result) + + return result + def read_image(self, image_path): image_mask = None image = oss_get_image(oss_client=self.minio_client, bucket=image_path.split("/", 1)[0], object_name=image_path.split("/", 1)[1], data_type="cv2") diff --git a/app/service/design_fast/pipeline/split.py b/app/service/design_fast/pipeline/split.py index d0d7511..5c62899 100644 --- a/app/service/design_fast/pipeline/split.py +++ b/app/service/design_fast/pipeline/split.py @@ -32,14 +32,14 @@ class Split(object): new_width = int(width * result['resize_scale'][0]) new_height = int(height * result['resize_scale'][1]) - front_mask = cv2.resize(result['front_mask'], (new_width, new_height)) - back_mask = cv2.resize(result['back_mask'], (new_width, new_height)) + front_mask = cv2.resize(result['front_mask'], (new_width, new_height), interpolation=cv2.INTER_AREA) + back_mask = cv2.resize(result['back_mask'], (new_width, new_height), interpolation=cv2.INTER_AREA) rgba_image = rgb_to_rgba(result['final_image'], front_mask + back_mask) new_size = (int(rgba_image.shape[1] * result["scale"]), int(rgba_image.shape[0] * result["scale"])) - rgba_image = cv2.resize(rgba_image, new_size) + rgba_image = cv2.resize(rgba_image, new_size, interpolation=cv2.INTER_AREA) result_front_image = np.zeros_like(rgba_image) - front_mask = cv2.resize(front_mask, new_size) + front_mask = cv2.resize(front_mask, new_size, interpolation=cv2.INTER_AREA) result_front_image[front_mask != 0] = rgba_image[front_mask != 0] result_front_image_pil = Image.fromarray(cvtColor(result_front_image, COLOR_BGR2RGBA)) if 'transparent' in result.keys(): @@ -48,7 +48,7 @@ class Split(object): if transparent['mask_url'] is not None and transparent['mask_url'] != "": # 预处理用户自选区mask seg_mask = oss_get_image(oss_client=self.minio_client, bucket=transparent['mask_url'].split('/')[0], object_name=transparent['mask_url'][transparent['mask_url'].find('/') + 1:], data_type="cv2") - seg_mask = cv2.resize(seg_mask, new_size, interpolation=cv2.INTER_NEAREST) + seg_mask = cv2.resize(seg_mask, new_size, interpolation=cv2.INTER_AREA) # 转换颜色空间为 RGB(OpenCV 默认是 BGR) image_rgb = cv2.cvtColor(seg_mask, cv2.COLOR_BGR2RGB) @@ -75,7 +75,7 @@ class Split(object): # if result["name"] in ('blouse', 'dress', 'outwear', 'tops'): # result_back_image = np.zeros_like(rgba_image) - # back_mask = cv2.resize(back_mask, new_size) + # back_mask = cv2.resize(back_mask, new_size, interpolation=cv2.INTER_AREA) # result_back_image[back_mask != 0] = rgba_image[back_mask != 0] # result_back_image_pil = Image.fromarray(cvtColor(result_back_image, COLOR_BGR2RGBA)) # result['back_image'], result["back_image_url"], _ = upload_png_mask(self.minio_client, result_back_image_pil, f'{generate_uuid()}', mask=None) @@ -104,7 +104,7 @@ class Split(object): # # result['back_mask_image'] = None result_back_image = np.zeros_like(rgba_image) - back_mask = cv2.resize(back_mask, new_size) + back_mask = cv2.resize(back_mask, new_size, interpolation=cv2.INTER_AREA) result_back_image[back_mask != 0] = rgba_image[back_mask != 0] result_back_image_pil = Image.fromarray(cvtColor(result_back_image, COLOR_BGR2RGBA)) result['back_image'], result["back_image_url"], _ = upload_png_mask(self.minio_client, result_back_image_pil, f'{generate_uuid()}', mask=None)