feat design 功能迁移
This commit is contained in:
0
app/service/design/core/__init__.py
Normal file
0
app/service/design/core/__init__.py
Normal file
116
app/service/design/core/layer.py
Normal file
116
app/service/design/core/layer.py
Normal file
@@ -0,0 +1,116 @@
|
||||
import logging
|
||||
|
||||
import numpy as np
|
||||
import cv2
|
||||
from matplotlib import pyplot as plt
|
||||
|
||||
from PIL import Image
|
||||
|
||||
|
||||
def show(img, win_name="temp"):
|
||||
cv2.imshow(win_name, img)
|
||||
cv2.waitKey(0)
|
||||
|
||||
|
||||
def crop(img):
|
||||
mid_point_h, mid_point_w = int(img.shape[0] / 2 + 30), int(img.shape[1] / 2)
|
||||
img_roi = img[mid_point_h - 520: mid_point_h + 520, mid_point_w - 340: mid_point_w + 340]
|
||||
return img_roi
|
||||
|
||||
|
||||
class Layer(object):
|
||||
def __init__(self):
|
||||
self._layer = []
|
||||
|
||||
@property
|
||||
def layer(self):
|
||||
return self._layer
|
||||
|
||||
def insert(self, layer_instance):
|
||||
if layer_instance['name'] == 'body':
|
||||
self._body = layer_instance
|
||||
self._layer.append(layer_instance)
|
||||
|
||||
def sort(self, priority):
|
||||
self._layer.sort(key=lambda x: priority[x['name']])
|
||||
|
||||
# def merge(self, cfg):
|
||||
# """
|
||||
# opencv shape order (height, width, channel)
|
||||
# image coordinate system:
|
||||
# |------------->x (width)
|
||||
# |
|
||||
# |
|
||||
# |
|
||||
# y (height)
|
||||
# Returns:
|
||||
#
|
||||
#
|
||||
# """
|
||||
# base_image = Image.new('RGBA', self._layer[1]['image'].size, (0, 0, 0, 0))
|
||||
# for layer in self._layer:
|
||||
# y, x = layer['position']
|
||||
# base_image.paste(layer['image'], (x, y), layer['image'])
|
||||
# # base_image.show()
|
||||
#
|
||||
# for x in self._layer:
|
||||
# if np.all(x['mask'] == 0):
|
||||
# continue
|
||||
# # obtain region of interest about roi(roi) and item-image(roi_image, roi_mask)
|
||||
# roi, roi_mask, roi_image, signal = self.get_roi(dst=dst, image=x)
|
||||
# temp_bg = np.expand_dims(cv2.bitwise_not(roi_mask), axis=2).repeat(3, axis=2)
|
||||
# tmp1 = (roi * (temp_bg / 255)).astype(np.uint8)
|
||||
# temp_fg = np.expand_dims(roi_mask, axis=2).repeat(3, axis=2)
|
||||
# tmp2 = (roi_image * (temp_fg / 255)).astype(np.uint8)
|
||||
#
|
||||
# roi[:] = cv2.add(tmp1, tmp2)
|
||||
# # show(cv2.resize(dst, (int(dst.shape[1] * 0.5), int(dst.shape[0] * 0.5)), interpolation=cv2.INTER_AREA),
|
||||
# # win_name=x.get('name'))
|
||||
# # crop image and get the central part
|
||||
# if cfg.get('basic')['self_template'] == False:
|
||||
# dst_roi = crop(dst)
|
||||
# else:
|
||||
# dst_roi = dst
|
||||
# return dst_roi, signal
|
||||
#
|
||||
# @staticmethod
|
||||
# def get_roi(dst, image):
|
||||
# signal = False
|
||||
# dst_y, dst_x = dst.shape[:2]
|
||||
# roi_height, roi_width = image['mask'].shape
|
||||
# roi_y0, roi_x0 = image['position']
|
||||
#
|
||||
# if roi_y0 < 0:
|
||||
# roi_yin = 0
|
||||
# mask_yin = -roi_y0
|
||||
# signal = True
|
||||
# else:
|
||||
# roi_yin = roi_y0
|
||||
# mask_yin = 0
|
||||
# if roi_y0 + roi_height > dst_y:
|
||||
# roi_yout = dst_y
|
||||
# mask_yout = dst_y - roi_y0
|
||||
# signal = True
|
||||
# else:
|
||||
# roi_yout = roi_height + roi_y0
|
||||
# mask_yout = roi_height
|
||||
# # x part
|
||||
# if roi_x0 < 0:
|
||||
# roi_xin = 0
|
||||
# mask_xin = -roi_x0
|
||||
# signal = True
|
||||
# else:
|
||||
# roi_xin = roi_x0
|
||||
# mask_xin = 0
|
||||
# if roi_x0 + roi_width > dst_x:
|
||||
# roi_xout = dst_x
|
||||
# mask_xout = dst_x - roi_x0
|
||||
# signal = True
|
||||
# else:
|
||||
# roi_xout = roi_width + roi_x0
|
||||
# mask_xout = roi_width
|
||||
#
|
||||
# roi = dst[roi_yin: roi_yout, roi_xin: roi_xout]
|
||||
# roi_mask = image['mask'][mask_yin: mask_yout, mask_xin: mask_xout]
|
||||
# roi_image = image['image'][mask_yin: mask_yout, mask_xin: mask_xout]
|
||||
# return roi, roi_mask, roi_image, signal
|
||||
45
app/service/design/core/priority.py
Normal file
45
app/service/design/core/priority.py
Normal file
@@ -0,0 +1,45 @@
|
||||
class Priority(object):
|
||||
"""Item layer priority levels.
|
||||
"""
|
||||
|
||||
def __init__(self, item_list):
|
||||
self._priority = dict(
|
||||
earring_front=99,
|
||||
bag_front=98,
|
||||
hairstyle_front=97,
|
||||
outwear_front=20,
|
||||
bottoms_front=19,
|
||||
dress_front=18,
|
||||
blouse_front=17,
|
||||
skirt_front=16,
|
||||
trousers_front=15,
|
||||
tops_front=14,
|
||||
shoes_right=1,
|
||||
shoes_left=1,
|
||||
body=0,
|
||||
tops_back=-14,
|
||||
trousers_back=-15,
|
||||
skirt_back=-16,
|
||||
blouse_back=-17,
|
||||
dress_back=-18,
|
||||
bottoms_back=-19,
|
||||
outwear_back=-20,
|
||||
hairstyle_back=-97,
|
||||
bag_back=-98,
|
||||
earring_back=-99,
|
||||
)
|
||||
self.clothing_start_num = 10
|
||||
if not isinstance(item_list, list):
|
||||
raise ValueError('item_list must be a list!')
|
||||
for cate in item_list:
|
||||
cate = cate.lower()
|
||||
if cate not in ('outwear', 'dress', 'blouse', 'skirt', 'trousers', 'tops', 'bottoms'):
|
||||
raise ValueError(f'Item type error. Cannot recognize {cate}')
|
||||
for i, cate in enumerate(item_list):
|
||||
cate = cate.lower()
|
||||
self._priority[f'{cate}_front'] = self.clothing_start_num - i
|
||||
self._priority[f'{cate}_back'] = -(self.clothing_start_num - i)
|
||||
|
||||
@property
|
||||
def priority(self):
|
||||
return self._priority
|
||||
Reference in New Issue
Block a user