feat(新功能): 1、design-print为解决sketch原图太灰导致印花颜色便暗 2、cv2.resize 插值算法更换,提升resize后图片质量 fix(修复bug): refactor(重构): test(增加测试):
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user