feat(新功能): 模特编辑逻辑修改
fix(修复bug): docs(文档变更): refactor(重构): test(增加测试):
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
import cv2
|
||||
import mediapipe as mp
|
||||
import numpy as np
|
||||
from minio import Minio
|
||||
|
||||
@@ -13,6 +12,8 @@ minio_client = Minio(MINIO_URL, access_key=MINIO_ACCESS, secret_key=MINIO_SECRET
|
||||
class MannequinEditService():
|
||||
def __init__(self, request_data):
|
||||
self.scale = request_data.scale
|
||||
self.top = request_data.top
|
||||
self.bottom = request_data.bottom
|
||||
self.image = oss_get_image(oss_client=minio_client, bucket=request_data.mannequins.split('/')[0], object_name=request_data.mannequins[request_data.mannequins.find('/') + 1:], data_type="cv2")
|
||||
self.mannequin_name = request_data.mannequin_name
|
||||
self.bucket_name = request_data.bucket_name
|
||||
@@ -27,59 +28,33 @@ class MannequinEditService():
|
||||
self.alpha = None
|
||||
|
||||
def __call__(self, *args, **kwargs):
|
||||
leg_top, leg_bottom = self.attitude_detection()
|
||||
if leg_top and leg_bottom:
|
||||
new_mannequin = self.resize_leg(leg_top, leg_bottom)
|
||||
_, encoded_image = cv2.imencode('.png', new_mannequin)
|
||||
image_bytes = encoded_image.tobytes()
|
||||
req = oss_upload_image(oss_client=minio_client, bucket=self.bucket_name, object_name=f"{self.mannequin_name}.png", image_bytes=image_bytes)
|
||||
return req.bucket_name + "/" + req.object_name
|
||||
else:
|
||||
return "No leg detected"
|
||||
new_mannequin = self.resize_leg(self.top, self.bottom)
|
||||
_, encoded_image = cv2.imencode('.png', new_mannequin)
|
||||
image_bytes = encoded_image.tobytes()
|
||||
req = oss_upload_image(oss_client=minio_client, bucket=self.bucket_name, object_name=f"{self.mannequin_name}.png", image_bytes=image_bytes)
|
||||
return req.bucket_name + "/" + req.object_name
|
||||
|
||||
def attitude_detection(self):
|
||||
mp_pose = mp.solutions.pose
|
||||
pose = mp_pose.Pose()
|
||||
def resize_leg(self, top, bottom):
|
||||
# 上部
|
||||
top_part = self.bgr[:top, :]
|
||||
top_part_alpha = self.alpha[:top, :]
|
||||
|
||||
# 将 BGR 图像转换为 RGB 格式
|
||||
image_rgb = cv2.cvtColor(self.bgr, cv2.COLOR_BGR2RGB)
|
||||
leg_top, leg_bottom = None, None
|
||||
# 进行姿态检测
|
||||
results = pose.process(image_rgb)
|
||||
if results.pose_landmarks:
|
||||
# 获取腿部关键点
|
||||
landmarks = results.pose_landmarks.landmark
|
||||
# 需要resize 部分
|
||||
part_resize = self.bgr[top:bottom, :]
|
||||
part_resize_alpha = self.alpha[top:bottom, :]
|
||||
|
||||
# 找到腿部上边界和下边界
|
||||
leg_top = int(landmarks[mp_pose.PoseLandmark.LEFT_HIP].y * self.h)
|
||||
leg_bottom = int(max(landmarks[mp_pose.PoseLandmark.LEFT_ANKLE].y,
|
||||
landmarks[mp_pose.PoseLandmark.RIGHT_ANKLE].y) * self.h)
|
||||
# 下部
|
||||
part_bottom = self.bgr[bottom:, :]
|
||||
part_bottom_alpha = self.alpha[bottom:, :]
|
||||
|
||||
return leg_top, leg_bottom
|
||||
new_height = int((bottom - top) * self.scale)
|
||||
|
||||
def resize_leg(self, leg_top, leg_bottom):
|
||||
# 上半身
|
||||
top_part_bgr = self.bgr[:leg_top, :]
|
||||
top_part_bgr_alpha = self.alpha[:leg_top, :]
|
||||
resized_thigh = cv2.resize(part_resize, (self.w, new_height), interpolation=cv2.INTER_LINEAR)
|
||||
resized_thigh_alpha = cv2.resize(part_resize_alpha, (self.w, new_height), interpolation=cv2.INTER_LINEAR)
|
||||
|
||||
# 小腿
|
||||
part_thigh = self.bgr[leg_top:leg_bottom, :]
|
||||
part_thigh_alpha = self.alpha[leg_top:leg_bottom, :]
|
||||
|
||||
# 大腿
|
||||
part_calf = self.bgr[leg_bottom:, :]
|
||||
part_calf_alpha = self.alpha[leg_bottom:, :]
|
||||
|
||||
new_thigh_height = int((leg_bottom - leg_top) * self.scale[0])
|
||||
new_calf_height = int((self.h - leg_bottom) * self.scale[1])
|
||||
|
||||
resized_thigh = cv2.resize(part_thigh, (self.w, new_thigh_height), interpolation=cv2.INTER_LINEAR)
|
||||
resized_thigh_alpha = cv2.resize(part_thigh_alpha, (self.w, new_thigh_height), interpolation=cv2.INTER_LINEAR)
|
||||
resized_calf = cv2.resize(part_calf, (self.w, new_calf_height), interpolation=cv2.INTER_LINEAR)
|
||||
resized_calf_alpha = cv2.resize(part_calf_alpha, (self.w, new_calf_height), interpolation=cv2.INTER_LINEAR)
|
||||
|
||||
new_bgr = np.vstack((top_part_bgr, resized_thigh, resized_calf))
|
||||
new_bgr_alpha = np.vstack((top_part_bgr_alpha, resized_thigh_alpha, resized_calf_alpha))
|
||||
# 组合
|
||||
new_bgr = np.vstack((top_part, resized_thigh, part_bottom))
|
||||
new_bgr_alpha = np.vstack((top_part_alpha, resized_thigh_alpha, part_bottom_alpha))
|
||||
|
||||
if self.alpha is not None:
|
||||
# 拼接 alpha 通道
|
||||
@@ -93,9 +68,11 @@ class MannequinEditService():
|
||||
if __name__ == '__main__':
|
||||
request_data = MannequinModel(
|
||||
mannequins="aida-sys-image/models/male/dc36ce58-46c3-4b6f-8787-5ca7d6fc26e6.png",
|
||||
scale=[0.75, 0.75],
|
||||
scale=0.1,
|
||||
bucket_name="test",
|
||||
mannequin_name="mannequin_name"
|
||||
mannequin_name="mannequin_name",
|
||||
top=270,
|
||||
bottom=432
|
||||
)
|
||||
service = MannequinEditService(request_data)
|
||||
print(service())
|
||||
|
||||
Reference in New Issue
Block a user