design design batch
This commit is contained in:
@@ -20,7 +20,7 @@ class Settings(BaseSettings):
|
|||||||
|
|
||||||
|
|
||||||
OSS = "minio"
|
OSS = "minio"
|
||||||
DEBUG = False
|
DEBUG = True
|
||||||
if DEBUG:
|
if DEBUG:
|
||||||
LOGS_PATH = "logs/"
|
LOGS_PATH = "logs/"
|
||||||
CATEGORY_PATH = "service/attribute/config/descriptor/category/category_dis.csv"
|
CATEGORY_PATH = "service/attribute/config/descriptor/category/category_dis.csv"
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ from app.service.design_batch.utils.save_json import oss_upload_json
|
|||||||
from app.service.design_batch.utils.synthesis_item import update_base_size_priority, synthesis, synthesis_single
|
from app.service.design_batch.utils.synthesis_item import update_base_size_priority, synthesis, synthesis_single
|
||||||
|
|
||||||
id_lock = threading.Lock()
|
id_lock = threading.Lock()
|
||||||
celery_app = Celery('tasks', broker='amqp://guest:guest@10.1.2.213:5672//', backend='rpc://')
|
celery_app = Celery('tasks', broker='amqp://guest:guest@10.1.2.190:5672//', backend='rpc://')
|
||||||
celery_app.conf.worker_log_format = '%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s %(message)s'
|
celery_app.conf.worker_log_format = '%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s %(message)s'
|
||||||
celery_app.conf.worker_hijack_root_logger = False
|
celery_app.conf.worker_hijack_root_logger = False
|
||||||
logging.getLogger('pika').setLevel(logging.WARNING)
|
logging.getLogger('pika').setLevel(logging.WARNING)
|
||||||
@@ -46,7 +46,7 @@ def process_layer(item, layers):
|
|||||||
layers.append(back_layer)
|
layers.append(back_layer)
|
||||||
|
|
||||||
|
|
||||||
@celery_app.task
|
# @celery_app.task
|
||||||
def batch_design(objects_data, tasks_id, json_name):
|
def batch_design(objects_data, tasks_id, json_name):
|
||||||
object_response = []
|
object_response = []
|
||||||
threads = []
|
threads = []
|
||||||
@@ -108,6 +108,7 @@ def batch_design(objects_data, tasks_id, json_name):
|
|||||||
|
|
||||||
with lock:
|
with lock:
|
||||||
object_response.append(items_response)
|
object_response.append(items_response)
|
||||||
|
logger.info(items_response)
|
||||||
publish_status(tasks_id, step + 1, items_response)
|
publish_status(tasks_id, step + 1, items_response)
|
||||||
active_threads -= 1
|
active_threads -= 1
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
from app.service.design_batch.pipeline import *
|
from app.service.design_fast.pipeline import LoadImage, KeyPoint, Segmentation, Color, PrintPainting, Scaling, Split, LoadBodyImage, ContourDetection
|
||||||
|
|
||||||
|
|
||||||
class BaseItem:
|
class BaseItem:
|
||||||
@@ -9,6 +9,27 @@ class BaseItem:
|
|||||||
self.result.update(basic)
|
self.result.update(basic)
|
||||||
|
|
||||||
|
|
||||||
|
class AccessoriesItem(BaseItem):
|
||||||
|
def __init__(self, data, basic, minio_client):
|
||||||
|
super().__init__(data, basic)
|
||||||
|
self.Accessories_pipeline = [
|
||||||
|
LoadImage(minio_client),
|
||||||
|
# KeyPoint(),
|
||||||
|
ContourDetection(),
|
||||||
|
# Segmentation(minio_client),
|
||||||
|
# BackPerspective(minio_client),
|
||||||
|
Color(minio_client),
|
||||||
|
PrintPainting(minio_client),
|
||||||
|
Scaling(),
|
||||||
|
Split(minio_client)
|
||||||
|
]
|
||||||
|
|
||||||
|
def process(self):
|
||||||
|
for item in self.Accessories_pipeline:
|
||||||
|
self.result = item(self.result)
|
||||||
|
return self.result
|
||||||
|
|
||||||
|
|
||||||
class TopItem(BaseItem):
|
class TopItem(BaseItem):
|
||||||
def __init__(self, data, basic, minio_client):
|
def __init__(self, data, basic, minio_client):
|
||||||
super().__init__(data, basic)
|
super().__init__(data, basic)
|
||||||
@@ -16,6 +37,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 +58,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_batch/pipeline/back_perspective.py
Normal file
79
app/service/design_batch/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
|
||||||
@@ -14,11 +14,18 @@ class Color:
|
|||||||
|
|
||||||
def __call__(self, result):
|
def __call__(self, result):
|
||||||
dim_image_h, dim_image_w = result['image'].shape[0:2]
|
dim_image_h, dim_image_w = result['image'].shape[0:2]
|
||||||
|
# 渐变色
|
||||||
if "gradient" in result.keys() and result['gradient'] != "":
|
if "gradient" in result.keys() and result['gradient'] != "":
|
||||||
bucket_name = result['gradient'].split('/')[0]
|
bucket_name = result['gradient'].split('/')[0]
|
||||||
object_name = result['gradient'][result['gradient'].find('/') + 1:]
|
object_name = result['gradient'][result['gradient'].find('/') + 1:]
|
||||||
pattern = self.get_gradient(bucket_name=bucket_name, object_name=object_name)
|
pattern = self.get_gradient(bucket_name=bucket_name, object_name=object_name)
|
||||||
resize_pattern = cv2.resize(pattern, (dim_image_w, dim_image_h), interpolation=cv2.INTER_AREA)
|
resize_pattern = cv2.resize(pattern, (dim_image_w, dim_image_h), interpolation=cv2.INTER_AREA)
|
||||||
|
# 无色
|
||||||
|
elif "color" not in result.keys() or result['color'] == "":
|
||||||
|
result['final_image'] = result['pattern_image'] = result['single_image'] = result['image']
|
||||||
|
result['alpha'] = 100 / 255.0
|
||||||
|
return result
|
||||||
|
# 正常颜色
|
||||||
else:
|
else:
|
||||||
pattern = self.get_pattern(result['color'])
|
pattern = self.get_pattern(result['color'])
|
||||||
resize_pattern = cv2.resize(pattern, (dim_image_w, dim_image_h), interpolation=cv2.INTER_AREA)
|
resize_pattern = cv2.resize(pattern, (dim_image_w, dim_image_h), interpolation=cv2.INTER_AREA)
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ import numpy as np
|
|||||||
from pymilvus import MilvusClient
|
from pymilvus import MilvusClient
|
||||||
|
|
||||||
from app.core.config import *
|
from app.core.config import *
|
||||||
from app.service.design_batch.utils.design_ensemble import get_keypoint_result
|
from app.service.design_fast.utils.design_ensemble import get_keypoint_result
|
||||||
|
from app.service.utils.decorator import ClassCallRunTime, RunTime
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
@@ -16,14 +17,15 @@ class KeyPoint:
|
|||||||
def get_name(cls):
|
def get_name(cls):
|
||||||
return cls.name
|
return cls.name
|
||||||
|
|
||||||
|
@ClassCallRunTime
|
||||||
def __call__(self, result):
|
def __call__(self, result):
|
||||||
if result['name'] in ['blouse', 'skirt', 'dress', 'outwear', 'trousers', 'tops', 'bottoms']: # 查询是否有数据 且类别相同 相同则直接读 不同则推理后更新
|
if result['name'] in ['blouse', 'skirt', 'dress', 'outwear', 'trousers', 'tops', 'bottoms']: # 查询是否有数据 且类别相同 相同则直接读 不同则推理后更新
|
||||||
# result['clothes_keypoint'] = self.infer_keypoint_result(result)
|
# result['clothes_keypoint'] = self.infer_keypoint_result(result)
|
||||||
site = 'up' if result['name'] in ['blouse', 'outwear', 'dress', 'tops'] else 'down'
|
site = 'up' if result['name'] in ['blouse', 'outwear', 'dress', 'tops'] else 'down'
|
||||||
# keypoint_cache = search_keypoint_cache(result["image_id"], site)
|
# keypoint_cache = search_keypoint_cache(result["image_id"], site)
|
||||||
keypoint_cache = self.keypoint_cache(result, site)
|
# keypoint_cache = self.keypoint_cache(result, site)
|
||||||
|
keypoint_cache = False
|
||||||
# 取消向量查询 直接过模型推理
|
# 取消向量查询 直接过模型推理
|
||||||
# keypoint_cache = False
|
|
||||||
if keypoint_cache is False:
|
if keypoint_cache is False:
|
||||||
keypoint_infer_result, site = self.infer_keypoint_result(result)
|
keypoint_infer_result, site = self.infer_keypoint_result(result)
|
||||||
result['clothes_keypoint'] = self.save_keypoint_cache(result["image_id"], keypoint_infer_result, site)
|
result['clothes_keypoint'] = self.save_keypoint_cache(result["image_id"], keypoint_infer_result, site)
|
||||||
@@ -87,7 +89,7 @@ class KeyPoint:
|
|||||||
logger.info(f"save keypoint cache milvus error : {e}")
|
logger.info(f"save keypoint cache milvus error : {e}")
|
||||||
return dict(zip(KEYPOINT_RESULT_TABLE_FIELD_SET, result.reshape(12, 2).astype(int).tolist()))
|
return dict(zip(KEYPOINT_RESULT_TABLE_FIELD_SET, result.reshape(12, 2).astype(int).tolist()))
|
||||||
|
|
||||||
# @ RunTime
|
@RunTime
|
||||||
def keypoint_cache(self, result, site):
|
def keypoint_cache(self, result, site):
|
||||||
try:
|
try:
|
||||||
client = MilvusClient(uri=MILVUS_URL, token=MILVUS_TOKEN, db_name=MILVUS_ALIAS)
|
client = MilvusClient(uri=MILVUS_URL, token=MILVUS_TOKEN, db_name=MILVUS_ALIAS)
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
|
import io
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import cv2
|
import cv2
|
||||||
|
import numpy as np
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
from app.service.utils.new_oss_client import oss_get_image
|
from app.service.utils.new_oss_client import oss_get_image
|
||||||
|
|
||||||
@@ -71,6 +74,8 @@ class LoadImage:
|
|||||||
keypoint = 'head_point'
|
keypoint = 'head_point'
|
||||||
elif name == 'earring':
|
elif name == 'earring':
|
||||||
keypoint = 'ear_point'
|
keypoint = 'ear_point'
|
||||||
|
elif name == 'accessories':
|
||||||
|
keypoint = "accessories"
|
||||||
else:
|
else:
|
||||||
raise KeyError(f"{name} does not belong to item category list: blouse, outwear, dress, trousers, skirt, "
|
raise KeyError(f"{name} does not belong to item category list: blouse, outwear, dress, trousers, skirt, "
|
||||||
f"bag, shoes, hairstyle, earring.")
|
f"bag, shoes, hairstyle, earring.")
|
||||||
|
|||||||
@@ -46,4 +46,16 @@ class Scaling:
|
|||||||
result['scale'] = result['scale_bag']
|
result['scale'] = result['scale_bag']
|
||||||
elif result['keypoint'] == 'ear_point':
|
elif result['keypoint'] == 'ear_point':
|
||||||
result['scale'] = result['scale_earrings']
|
result['scale'] = result['scale_earrings']
|
||||||
|
elif result['keypoint'] == 'accessories':
|
||||||
|
# 由于没有识别配饰keypoint的模型 所以统一将配饰的两个关键点设定为 (0,0) (0,img.width)
|
||||||
|
# 模特的关键点设定为(0,0) (0,320/2) 距离比例简写为 160 / img.width
|
||||||
|
distance_clo = result['img_shape'][1]
|
||||||
|
distance_bdy = 320 / 2
|
||||||
|
|
||||||
|
if distance_clo == 0:
|
||||||
|
result['scale'] = 1
|
||||||
|
else:
|
||||||
|
result['scale'] = distance_bdy / distance_clo
|
||||||
|
else:
|
||||||
|
result['scale'] = 1
|
||||||
return result
|
return result
|
||||||
|
|||||||
@@ -5,7 +5,8 @@ import cv2
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
|
|
||||||
from app.core.config import SEG_CACHE_PATH
|
from app.core.config import SEG_CACHE_PATH
|
||||||
from app.service.design_batch.utils.design_ensemble import get_seg_result
|
from app.service.design_fast.utils.design_ensemble import get_seg_result
|
||||||
|
from app.service.utils.decorator import ClassCallRunTime
|
||||||
from app.service.utils.new_oss_client import oss_get_image
|
from app.service.utils.new_oss_client import oss_get_image
|
||||||
|
|
||||||
logger = logging.getLogger()
|
logger = logging.getLogger()
|
||||||
@@ -15,6 +16,7 @@ class Segmentation:
|
|||||||
def __init__(self, minio_client):
|
def __init__(self, minio_client):
|
||||||
self.minio_client = minio_client
|
self.minio_client = minio_client
|
||||||
|
|
||||||
|
@ClassCallRunTime
|
||||||
def __call__(self, result):
|
def __call__(self, result):
|
||||||
if "seg_mask_url" in result.keys() and result['seg_mask_url'] != "":
|
if "seg_mask_url" in result.keys() and result['seg_mask_url'] != "":
|
||||||
seg_mask = oss_get_image(oss_client=self.minio_client, bucket=result['seg_mask_url'].split('/')[0], object_name=result['seg_mask_url'][result['seg_mask_url'].find('/') + 1:], data_type="cv2")
|
seg_mask = oss_get_image(oss_client=self.minio_client, bucket=result['seg_mask_url'].split('/')[0], object_name=result['seg_mask_url'][result['seg_mask_url'].find('/') + 1:], data_type="cv2")
|
||||||
@@ -31,13 +33,26 @@ class Segmentation:
|
|||||||
result['back_mask'] = np.array(green_mask, dtype=np.uint8) * 255
|
result['back_mask'] = np.array(green_mask, dtype=np.uint8) * 255
|
||||||
result['mask'] = result['front_mask'] + result['back_mask']
|
result['mask'] = result['front_mask'] + result['back_mask']
|
||||||
else:
|
else:
|
||||||
# 本地查询seg 缓存是否存在
|
# preview 过模型 不缓存
|
||||||
_, seg_result = self.load_seg_result(result["image_id"])
|
if "preview_submit" in result.keys() and result['preview_submit'] == "preview":
|
||||||
result['seg_result'] = seg_result
|
# 推理获得seg 结果
|
||||||
if not _:
|
seg_result = get_seg_result(result["image_id"], result['image'])[0]
|
||||||
|
# submit 过模型 缓存
|
||||||
|
elif "preview_submit" in result.keys() and result['preview_submit'] == "submit":
|
||||||
# 推理获得seg 结果
|
# 推理获得seg 结果
|
||||||
seg_result = get_seg_result(result["image_id"], result['image'])[0]
|
seg_result = get_seg_result(result["image_id"], result['image'])[0]
|
||||||
self.save_seg_result(seg_result, result['image_id'])
|
self.save_seg_result(seg_result, result['image_id'])
|
||||||
|
# null 正常流程 加载本地缓存 无缓存则过模型
|
||||||
|
else:
|
||||||
|
# 本地查询seg 缓存是否存在
|
||||||
|
_, seg_result = self.load_seg_result(result["image_id"])
|
||||||
|
# 判断缓存和实际图片size是否相同
|
||||||
|
if not _ or result["image"].shape[:2] != seg_result.shape:
|
||||||
|
# 推理获得seg 结果
|
||||||
|
seg_result = get_seg_result(result["image_id"], result['image'])[0]
|
||||||
|
self.save_seg_result(seg_result, result['image_id'])
|
||||||
|
result['seg_result'] = seg_result
|
||||||
|
|
||||||
# 处理前片后片
|
# 处理前片后片
|
||||||
temp_front = seg_result == 1.0
|
temp_front = seg_result == 1.0
|
||||||
result['front_mask'] = (255 * (temp_front + 0).astype(np.uint8))
|
result['front_mask'] = (255 * (temp_front + 0).astype(np.uint8))
|
||||||
@@ -48,7 +63,7 @@ class Segmentation:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def save_seg_result(seg_result, image_id):
|
def save_seg_result(seg_result, image_id):
|
||||||
file_path = f"seg_cache/{image_id}.npy"
|
file_path = f"{SEG_CACHE_PATH}{image_id}.npy"
|
||||||
try:
|
try:
|
||||||
np.save(file_path, seg_result)
|
np.save(file_path, seg_result)
|
||||||
logger.info(f"保存成功 :{os.path.abspath(file_path)}")
|
logger.info(f"保存成功 :{os.path.abspath(file_path)}")
|
||||||
@@ -57,7 +72,7 @@ class Segmentation:
|
|||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def load_seg_result(image_id):
|
def load_seg_result(image_id):
|
||||||
file_path = f"seg_cache/{image_id}.npy"
|
file_path = f"{SEG_CACHE_PATH}{image_id}.npy"
|
||||||
logger.info(f"load seg file name is :{SEG_CACHE_PATH}{image_id}.npy")
|
logger.info(f"load seg file name is :{SEG_CACHE_PATH}{image_id}.npy")
|
||||||
try:
|
try:
|
||||||
seg_result = np.load(file_path)
|
seg_result = np.load(file_path)
|
||||||
|
|||||||
@@ -7,10 +7,11 @@ from PIL import Image
|
|||||||
from cv2 import cvtColor, COLOR_BGR2RGBA
|
from cv2 import cvtColor, COLOR_BGR2RGBA
|
||||||
|
|
||||||
from app.core.config import AIDA_CLOTHING
|
from app.core.config import AIDA_CLOTHING
|
||||||
from app.service.design_batch.utils.conversion_image import rgb_to_rgba
|
from app.service.design_fast.utils.conversion_image import rgb_to_rgba
|
||||||
from app.service.design_batch.utils.upload_image import upload_png_mask
|
from app.service.design_fast.utils.transparent import sketch_to_transparent
|
||||||
|
from app.service.design_fast.utils.upload_image import upload_png_mask
|
||||||
from app.service.utils.generate_uuid import generate_uuid
|
from app.service.utils.generate_uuid import generate_uuid
|
||||||
from app.service.utils.new_oss_client import oss_upload_image
|
from app.service.utils.new_oss_client import oss_upload_image, oss_get_image
|
||||||
|
|
||||||
|
|
||||||
class Split(object):
|
class Split(object):
|
||||||
@@ -20,7 +21,7 @@ class Split(object):
|
|||||||
def __call__(self, result):
|
def __call__(self, result):
|
||||||
try:
|
try:
|
||||||
|
|
||||||
if result['name'] in ('outwear', 'dress', 'blouse', 'skirt', 'trousers', 'tops', 'bottoms'):
|
if result['name'] in ('outwear', 'dress', 'blouse', 'skirt', 'trousers', 'tops', 'bottoms','accessories'):
|
||||||
front_mask = result['front_mask']
|
front_mask = result['front_mask']
|
||||||
back_mask = result['back_mask']
|
back_mask = result['back_mask']
|
||||||
rgba_image = rgb_to_rgba(result['final_image'], front_mask + back_mask)
|
rgba_image = rgb_to_rgba(result['final_image'], front_mask + back_mask)
|
||||||
@@ -30,6 +31,24 @@ class Split(object):
|
|||||||
front_mask = cv2.resize(front_mask, new_size)
|
front_mask = cv2.resize(front_mask, new_size)
|
||||||
result_front_image[front_mask != 0] = rgba_image[front_mask != 0]
|
result_front_image[front_mask != 0] = rgba_image[front_mask != 0]
|
||||||
result_front_image_pil = Image.fromarray(cvtColor(result_front_image, COLOR_BGR2RGBA))
|
result_front_image_pil = Image.fromarray(cvtColor(result_front_image, COLOR_BGR2RGBA))
|
||||||
|
if 'transparent' in result.keys():
|
||||||
|
# 用户自选区域transparent
|
||||||
|
transparent = result['transparent']
|
||||||
|
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)
|
||||||
|
# 转换颜色空间为 RGB(OpenCV 默认是 BGR)
|
||||||
|
image_rgb = cv2.cvtColor(seg_mask, cv2.COLOR_BGR2RGB)
|
||||||
|
|
||||||
|
r, g, b = cv2.split(image_rgb)
|
||||||
|
blue_mask = b > r
|
||||||
|
|
||||||
|
# 创建红色和绿色掩码
|
||||||
|
transparent_mask = np.array(blue_mask, dtype=np.uint8) * 255
|
||||||
|
result_front_image_pil = sketch_to_transparent(result_front_image_pil, transparent_mask, transparent["scale"])
|
||||||
|
else:
|
||||||
|
result_front_image_pil = sketch_to_transparent(result_front_image_pil, front_mask, transparent["scale"])
|
||||||
result['front_image'], result["front_image_url"], _ = upload_png_mask(self.minio_client, result_front_image_pil, f'{generate_uuid()}', mask=None)
|
result['front_image'], result["front_image_url"], _ = upload_png_mask(self.minio_client, result_front_image_pil, f'{generate_uuid()}', mask=None)
|
||||||
|
|
||||||
height, width = front_mask.shape
|
height, width = front_mask.shape
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ from app.service.design_batch.utils.MQ import publish_status
|
|||||||
|
|
||||||
|
|
||||||
async def start_design_batch_generate(data, file):
|
async def start_design_batch_generate(data, file):
|
||||||
generate_clothes_task = batch_design.delay(json.loads(file.decode())['objects'], data.total, data.tasks_id)
|
generate_clothes_task = batch_design(json.loads(file.decode())['objects'], data.total, data.file_name)
|
||||||
print(generate_clothes_task)
|
print(generate_clothes_task)
|
||||||
publish_status(data.tasks_id, "0/100", "")
|
publish_status(data.tasks_id, "0/100", "")
|
||||||
return {"task_id": data.tasks_id}
|
return {"task_id": data.tasks_id}
|
||||||
|
|||||||
@@ -2,9 +2,12 @@ import json
|
|||||||
|
|
||||||
import pika
|
import pika
|
||||||
|
|
||||||
|
from app.core.config import RABBITMQ_PARAMS
|
||||||
|
|
||||||
|
|
||||||
def publish_status(task_id, progress, result):
|
def publish_status(task_id, progress, result):
|
||||||
connection = pika.BlockingConnection(pika.ConnectionParameters('10.1.2.213'))
|
connection = pika.BlockingConnection(pika.ConnectionParameters(**RABBITMQ_PARAMS))
|
||||||
|
# connection = pika.BlockingConnection(pika.ConnectionParameters('10.1.2.190'))
|
||||||
channel = connection.channel()
|
channel = connection.channel()
|
||||||
channel.queue_declare(queue='DesignBatch', durable=True)
|
channel.queue_declare(queue='DesignBatch', durable=True)
|
||||||
message = {'task_id': task_id, 'progress': progress, "result": result}
|
message = {'task_id': task_id, 'progress': progress, "result": result}
|
||||||
@@ -15,3 +18,7 @@ def publish_status(task_id, progress, result):
|
|||||||
delivery_mode=2,
|
delivery_mode=2,
|
||||||
))
|
))
|
||||||
connection.close()
|
connection.close()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
publish_status("1", "1", "1")
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ def seg_preprocess(img_path):
|
|||||||
if ori_shape != (img_scale_w, img_scale_h):
|
if ori_shape != (img_scale_w, img_scale_h):
|
||||||
# mmcv.imresize(img, img_scale_h, img_scale_w) # 老代码 引以为戒!哈哈哈~ h和w写反了
|
# mmcv.imresize(img, img_scale_h, img_scale_w) # 老代码 引以为戒!哈哈哈~ h和w写反了
|
||||||
img = cv2.resize(img, (img_scale_h, img_scale_w))
|
img = cv2.resize(img, (img_scale_h, img_scale_w))
|
||||||
img = mmcv.imnormalize(img, mean=np.array([123.675, 116.28, 103.53]), std=np.array([58.395, 57.12, 57.375]), to_rgb=True)
|
# img = mmcv.imnormalize(img, mean=np.array([123.675, 116.28, 103.53]), std=np.array([58.395, 57.12, 57.375]), to_rgb=True)
|
||||||
preprocessed_img = np.expand_dims(img.transpose(2, 0, 1), axis=0)
|
preprocessed_img = np.expand_dims(img.transpose(2, 0, 1), axis=0)
|
||||||
return preprocessed_img, ori_shape
|
return preprocessed_img, ori_shape
|
||||||
|
|
||||||
|
|||||||
@@ -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,46 @@ 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
|
||||||
|
|
||||||
|
|
||||||
|
def organize_accessories(layer):
|
||||||
|
# 起始坐标
|
||||||
|
start_point = (0, 0)
|
||||||
|
# 前片数据
|
||||||
|
front_layer = dict(priority=layer['priority'] if layer.get("layer_order", False) else PRIORITY_DICT.get(f'{layer["name"].lower()}_front', None),
|
||||||
|
name=f'{layer["name"].lower()}_front',
|
||||||
|
image=layer["front_image"],
|
||||||
|
# mask_image=layer['front_mask_image'],
|
||||||
|
image_url=layer['front_image_url'],
|
||||||
|
mask_url=layer['mask_url'],
|
||||||
|
sacle=layer['scale'],
|
||||||
|
clothes_keypoint=(0, 0),
|
||||||
|
position=start_point,
|
||||||
|
resize_scale=layer["resize_scale"],
|
||||||
|
mask=cv2.resize(layer['mask'], layer["front_image"].size),
|
||||||
|
gradient_string=layer['gradient_string'] if 'gradient_string' in layer.keys() else "",
|
||||||
|
pattern_image_url=layer['pattern_image_url'],
|
||||||
|
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),
|
||||||
|
name=f'{layer["name"].lower()}_back',
|
||||||
|
image=layer["back_image"],
|
||||||
|
# mask_image=layer['back_mask_image'],
|
||||||
|
image_url=layer['back_image_url'],
|
||||||
|
mask_url=layer['mask_url'],
|
||||||
|
sacle=layer['scale'],
|
||||||
|
clothes_keypoint=(0, 0),
|
||||||
|
position=start_point,
|
||||||
|
resize_scale=layer["resize_scale"],
|
||||||
|
mask=cv2.resize(layer['mask'], layer["front_image"].size),
|
||||||
|
gradient_string=layer['gradient_string'] if 'gradient_string' in layer.keys() else "",
|
||||||
|
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
|
||||||
|
|
||||||
|
|||||||
@@ -79,9 +79,11 @@ def synthesis(data, size, basic_info):
|
|||||||
_, binary_body_mask = cv2.threshold(body_mask, 127, 255, cv2.THRESH_BINARY)
|
_, binary_body_mask = cv2.threshold(body_mask, 127, 255, cv2.THRESH_BINARY)
|
||||||
top_outer_mask = np.array(binary_body_mask)
|
top_outer_mask = np.array(binary_body_mask)
|
||||||
bottom_outer_mask = np.array(binary_body_mask)
|
bottom_outer_mask = np.array(binary_body_mask)
|
||||||
|
accessories_outer_mask = np.array(binary_body_mask)
|
||||||
|
|
||||||
top = True
|
top = True
|
||||||
bottom = True
|
bottom = True
|
||||||
|
accessories = True
|
||||||
i = len(data)
|
i = len(data)
|
||||||
while i:
|
while i:
|
||||||
i -= 1
|
i -= 1
|
||||||
@@ -98,7 +100,7 @@ def synthesis(data, size, basic_info):
|
|||||||
background[all_y_start:all_y_end, all_x_start:all_x_end] = sketch_mask[mask_y_start:mask_y_end, mask_x_start:mask_x_end]
|
background[all_y_start:all_y_end, all_x_start:all_x_end] = sketch_mask[mask_y_start:mask_y_end, mask_x_start:mask_x_end]
|
||||||
top_outer_mask = background + top_outer_mask
|
top_outer_mask = background + top_outer_mask
|
||||||
elif bottom and data[i]['name'] in ["trousers_front", "skirt_front", "bottoms_front", "dress_front"]:
|
elif bottom and data[i]['name'] in ["trousers_front", "skirt_front", "bottoms_front", "dress_front"]:
|
||||||
bottom = False
|
# bottom = False
|
||||||
mask_shape = data[i]['mask'].shape
|
mask_shape = data[i]['mask'].shape
|
||||||
y_offset, x_offset = data[i]['adaptive_position']
|
y_offset, x_offset = data[i]['adaptive_position']
|
||||||
# 初始化叠加区域的起始和结束位置
|
# 初始化叠加区域的起始和结束位置
|
||||||
@@ -109,10 +111,23 @@ def synthesis(data, size, basic_info):
|
|||||||
background = np.zeros_like(top_outer_mask)
|
background = np.zeros_like(top_outer_mask)
|
||||||
background[all_y_start:all_y_end, all_x_start:all_x_end] = sketch_mask[mask_y_start:mask_y_end, mask_x_start:mask_x_end]
|
background[all_y_start:all_y_end, all_x_start:all_x_end] = sketch_mask[mask_y_start:mask_y_end, mask_x_start:mask_x_end]
|
||||||
bottom_outer_mask = background + bottom_outer_mask
|
bottom_outer_mask = background + bottom_outer_mask
|
||||||
|
elif accessories and data[i]['name'] in ['accessories_front']:
|
||||||
|
mask_shape = data[i]['mask'].shape
|
||||||
|
y_offset, x_offset = data[i]['adaptive_position']
|
||||||
|
# 初始化叠加区域的起始和结束位置
|
||||||
|
all_y_start, all_y_end, mask_y_start, mask_y_end = positioning(all_mask_shape=all_mask_shape[0], mask_shape=mask_shape[0], offset=y_offset)
|
||||||
|
all_x_start, all_x_end, mask_x_start, mask_x_end = positioning(all_mask_shape=all_mask_shape[1], mask_shape=mask_shape[1], offset=x_offset)
|
||||||
|
# 将叠加区域赋值为相应的像素值
|
||||||
|
_, sketch_mask = cv2.threshold(data[i]['mask'], 127, 255, cv2.THRESH_BINARY)
|
||||||
|
background = np.zeros_like(top_outer_mask)
|
||||||
|
background[all_y_start:all_y_end, all_x_start:all_x_end] = sketch_mask[mask_y_start:mask_y_end, mask_x_start:mask_x_end]
|
||||||
|
accessories_outer_mask = background + accessories_outer_mask
|
||||||
|
pass
|
||||||
elif bottom is False and top is False:
|
elif bottom is False and top is False:
|
||||||
break
|
break
|
||||||
|
|
||||||
all_mask = cv2.bitwise_or(top_outer_mask, bottom_outer_mask)
|
all_mask = cv2.bitwise_or(top_outer_mask, bottom_outer_mask)
|
||||||
|
all_mask = cv2.bitwise_or(all_mask, accessories_outer_mask)
|
||||||
|
|
||||||
for layer in data:
|
for layer in data:
|
||||||
if layer['image'] is not None:
|
if layer['image'] is not None:
|
||||||
@@ -185,12 +200,14 @@ def update_base_size_priority(layers, size):
|
|||||||
# 计算透明背景图片的宽度
|
# 计算透明背景图片的宽度
|
||||||
min_x = min(info['position'][1] for info in layers)
|
min_x = min(info['position'][1] for info in layers)
|
||||||
x_list = []
|
x_list = []
|
||||||
|
new_height = 700
|
||||||
for info in layers:
|
for info in layers:
|
||||||
if info['image'] is not None:
|
if info['image'] is not None:
|
||||||
x_list.append(info['position'][1] + info['image'].width)
|
x_list.append(info['position'][1] + info['image'].width)
|
||||||
|
if info['name'] == 'mannequin':
|
||||||
|
new_height = info['image'].height
|
||||||
max_x = max(x_list)
|
max_x = max(x_list)
|
||||||
new_width = max_x - min_x
|
new_width = max_x - min_x
|
||||||
new_height = 700
|
|
||||||
# 更新坐标
|
# 更新坐标
|
||||||
for info in layers:
|
for info in layers:
|
||||||
info['adaptive_position'] = (info['position'][0], info['position'][1] - min_x)
|
info['adaptive_position'] = (info['position'][0], info['position'][1] - min_x)
|
||||||
|
|||||||
26
app/service/design_batch/utils/transparent.py
Normal file
26
app/service/design_batch/utils/transparent.py
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
|
||||||
|
def sketch_to_transparent(image, mask, transparency):
|
||||||
|
# 打开原始图片
|
||||||
|
image = image.convert("RGBA")
|
||||||
|
# 打开mask图片,假设mask图片是灰度图,白色区域为要处理的区域,黑色区域为保留的区域
|
||||||
|
mask = Image.fromarray(mask)
|
||||||
|
|
||||||
|
# 根据透明度调整因子,将透明度转换为0-255之间的值
|
||||||
|
alpha_value = int((1 - transparency) * 255.0)
|
||||||
|
|
||||||
|
# 获取图片的像素数据
|
||||||
|
image_pixels = image.load()
|
||||||
|
mask_pixels = mask.load()
|
||||||
|
|
||||||
|
width, height = image.size
|
||||||
|
|
||||||
|
for y in range(height):
|
||||||
|
for x in range(width):
|
||||||
|
# 如果mask区域对应的像素为白色(值大于128,这里假设白色为要处理的区域,可根据实际情况调整)
|
||||||
|
if mask_pixels[x, y] > 128:
|
||||||
|
r, g, b, a = image_pixels[x, y]
|
||||||
|
image_pixels[x, y] = (r, g, b, alpha_value)
|
||||||
|
|
||||||
|
return image
|
||||||
Reference in New Issue
Block a user