feat 新增sketch 背后视角图
fix
This commit is contained in:
@@ -81,6 +81,7 @@ def design_generate(request_data):
|
|||||||
'mask_url': lay['mask_url'],
|
'mask_url': lay['mask_url'],
|
||||||
'image_url': lay['image_url'] if 'image_url' in lay.keys() else None,
|
'image_url': lay['image_url'] if 'image_url' in lay.keys() else None,
|
||||||
'pattern_image_url': lay['pattern_image_url'] if 'pattern_image_url' in lay.keys() else None,
|
'pattern_image_url': lay['pattern_image_url'] if 'pattern_image_url' in lay.keys() else None,
|
||||||
|
'back_perspective_url': lay['back_perspective_url'] if 'back_perspective_url' in lay.keys() else None,
|
||||||
})
|
})
|
||||||
items_response['synthesis_url'] = synthesis(layers, new_size, basic)
|
items_response['synthesis_url'] = synthesis(layers, new_size, basic)
|
||||||
else:
|
else:
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
from app.service.design_fast.pipeline import LoadImage, KeyPoint, Segmentation, Color, PrintPainting, Scaling, Split, LoadBodyImage, ContourDetection
|
from app.service.design_fast.pipeline import LoadImage, KeyPoint, Segmentation, Color, PrintPainting, Scaling, Split, LoadBodyImage, ContourDetection, BackPerspective
|
||||||
|
|
||||||
|
|
||||||
class BaseItem:
|
class BaseItem:
|
||||||
@@ -16,6 +16,7 @@ class TopItem(BaseItem):
|
|||||||
LoadImage(minio_client),
|
LoadImage(minio_client),
|
||||||
KeyPoint(),
|
KeyPoint(),
|
||||||
Segmentation(minio_client),
|
Segmentation(minio_client),
|
||||||
|
BackPerspective(minio_client),
|
||||||
Color(minio_client),
|
Color(minio_client),
|
||||||
PrintPainting(minio_client),
|
PrintPainting(minio_client),
|
||||||
Scaling(),
|
Scaling(),
|
||||||
@@ -36,6 +37,7 @@ class BottomItem(BaseItem):
|
|||||||
KeyPoint(),
|
KeyPoint(),
|
||||||
ContourDetection(),
|
ContourDetection(),
|
||||||
# Segmentation(),
|
# Segmentation(),
|
||||||
|
BackPerspective(minio_client),
|
||||||
Color(minio_client),
|
Color(minio_client),
|
||||||
PrintPainting(minio_client),
|
PrintPainting(minio_client),
|
||||||
Scaling(),
|
Scaling(),
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
from .back_perspective import BackPerspective
|
||||||
from .color import Color
|
from .color import Color
|
||||||
from .contour_detection import ContourDetection
|
from .contour_detection import ContourDetection
|
||||||
from .keypoint import KeyPoint
|
from .keypoint import KeyPoint
|
||||||
@@ -13,6 +14,7 @@ __all__ = [
|
|||||||
'KeyPoint',
|
'KeyPoint',
|
||||||
'ContourDetection',
|
'ContourDetection',
|
||||||
'Segmentation',
|
'Segmentation',
|
||||||
|
'BackPerspective',
|
||||||
'Color',
|
'Color',
|
||||||
'PrintPainting',
|
'PrintPainting',
|
||||||
'Scaling',
|
'Scaling',
|
||||||
|
|||||||
79
app/service/design_fast/pipeline/back_perspective.py
Normal file
79
app/service/design_fast/pipeline/back_perspective.py
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
import cv2
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
from app.service.design_fast.utils.design_ensemble import get_seg_result
|
||||||
|
from app.service.utils.new_oss_client import oss_upload_image
|
||||||
|
|
||||||
|
|
||||||
|
class BackPerspective:
|
||||||
|
def __init__(self, minio_client):
|
||||||
|
self.minio_client = minio_client
|
||||||
|
|
||||||
|
def __call__(self, result):
|
||||||
|
|
||||||
|
# 如果sketch为系统图 查看是否有对应的 背后视角图
|
||||||
|
if result['path'].split('/')[0] == 'aida-sys-image':
|
||||||
|
file_path = result['path'].replace("images", 'images_back', 1)
|
||||||
|
if self.is_file_exists(bucket_name='aida-sys-image', file_name=file_path[file_path.find('/') + 1:]):
|
||||||
|
result['back_perspective_url'] = file_path
|
||||||
|
return result
|
||||||
|
else:
|
||||||
|
seg_result = get_seg_result("1", result['image'])[0]
|
||||||
|
elif result['name'] in ['blouse', 'outwear', 'dress', 'tops']:
|
||||||
|
seg_result = result['seg_result']
|
||||||
|
else:
|
||||||
|
seg_result = get_seg_result("1", result['image'])[0]
|
||||||
|
|
||||||
|
m = self.thicken_contours_and_display(seg_result, thickness=10, color=(0, 0, 0))
|
||||||
|
back_sketch = result['image'].copy()
|
||||||
|
back_sketch[m > 100] = 255
|
||||||
|
# 上传背后视角图
|
||||||
|
_, img_encoded = cv2.imencode(".jpg", back_sketch)
|
||||||
|
|
||||||
|
resp = oss_upload_image(self.minio_client, bucket='test', object_name=result['path'], image_bytes=img_encoded.tobytes())
|
||||||
|
result['back_perspective_url'] = f"{resp.bucket_name}/{resp.object_name}"
|
||||||
|
return result
|
||||||
|
|
||||||
|
def thicken_contours_and_display(self, mask, thickness=10, color=(0, 0, 0)):
|
||||||
|
mask = mask.astype(np.uint8) * 255
|
||||||
|
# 查找轮廓
|
||||||
|
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
||||||
|
|
||||||
|
# 创建一个彩色副本用于绘制轮廓
|
||||||
|
mask_color = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
|
||||||
|
|
||||||
|
def thicken_contour_inward(contour, thick):
|
||||||
|
# 创建一个空白的黑色图像与原始掩码大小相同
|
||||||
|
blank = np.zeros_like(mask)
|
||||||
|
# 在空白图像上绘制白色的轮廓
|
||||||
|
cv2.drawContours(blank, [contour], -1, 255, thickness=thick)
|
||||||
|
# 找到轮廓的中心(可以用重心等方法近似)
|
||||||
|
M = cv2.moments(contour)
|
||||||
|
cx = int(M['m10'] / M['m00'])
|
||||||
|
cy = int(M['m01'] / M['m00'])
|
||||||
|
# 进行距离变换,离中心越近的值越小
|
||||||
|
dist_transform = cv2.distanceTransform(255 - blank, cv2.DIST_L2, 5)
|
||||||
|
# 根据距离变换的值来决定是否保留像素,离中心近的像素更容易被保留
|
||||||
|
result = np.zeros_like(mask)
|
||||||
|
for i in range(dist_transform.shape[0]):
|
||||||
|
for j in range(dist_transform.shape[1]):
|
||||||
|
if dist_transform[i, j] < thick:
|
||||||
|
result[i, j] = 255
|
||||||
|
return result
|
||||||
|
|
||||||
|
for contour in contours:
|
||||||
|
thickened_contour = thicken_contour_inward(contour, thickness)
|
||||||
|
mask_color[thickened_contour > 0] = color
|
||||||
|
|
||||||
|
_, binary_result = cv2.threshold(mask_color, 127, 255, cv2.THRESH_BINARY)
|
||||||
|
|
||||||
|
# 转换为掩码形式
|
||||||
|
mask_result = cv2.cvtColor(binary_result, cv2.COLOR_BGR2GRAY)
|
||||||
|
return mask_result
|
||||||
|
|
||||||
|
def is_file_exists(self, bucket_name, file_name):
|
||||||
|
try:
|
||||||
|
self.minio_client.stat_object(bucket_name, file_name)
|
||||||
|
return True
|
||||||
|
except Exception:
|
||||||
|
return False
|
||||||
@@ -33,8 +33,8 @@ def organize_clothing(layer):
|
|||||||
mask=cv2.resize(layer['mask'], layer["front_image"].size),
|
mask=cv2.resize(layer['mask'], layer["front_image"].size),
|
||||||
gradient_string=layer['gradient_string'] if 'gradient_string' in layer.keys() else "",
|
gradient_string=layer['gradient_string'] if 'gradient_string' in layer.keys() else "",
|
||||||
pattern_image_url=layer['pattern_image_url'],
|
pattern_image_url=layer['pattern_image_url'],
|
||||||
pattern_image=layer['pattern_image']
|
pattern_image=layer['pattern_image'],
|
||||||
|
back_perspective_url=layer['back_perspective_url'] if 'back_perspective_url' in layer.keys() else ""
|
||||||
)
|
)
|
||||||
# 后片数据
|
# 后片数据
|
||||||
back_layer = dict(priority=-layer.get("priority", 0) if layer.get("layer_order", False) else PRIORITY_DICT.get(f'{layer["name"].lower()}_back', None),
|
back_layer = dict(priority=-layer.get("priority", 0) if layer.get("layer_order", False) else PRIORITY_DICT.get(f'{layer["name"].lower()}_back', None),
|
||||||
@@ -50,6 +50,7 @@ def organize_clothing(layer):
|
|||||||
mask=cv2.resize(layer['mask'], layer["front_image"].size),
|
mask=cv2.resize(layer['mask'], layer["front_image"].size),
|
||||||
gradient_string=layer['gradient_string'] if 'gradient_string' in layer.keys() else "",
|
gradient_string=layer['gradient_string'] if 'gradient_string' in layer.keys() else "",
|
||||||
pattern_image_url=layer['pattern_image_url'],
|
pattern_image_url=layer['pattern_image_url'],
|
||||||
|
back_perspective_url=layer['back_perspective_url'] if 'back_perspective_url' in layer.keys() else ""
|
||||||
)
|
)
|
||||||
return front_layer, back_layer
|
return front_layer, back_layer
|
||||||
|
|
||||||
|
|||||||
@@ -82,7 +82,7 @@ if __name__ == '__main__':
|
|||||||
# url = "aida-users/89/sketchboard/female/Dress/e6724ab7-8d3f-4677-abe0-c3e42ab7af85.jpeg"
|
# url = "aida-users/89/sketchboard/female/Dress/e6724ab7-8d3f-4677-abe0-c3e42ab7af85.jpeg"
|
||||||
# url = "aida-users/87/print/956614a2-7e75-4fbe-9ed0-c1831e37a2c9-4-87.png"
|
# url = "aida-users/87/print/956614a2-7e75-4fbe-9ed0-c1831e37a2c9-4-87.png"
|
||||||
# url = "aida-users/89/single_logo/123-89.png"
|
# url = "aida-users/89/single_logo/123-89.png"
|
||||||
url = "aida-users/31/sketchboard/female/dress/6edcbf92-7da9-4809-a0a8-a4b4f06dec1e0628000041.jpg"
|
url = "aida-sys-image/images_back/female/trousers/0825000630.jpg"
|
||||||
# url = "aida-collection-element/12148/Sketchboard/95ea577b-305b-4a62-b30a-39c0dd3ddb3f.png"
|
# url = "aida-collection-element/12148/Sketchboard/95ea577b-305b-4a62-b30a-39c0dd3ddb3f.png"
|
||||||
read_type = "cv2"
|
read_type = "cv2"
|
||||||
if read_type == "cv2":
|
if read_type == "cv2":
|
||||||
|
|||||||
Reference in New Issue
Block a user