Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,9 +13,6 @@ 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
|
|
@@ -44,15 +41,32 @@ def overlay_dress(frame, dress, landmarks):
|
|
| 44 |
|
| 45 |
@app.route("/tryon", methods=["POST"])
|
| 46 |
def tryon():
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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})
|
|
|
|
| 13 |
pose = mp_pose.Pose(static_image_mode=False, min_detection_confidence=0.5)
|
| 14 |
mp_drawing = mp.solutions.drawing_utils
|
| 15 |
|
|
|
|
|
|
|
|
|
|
| 16 |
def overlay_dress(frame, dress, landmarks):
|
| 17 |
if landmarks is not None:
|
| 18 |
h, w, _ = frame.shape
|
|
|
|
| 41 |
|
| 42 |
@app.route("/tryon", methods=["POST"])
|
| 43 |
def tryon():
|
| 44 |
+
# Expect JSON with keys: 'image' (webcam) and 'dress' (uploaded dress)
|
| 45 |
+
data = request.json
|
| 46 |
+
if "image" not in data or "dress" not in data:
|
| 47 |
+
return jsonify({"error": "Both 'image' and 'dress' are required"}), 400
|
| 48 |
+
|
| 49 |
+
# Decode webcam image
|
| 50 |
+
webcam_bytes = base64.b64decode(data["image"].split(",")[1])
|
| 51 |
+
webcam_img = Image.open(io.BytesIO(webcam_bytes)).convert("RGB")
|
| 52 |
+
frame = cv2.cvtColor(np.array(webcam_img), cv2.COLOR_RGB2BGR)
|
| 53 |
+
|
| 54 |
+
# Decode uploaded dress image
|
| 55 |
+
dress_bytes = base64.b64decode(data["dress"].split(",")[1])
|
| 56 |
+
dress_img = cv2.cvtColor(np.array(Image.open(io.BytesIO(dress_bytes)).convert("RGBA")), cv2.COLOR_RGBA2BGRA)
|
| 57 |
+
|
| 58 |
+
# Pose detection
|
| 59 |
results = pose.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
| 60 |
landmarks = results.pose_landmarks.landmark if results.pose_landmarks else None
|
| 61 |
+
|
| 62 |
+
# Overlay dress
|
| 63 |
frame = overlay_dress(frame, dress_img, landmarks)
|
| 64 |
+
|
| 65 |
+
# Draw pose landmarks
|
| 66 |
if results.pose_landmarks:
|
| 67 |
mp_drawing.draw_landmarks(frame, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
|
| 68 |
+
|
| 69 |
+
# Encode result
|
| 70 |
_, buffer = cv2.imencode(".jpg", frame)
|
| 71 |
img_base64 = "data:image/jpeg;base64," + base64.b64encode(buffer).decode()
|
| 72 |
return jsonify({"image": img_base64})
|