Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, request, jsonify
|
| 2 |
+
import cv2
|
| 3 |
+
import mediapipe as mp
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import base64
|
| 7 |
+
import io
|
| 8 |
+
|
| 9 |
+
app = Flask(__name__)
|
| 10 |
+
|
| 11 |
+
# MediaPipe Pose
|
| 12 |
+
mp_pose = mp.solutions.pose
|
| 13 |
+
pose = mp_pose.Pose(static_image_mode=False, min_detection_confidence=0.5)
|
| 14 |
+
mp_drawing = mp.solutions.drawing_utils
|
| 15 |
+
|
| 16 |
+
# Load dress
|
| 17 |
+
dress_img = cv2.imread("dress.png", cv2.IMREAD_UNCHANGED)
|
| 18 |
+
|
| 19 |
+
def overlay_dress(frame, dress, landmarks):
|
| 20 |
+
if landmarks is not None:
|
| 21 |
+
h, w, _ = frame.shape
|
| 22 |
+
def to_pixel(lm):
|
| 23 |
+
return int(lm.x * w), int(lm.y * h)
|
| 24 |
+
left_shoulder = to_pixel(landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value])
|
| 25 |
+
right_shoulder = to_pixel(landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value])
|
| 26 |
+
left_hip = to_pixel(landmarks[mp_pose.PoseLandmark.LEFT_HIP.value])
|
| 27 |
+
right_hip = to_pixel(landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value])
|
| 28 |
+
dress_width = int(np.linalg.norm(np.array(left_shoulder)-np.array(right_shoulder))*1.8)
|
| 29 |
+
top_shoulder_y = min(left_shoulder[1], right_shoulder[1])
|
| 30 |
+
bottom_hip_y = max(left_hip[1], right_hip[1])
|
| 31 |
+
dress_height = int((bottom_hip_y - top_shoulder_y)*1.2)
|
| 32 |
+
center_x = (left_shoulder[0]+right_shoulder[0])//2
|
| 33 |
+
x1 = max(center_x - dress_width//2,0)
|
| 34 |
+
y1 = max(top_shoulder_y - 30,0)
|
| 35 |
+
x2 = min(x1 + dress_width, w)
|
| 36 |
+
y2 = min(y1 + dress_height, h)
|
| 37 |
+
dress_resized = cv2.resize(dress, (x2-x1, y2-y1), interpolation=cv2.INTER_AREA)
|
| 38 |
+
if dress_resized.shape[2]==4:
|
| 39 |
+
alpha_s = dress_resized[:,:,3]/255.0
|
| 40 |
+
alpha_l = 1.0 - alpha_s
|
| 41 |
+
for c in range(3):
|
| 42 |
+
frame[y1:y2,x1:x2,c] = alpha_s*dress_resized[:,:,c] + alpha_l*frame[y1:y2,x1:x2,c]
|
| 43 |
+
return frame
|
| 44 |
+
|
| 45 |
+
@app.route("/tryon", methods=["POST"])
|
| 46 |
+
def tryon():
|
| 47 |
+
data = request.json["image"]
|
| 48 |
+
img_bytes = base64.b64decode(data.split(",")[1])
|
| 49 |
+
img = Image.open(io.BytesIO(img_bytes)).convert("RGB")
|
| 50 |
+
frame = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
|
| 51 |
+
results = pose.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
| 52 |
+
landmarks = results.pose_landmarks.landmark if results.pose_landmarks else None
|
| 53 |
+
frame = overlay_dress(frame, dress_img, landmarks)
|
| 54 |
+
if results.pose_landmarks:
|
| 55 |
+
mp_drawing.draw_landmarks(frame, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
|
| 56 |
+
_, buffer = cv2.imencode(".jpg", frame)
|
| 57 |
+
img_base64 = "data:image/jpeg;base64," + base64.b64encode(buffer).decode()
|
| 58 |
+
return jsonify({"image": img_base64})
|
| 59 |
+
|
| 60 |
+
if __name__=="__main__":
|
| 61 |
+
app.run(host="0.0.0.0", port=5000)
|