Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ from io import BytesIO
|
|
5 |
import torch
|
6 |
import base64
|
7 |
import io
|
|
|
8 |
import gradio as gr
|
9 |
import numpy as np
|
10 |
import spaces
|
@@ -149,7 +150,7 @@ def start_tryon_full_body(tops_img, bottoms_img, model_parse_tops, model_parse_b
|
|
149 |
human_img_arg_tops = _apply_exif_orientation(human_img_orig_tops.resize((384, 512)))
|
150 |
human_img_arg_tops = convert_PIL_to_numpy(human_img_arg_tops, format="BGR")
|
151 |
|
152 |
-
args = apply_net.create_argument_parser().parse_args(
|
153 |
pose_img_tops = args.func(args, human_img_arg_tops)
|
154 |
pose_img_tops = pose_img_tops[:, :, ::-1]
|
155 |
pose_img_tops = Image.fromarray(pose_img_tops).resize((768, 1024))
|
@@ -367,16 +368,24 @@ def start_tryon(dict, garm_img, garment_des, is_checked, is_checked_crop, denois
|
|
367 |
return images[0], mask_gray
|
368 |
|
369 |
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
|
376 |
-
|
377 |
-
|
378 |
-
|
379 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
380 |
|
381 |
@app.route('/tryon', methods=['POST'])
|
382 |
def tryon():
|
@@ -409,41 +418,45 @@ def tryon():
|
|
409 |
|
410 |
@app.route('/tryon-full', methods=['POST'])
|
411 |
def tryon_full():
|
412 |
-
|
413 |
-
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
-
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
-
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
-
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
-
|
432 |
-
|
433 |
-
|
434 |
-
|
435 |
-
|
436 |
-
|
437 |
-
|
438 |
-
|
439 |
-
|
440 |
-
|
441 |
-
|
442 |
-
|
443 |
-
|
444 |
-
|
445 |
-
|
446 |
-
|
|
|
|
|
|
|
|
|
447 |
|
448 |
if __name__ == "__main__":
|
449 |
app.run(debug=True, host="0.0.0.0", port=7860)
|
|
|
5 |
import torch
|
6 |
import base64
|
7 |
import io
|
8 |
+
import logging
|
9 |
import gradio as gr
|
10 |
import numpy as np
|
11 |
import spaces
|
|
|
150 |
human_img_arg_tops = _apply_exif_orientation(human_img_orig_tops.resize((384, 512)))
|
151 |
human_img_arg_tops = convert_PIL_to_numpy(human_img_arg_tops, format="BGR")
|
152 |
|
153 |
+
args = apply_net.create_argument_parser().parse_args(['show', './configs/densepose_rcnn_R_50_FPN_s1x.yaml', './ckpt/densepose/model_final_162be9.pkl', 'dp_segm', '-v', '--opts', 'MODEL.DEVICE', 'cuda'])
|
154 |
pose_img_tops = args.func(args, human_img_arg_tops)
|
155 |
pose_img_tops = pose_img_tops[:, :, ::-1]
|
156 |
pose_img_tops = Image.fromarray(pose_img_tops).resize((768, 1024))
|
|
|
368 |
return images[0], mask_gray
|
369 |
|
370 |
|
371 |
+
def decode_image_from_base64(base64_str):
|
372 |
+
try:
|
373 |
+
img_data = base64.b64decode(base64_str)
|
374 |
+
img = Image.open(BytesIO(img_data))
|
375 |
+
return img
|
376 |
+
except Exception as e:
|
377 |
+
logging.error(f"Error decoding image: {e}")
|
378 |
+
raise
|
379 |
+
|
380 |
+
def encode_image_to_base64(img):
|
381 |
+
try:
|
382 |
+
buffered = BytesIO()
|
383 |
+
img.save(buffered, format="PNG")
|
384 |
+
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
385 |
+
return img_str
|
386 |
+
except Exception as e:
|
387 |
+
logging.error(f"Error encoding image: {e}")
|
388 |
+
raise
|
389 |
|
390 |
@app.route('/tryon', methods=['POST'])
|
391 |
def tryon():
|
|
|
418 |
|
419 |
@app.route('/tryon-full', methods=['POST'])
|
420 |
def tryon_full():
|
421 |
+
try:
|
422 |
+
data = request.json
|
423 |
+
|
424 |
+
# Decode images
|
425 |
+
tops_image = decode_image_from_base64(data['tops_image'])
|
426 |
+
bottoms_image = decode_image_from_base64(data['bottoms_image'])
|
427 |
+
model_parse_tops = decode_image_from_base64(data['model_parse_tops'])
|
428 |
+
model_parse_bottoms = decode_image_from_base64(data['model_parse_bottoms'])
|
429 |
+
|
430 |
+
# Retrieve additional parameters
|
431 |
+
is_checked = data.get('use_auto_mask', True)
|
432 |
+
is_checked_crop = data.get('use_auto_crop', False)
|
433 |
+
denoise_steps = int(data.get('denoise_steps', 30))
|
434 |
+
seed = int(data.get('seed', 42))
|
435 |
+
|
436 |
+
# Call the start_tryon_full_body function
|
437 |
+
output_image, mask_image = start_tryon_full_body(
|
438 |
+
tops_image,
|
439 |
+
bottoms_image,
|
440 |
+
model_parse_tops,
|
441 |
+
model_parse_bottoms,
|
442 |
+
is_checked,
|
443 |
+
is_checked_crop,
|
444 |
+
denoise_steps,
|
445 |
+
seed
|
446 |
+
)
|
447 |
+
|
448 |
+
# Convert output image to base64
|
449 |
+
output_base64 = encode_image_to_base64(output_image)
|
450 |
+
mask_base64 = encode_image_to_base64(mask_image)
|
451 |
+
|
452 |
+
return jsonify({
|
453 |
+
'output_image': output_base64,
|
454 |
+
'mask_image': mask_base64
|
455 |
+
})
|
456 |
+
|
457 |
+
except Exception as e:
|
458 |
+
logging.error(f"Error in /tryon-full: {e}")
|
459 |
+
return jsonify({'error': str(e)}), 200
|
460 |
|
461 |
if __name__ == "__main__":
|
462 |
app.run(debug=True, host="0.0.0.0", port=7860)
|