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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -12
app.py CHANGED
@@ -5,8 +5,10 @@ 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
@@ -19,29 +21,36 @@ def overlay_dress(frame, dress, landmarks):
19
  h, w, _ = frame.shape
20
  def to_pixel(lm):
21
  return int(lm.x * w), int(lm.y * h)
 
22
  left_shoulder = to_pixel(landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value])
23
  right_shoulder = to_pixel(landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value])
24
  left_hip = to_pixel(landmarks[mp_pose.PoseLandmark.LEFT_HIP.value])
25
  right_hip = to_pixel(landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value])
 
 
26
  dress_width = int(np.linalg.norm(np.array(left_shoulder)-np.array(right_shoulder))*1.8)
27
  top_shoulder_y = min(left_shoulder[1], right_shoulder[1])
28
  bottom_hip_y = max(left_hip[1], right_hip[1])
29
  dress_height = int((bottom_hip_y - top_shoulder_y)*1.2)
 
30
  center_x = (left_shoulder[0]+right_shoulder[0])//2
31
- x1 = max(center_x - dress_width//2,0)
32
- y1 = max(top_shoulder_y - 30,0)
33
  x2 = min(x1 + dress_width, w)
34
  y2 = min(y1 + dress_height, h)
 
 
35
  dress_resized = cv2.resize(dress, (x2-x1, y2-y1), interpolation=cv2.INTER_AREA)
36
- if dress_resized.shape[2]==4:
37
- alpha_s = dress_resized[:,:,3]/255.0
38
  alpha_l = 1.0 - alpha_s
39
  for c in range(3):
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
 
@@ -49,8 +58,11 @@ def home():
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])
@@ -72,13 +84,14 @@ def tryon():
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)
 
5
  from PIL import Image
6
  import base64
7
  import io
8
+ from flask_cors import CORS
9
 
10
  app = Flask(__name__)
11
+ CORS(app) # Allow cross-origin requests from Flutter app
12
 
13
  # MediaPipe Pose
14
  mp_pose = mp.solutions.pose
 
21
  h, w, _ = frame.shape
22
  def to_pixel(lm):
23
  return int(lm.x * w), int(lm.y * h)
24
+
25
  left_shoulder = to_pixel(landmarks[mp_pose.PoseLandmark.LEFT_SHOULDER.value])
26
  right_shoulder = to_pixel(landmarks[mp_pose.PoseLandmark.RIGHT_SHOULDER.value])
27
  left_hip = to_pixel(landmarks[mp_pose.PoseLandmark.LEFT_HIP.value])
28
  right_hip = to_pixel(landmarks[mp_pose.PoseLandmark.RIGHT_HIP.value])
29
+
30
+ # Dress size & position
31
  dress_width = int(np.linalg.norm(np.array(left_shoulder)-np.array(right_shoulder))*1.8)
32
  top_shoulder_y = min(left_shoulder[1], right_shoulder[1])
33
  bottom_hip_y = max(left_hip[1], right_hip[1])
34
  dress_height = int((bottom_hip_y - top_shoulder_y)*1.2)
35
+
36
  center_x = (left_shoulder[0]+right_shoulder[0])//2
37
+ x1 = max(center_x - dress_width//2, 0)
38
+ y1 = max(top_shoulder_y - 30, 0)
39
  x2 = min(x1 + dress_width, w)
40
  y2 = min(y1 + dress_height, h)
41
+
42
+ # Resize and blend dress
43
  dress_resized = cv2.resize(dress, (x2-x1, y2-y1), interpolation=cv2.INTER_AREA)
44
+ if dress_resized.shape[2] == 4: # has alpha channel
45
+ alpha_s = dress_resized[:, :, 3] / 255.0
46
  alpha_l = 1.0 - alpha_s
47
  for c in range(3):
48
+ frame[y1:y2, x1:x2, c] = alpha_s*dress_resized[:, :, c] + alpha_l*frame[y1:y2, x1:x2, c]
49
+
50
  return frame
51
 
52
+ # Home route for HF Spaces detection
53
+ @app.route("/", methods=["GET"])
54
  def home():
55
  return "<h1>Flask API is running!</h1>"
56
 
 
58
  @app.route("/tryon", methods=["POST"])
59
  def tryon():
60
  try:
61
+ data = request.json.get("image") # User webcam image in base64
62
+ dress_data = request.json.get("dress") # Dress image in base64
63
+
64
+ if not data or not dress_data:
65
+ return jsonify({"error": "Missing image or dress data"}), 400
66
 
67
  # Decode user image
68
  img_bytes = base64.b64decode(data.split(",")[1])
 
84
  if results.pose_landmarks:
85
  mp_drawing.draw_landmarks(frame, results.pose_landmarks, mp_pose.POSE_CONNECTIONS)
86
 
87
+ # Encode output to base64
88
  _, buffer = cv2.imencode(".jpg", frame)
89
  img_base64 = "data:image/jpeg;base64," + base64.b64encode(buffer).decode()
90
 
91
  return jsonify({"image": img_base64})
92
+
93
  except Exception as e:
94
+ return jsonify({"error": str(e)}), 500
95
 
96
+ if __name__ == "__main__":
97
  app.run(host="0.0.0.0", port=5000)