sahadev10 commited on
Commit
e212ae5
·
verified ·
1 Parent(s): 4205306

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -15
app.py CHANGED
@@ -132,16 +132,22 @@ import base64
132
  import requests
133
  from io import BytesIO
134
 
135
- # Function to process the image and calculate measurements
136
- def process_image(image, user_height_cm):
137
- # Check if the user height is valid
138
- if user_height_cm == 0 or user_height_cm is None:
139
- return "User height cannot be zero or None. Please provide a valid height.", None
140
 
 
 
 
 
 
 
 
 
 
 
 
141
  # Convert Gradio image input to OpenCV format
142
  image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
143
 
144
- # Run keypoint detection (using your predictor)
145
  outputs = predictor(image)
146
 
147
  # Extract keypoints
@@ -152,7 +158,6 @@ def process_image(image, user_height_cm):
152
  return "No keypoints detected.", None
153
 
154
  # Save keypoints to JSON
155
- output_file = "keypoints.json"
156
  with open(output_file, "w") as f:
157
  json.dump({"keypoints": keypoints}, f, indent=4)
158
 
@@ -186,18 +191,10 @@ def process_image(image, user_height_cm):
186
  ankle_mid = ((keypoints[L_ANKLE] + keypoints[R_ANKLE]) / 2).tolist()
187
  pixel_height = get_distance(keypoints[NOSE], ankle_mid)
188
 
189
- # Check if pixel height is non-zero to avoid divide by zero error
190
- if pixel_height == 0:
191
- return "Error in calculating pixel height. Please check the input image.", None
192
-
193
  # Estimated full body height (add approx head length)
194
  estimated_full_pixel_height = pixel_height / 0.87 # Since 87% = nose to ankle
195
  pixels_per_cm = estimated_full_pixel_height / user_height_cm
196
 
197
- # Ensure pixels_per_cm is valid (non-zero)
198
- if pixels_per_cm == 0:
199
- return "Error in calculating pixels per cm. Ensure the user height is correct.", None
200
-
201
  # Waist and shoulder measurements
202
  shoulder_width_px = get_distance(keypoints[L_SHOULDER], keypoints[R_SHOULDER])
203
  waist_width_px = get_distance(keypoints[L_HIP], keypoints[R_HIP])
@@ -234,6 +231,7 @@ def process_image(image, user_height_cm):
234
  measurements = calculate_body_measurements(waist_circumference, hip_circumference, shoulder_width_cm, torso_length_cm, arm_length_cm)
235
 
236
  return measurements, cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
 
237
 
238
  # Save to database function
239
  def save_to_database(measurements, image, user_height_cm):
 
132
  import requests
133
  from io import BytesIO
134
 
 
 
 
 
 
135
 
136
+
137
+ # Load pre-trained Keypoint R-CNN model
138
+ cfg = get_cfg()
139
+ cfg.merge_from_file(model_zoo.get_config_file("COCO-Keypoints/keypoint_rcnn_R_50_FPN_3x.yaml"))
140
+ cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-Keypoints/keypoint_rcnn_R_50_FPN_3x.yaml")
141
+ cfg.MODEL.DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
142
+
143
+ # Load the predictor
144
+ predictor = DefaultPredictor(cfg)
145
+
146
+ def process_image(image, user_height_cm):
147
  # Convert Gradio image input to OpenCV format
148
  image = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
149
 
150
+ # Run keypoint detection
151
  outputs = predictor(image)
152
 
153
  # Extract keypoints
 
158
  return "No keypoints detected.", None
159
 
160
  # Save keypoints to JSON
 
161
  with open(output_file, "w") as f:
162
  json.dump({"keypoints": keypoints}, f, indent=4)
163
 
 
191
  ankle_mid = ((keypoints[L_ANKLE] + keypoints[R_ANKLE]) / 2).tolist()
192
  pixel_height = get_distance(keypoints[NOSE], ankle_mid)
193
 
 
 
 
 
194
  # Estimated full body height (add approx head length)
195
  estimated_full_pixel_height = pixel_height / 0.87 # Since 87% = nose to ankle
196
  pixels_per_cm = estimated_full_pixel_height / user_height_cm
197
 
 
 
 
 
198
  # Waist and shoulder measurements
199
  shoulder_width_px = get_distance(keypoints[L_SHOULDER], keypoints[R_SHOULDER])
200
  waist_width_px = get_distance(keypoints[L_HIP], keypoints[R_HIP])
 
231
  measurements = calculate_body_measurements(waist_circumference, hip_circumference, shoulder_width_cm, torso_length_cm, arm_length_cm)
232
 
233
  return measurements, cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
234
+
235
 
236
  # Save to database function
237
  def save_to_database(measurements, image, user_height_cm):