NexaClothes / model.py
NexaCoders's picture
Create model.py
60ce45d verified
Raw History Blame Contribute Delete
2.27 kB
import cv2
import numpy as np
import mediapipe as mp
from advanced_fitter import fitter
class PerfectFitModel:
def __init__(self):
self.mp_pose = mp.solutions.pose
self.pose = self.mp_pose.Pose(
static_image_mode=True,
model_complexity=2,
min_detection_confidence=0.7
)
def predict(self, person_image, clothing_path, clothing_type, size):
"""HF Spaces prediction method"""
rgb_image = cv2.cvtColor(person_image, cv2.COLOR_BGR2RGB)
results = self.pose.process(rgb_image)
if not results.pose_landmarks:
return person_image
h, w = person_image.shape[:2]
keypoints = [(lm.x * w, lm.y * h) for lm in results.pose_landmarks.landmark]
clothing_img = cv2.imread(clothing_path, cv2.IMREAD_UNCHANGED)
measurements = fitter.extract_body_measurements(keypoints)
warped_clothing, _ = fitter.warp_clothing_to_perfect_fit(
clothing_img, measurements, clothing_type
)
# Apply perfect overlay (simplified for HF)
result = self._smart_overlay(person_image, warped_clothing, keypoints, clothing_type)
return result
def _smart_overlay(self, person_img, clothing, keypoints, clothing_type):
# Simplified overlay logic for speed
LEFT_SHOULDER, RIGHT_SHOULDER, LEFT_HIP = 11, 12, 23
x1 = int(min(kp[0] for kp in keypoints[:33]) - 30)
y1 = int(min(kp[1] for kp in keypoints[:33]) - 50)
x2 = int(max(kp[0] for kp in keypoints[:33]) + 30)
y2 = int(keypoints[LEFT_HIP][1] + 40)
x1, y1 = max(0, x1), max(0, y1)
x2, y2 = min(person_img.shape[1], x2), min(person_img.shape[0], y2)
region = person_img[y1:y2, x1:x2]
clothing_resized = cv2.resize(clothing, (region.shape[1], region.shape[0]))
if len(clothing_resized.shape) == 3 and clothing_resized.shape[2] == 4:
alpha = clothing_resized[:, :, 3] / 255.0
for c in range(3):
region[:, :, c] = region[:, :, c] * (1 - alpha) + clothing_resized[:, :, c] * alpha
person_img[y1:y2, x1:x2] = region
return person_img