Spaces:
Saad0KH
/
Running on Zero

Saad0KH commited on
Commit
eb7b596
ยท
verified ยท
1 Parent(s): c761dc5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -0
app.py CHANGED
@@ -122,6 +122,45 @@ def pil_to_binary_mask(pil_image, threshold=0):
122
  output_mask = Image.fromarray(mask)
123
  return output_mask
124
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
125
  @spaces.GPU
126
  def start_tryon(dict, garm_img, garment_des, is_checked, is_checked_crop, denoise_steps, seed):
127
  device = "cuda"
@@ -270,6 +309,37 @@ def tryon():
270
  'mask_image': mask_base64
271
  })
272
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
273
  if __name__ == "__main__":
274
  app.run(debug=True, host="0.0.0.0", port=7860)
275
 
 
122
  output_mask = Image.fromarray(mask)
123
  return output_mask
124
 
125
+ @spaces.GPU
126
+ def start_tryon_full_body(tops_image: Image.Image, bottoms_image: Image.Image, model_parse_tops: Image.Image, model_parse_bottoms: Image.Image, keypoints_tops: dict, keypoints_bottoms: dict):
127
+ """
128
+ Combines tops and bottoms images into a single output image after processing with get_mask_location.
129
+ """
130
+
131
+ # Get mask for the tops (upper body)
132
+ mask_tops, _ = get_mask_location('hd', "upper_body", model_parse_tops, keypoints_tops)
133
+
134
+ # Get mask for the bottoms (lower body)
135
+ mask_bottoms, _ = get_mask_location('hd', "lower_body", model_parse_bottoms, keypoints_bottoms)
136
+
137
+ # Convert masks to NumPy arrays
138
+ mask_tops_np = np.array(mask_tops)
139
+ mask_bottoms_np = np.array(mask_bottoms)
140
+
141
+ # Convert tops and bottoms images to NumPy arrays
142
+ tops_np = np.array(tops_image)
143
+ bottoms_np = np.array(bottoms_image)
144
+
145
+ # Ensure that tops and bottoms images have the same dimensions as their masks
146
+ tops_resized = cv2.resize(tops_np, (mask_tops_np.shape[1], mask_tops_np.shape[0]))
147
+ bottoms_resized = cv2.resize(bottoms_np, (mask_bottoms_np.shape[1], mask_bottoms_np.shape[0]))
148
+
149
+ # Create a blank canvas for the final output image
150
+ combined_image = np.zeros_like(tops_resized)
151
+
152
+ # Apply the tops mask to the combined image
153
+ combined_image[mask_tops_np > 0] = tops_resized[mask_tops_np > 0]
154
+
155
+ # Apply the bottoms mask to the combined image
156
+ combined_image[mask_bottoms_np > 0] = bottoms_resized[mask_bottoms_np > 0]
157
+
158
+ # Convert the final combined image back to a PIL image
159
+ combined_image_pil = Image.fromarray(combined_image)
160
+
161
+ return combined_image_pil
162
+
163
+
164
  @spaces.GPU
165
  def start_tryon(dict, garm_img, garment_des, is_checked, is_checked_crop, denoise_steps, seed):
166
  device = "cuda"
 
309
  'mask_image': mask_base64
310
  })
311
 
312
+ @app.route('/tryon-full', methods=['POST'])
313
+ def tryon_full():
314
+ data = request.json
315
+
316
+ # Decode input images
317
+ tops_image = decode_image_from_base64(data['tops_image'])
318
+ bottoms_image = decode_image_from_base64(data['bottoms_image'])
319
+ model_parse_tops = decode_image_from_base64(data['model_parse_tops'])
320
+ model_parse_bottoms = decode_image_from_base64(data['model_parse_bottoms'])
321
+
322
+ # Decode keypoints
323
+ keypoints_tops = data.get('keypoints_tops', {})
324
+ keypoints_bottoms = data.get('keypoints_bottoms', {})
325
+
326
+ # Call the start_tryon function
327
+ output_image = start_tryon_full_body(
328
+ tops_image,
329
+ bottoms_image,
330
+ model_parse_tops,
331
+ model_parse_bottoms,
332
+ keypoints_tops,
333
+ keypoints_bottoms
334
+ )
335
+
336
+ # Convert output image to base64
337
+ output_base64 = encode_image_to_base64(output_image)
338
+
339
+ return jsonify({
340
+ 'output_image': output_base64
341
+ })
342
+
343
  if __name__ == "__main__":
344
  app.run(debug=True, host="0.0.0.0", port=7860)
345