Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -13,6 +13,7 @@ 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 |
def overlay_dress(frame, dress, landmarks):
|
| 17 |
if landmarks is not None:
|
| 18 |
h, w, _ = frame.shape
|
|
@@ -39,37 +40,45 @@ def overlay_dress(frame, dress, landmarks):
|
|
| 39 |
frame[y1:y2,x1:x2,c] = alpha_s*dress_resized[:,:,c] + alpha_l*frame[y1:y2,x1:x2,c]
|
| 40 |
return frame
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
@app.route("/tryon", methods=["POST"])
|
| 43 |
def tryon():
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
frame = cv2.cvtColor(np.array(webcam_img), cv2.COLOR_RGB2BGR)
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
landmarks = results.pose_landmarks.landmark if results.pose_landmarks else None
|
| 61 |
|
| 62 |
-
|
| 63 |
-
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
return jsonify({"image": img_base64})
|
| 73 |
|
| 74 |
if __name__=="__main__":
|
| 75 |
app.run(host="0.0.0.0", port=5000)
|
|
|
|
| 13 |
pose = mp_pose.Pose(static_image_mode=False, min_detection_confidence=0.5)
|
| 14 |
mp_drawing = mp.solutions.drawing_utils
|
| 15 |
|
| 16 |
+
# Overlay function
|
| 17 |
def overlay_dress(frame, dress, landmarks):
|
| 18 |
if landmarks is not None:
|
| 19 |
h, w, _ = frame.shape
|
|
|
|
| 40 |
frame[y1:y2,x1:x2,c] = alpha_s*dress_resized[:,:,c] + alpha_l*frame[y1:y2,x1:x2,c]
|
| 41 |
return frame
|
| 42 |
|
| 43 |
+
# Simple home route for HF Spaces detection
|
| 44 |
+
@app.route("/")
|
| 45 |
+
def home():
|
| 46 |
+
return "<h1>Flask API is running!</h1>"
|
| 47 |
+
|
| 48 |
+
# Virtual try-on API
|
| 49 |
@app.route("/tryon", methods=["POST"])
|
| 50 |
def tryon():
|
| 51 |
+
try:
|
| 52 |
+
data = request.json["image"] # Base64 string of user webcam
|
| 53 |
+
dress_data = request.json["dress"] # Base64 string of uploaded dress
|
| 54 |
+
|
| 55 |
+
# Decode user image
|
| 56 |
+
img_bytes = base64.b64decode(data.split(",")[1])
|
| 57 |
+
img = Image.open(io.BytesIO(img_bytes)).convert("RGB")
|
| 58 |
+
frame = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
|
| 59 |
|
| 60 |
+
# Decode dress image
|
| 61 |
+
dress_bytes = base64.b64decode(dress_data.split(",")[1])
|
| 62 |
+
dress_img = cv2.imdecode(np.frombuffer(dress_bytes, np.uint8), cv2.IMREAD_UNCHANGED)
|
|
|
|
| 63 |
|
| 64 |
+
# Pose detection
|
| 65 |
+
results = pose.process(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))
|
| 66 |
+
landmarks = results.pose_landmarks.landmark if results.pose_landmarks else None
|
| 67 |
|
| 68 |
+
# Overlay dress
|
| 69 |
+
frame = overlay_dress(frame, dress_img, landmarks)
|
|
|
|
| 70 |
|
| 71 |
+
# Draw pose landmarks
|
| 72 |
+
if results.pose_landmarks:
|
| 73 |
+
mp_drawing.draw_landmarks(frame, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
|
| 74 |
|
| 75 |
+
# Encode output
|
| 76 |
+
_, buffer = cv2.imencode(".jpg", frame)
|
| 77 |
+
img_base64 = "data:image/jpeg;base64," + base64.b64encode(buffer).decode()
|
| 78 |
|
| 79 |
+
return jsonify({"image": img_base64})
|
| 80 |
+
except Exception as e:
|
| 81 |
+
return jsonify({"error": str(e)})
|
|
|
|
| 82 |
|
| 83 |
if __name__=="__main__":
|
| 84 |
app.run(host="0.0.0.0", port=5000)
|