maham234 commited on
Commit
bb5b4b9
·
verified ·
1 Parent(s): 9544912

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -23
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
- # 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})
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)