ptmsc commited on
Commit
0f7f5eb
·
1 Parent(s): 99959d8

call api to handle tryon

Browse files
Files changed (2) hide show
  1. app.py +81 -12
  2. requirements.txt +2 -1
app.py CHANGED
@@ -1,8 +1,11 @@
1
- import gradio as gr
 
2
  import cv2
3
- import numpy as np
4
  import mediapipe as mp
5
- import os
 
 
6
 
7
  example_path = os.path.join(os.path.dirname(__file__), 'example')
8
 
@@ -55,12 +58,78 @@ def detect_pose(image):
55
  return image
56
 
57
 
58
- def process_image(human_img):
59
- # Convert PIL image to NumPy array
60
- human_img = np.array(human_img)
 
61
 
62
- processed_image = detect_pose(human_img)
63
- return processed_image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
 
66
  image_blocks = gr.Blocks().queue()
@@ -69,7 +138,7 @@ with image_blocks as demo:
69
  gr.HTML("<center><p>Upload an image of a person and an image of a garment ✨</p></center>")
70
  with gr.Row():
71
  with gr.Column():
72
- human_img = gr.Image(type="pil", label='Human', interactive=True)
73
  example = gr.Examples(
74
  inputs=human_img,
75
  examples_per_page=10,
@@ -77,7 +146,7 @@ with image_blocks as demo:
77
  )
78
 
79
  with gr.Column():
80
- garm_img = gr.Image(label="Garment", type="pil", interactive=True)
81
  example = gr.Examples(
82
  inputs=garm_img,
83
  examples_per_page=8,
@@ -89,6 +158,6 @@ with image_blocks as demo:
89
  try_button = gr.Button(value="Try-on", variant='primary')
90
 
91
  # Linking the button to the processing function
92
- try_button.click(fn=process_image, inputs=human_img, outputs=image_out)
93
 
94
- image_blocks.launch()
 
1
+ import os
2
+
3
  import cv2
4
+ import gradio as gr
5
  import mediapipe as mp
6
+ import numpy as np
7
+ from PIL import Image
8
+ from gradio_client import Client, handle_file
9
 
10
  example_path = os.path.join(os.path.dirname(__file__), 'example')
11
 
 
58
  return image
59
 
60
 
61
+ def align_clothing(body_img, clothing_img):
62
+ image_rgb = cv2.cvtColor(body_img, cv2.COLOR_BGR2RGB)
63
+ result = pose.process(image_rgb)
64
+ output = body_img.copy()
65
 
66
+ if result.pose_landmarks:
67
+ h, w, _ = output.shape
68
+
69
+ # Extract key points
70
+ def get_point(landmark_id):
71
+ lm = result.pose_landmarks.landmark[landmark_id]
72
+ return int(lm.x * w), int(lm.y * h)
73
+
74
+ left_shoulder = get_point(mp_pose_landmark.LEFT_SHOULDER)
75
+ right_shoulder = get_point(mp_pose_landmark.RIGHT_SHOULDER)
76
+ left_hip = get_point(mp_pose_landmark.LEFT_HIP)
77
+ right_hip = get_point(mp_pose_landmark.RIGHT_HIP)
78
+
79
+ # Destination box (torso region)
80
+ dst_pts = np.array([
81
+ left_shoulder,
82
+ right_shoulder,
83
+ right_hip,
84
+ left_hip
85
+ ], dtype=np.float32)
86
+
87
+ # Source box (clothing image corners)
88
+ src_h, src_w = clothing_img.shape[:2]
89
+ src_pts = np.array([
90
+ [0, 0],
91
+ [src_w, 0],
92
+ [src_w, src_h],
93
+ [0, src_h]
94
+ ], dtype=np.float32)
95
+
96
+ # Compute perspective transform and warp
97
+ matrix = cv2.getPerspectiveTransform(src_pts, dst_pts)
98
+ warped_clothing = cv2.warpPerspective(clothing_img, matrix, (w, h), borderMode=cv2.BORDER_TRANSPARENT)
99
+
100
+ # Handle transparency
101
+ if clothing_img.shape[2] == 4:
102
+ alpha = warped_clothing[:, :, 3] / 255.0
103
+ for c in range(3):
104
+ output[:, :, c] = (1 - alpha) * output[:, :, c] + alpha * warped_clothing[:, :, c]
105
+ else:
106
+ output = cv2.addWeighted(output, 0.8, warped_clothing, 0.5, 0)
107
+
108
+ return output
109
+
110
+
111
+ def process_image(human_img_path, garm_img_path):
112
+ client = Client("franciszzj/Leffa")
113
+
114
+ result = client.predict(
115
+ src_image_path=handle_file(human_img_path),
116
+ ref_image_path=handle_file(garm_img_path),
117
+ ref_acceleration=False,
118
+ step=30,
119
+ scale=2.5,
120
+ seed=42,
121
+ vt_model_type="viton_hd",
122
+ vt_garment_type="upper_body",
123
+ vt_repaint=False,
124
+ api_name="/leffa_predict_vt"
125
+ )
126
+
127
+ print(result)
128
+ generated_image_path = result[0]
129
+ print("generated_image_path" + generated_image_path)
130
+ generated_image = Image.open(generated_image_path)
131
+
132
+ return generated_image
133
 
134
 
135
  image_blocks = gr.Blocks().queue()
 
138
  gr.HTML("<center><p>Upload an image of a person and an image of a garment ✨</p></center>")
139
  with gr.Row():
140
  with gr.Column():
141
+ human_img = gr.Image(type="filepath", label='Human', interactive=True)
142
  example = gr.Examples(
143
  inputs=human_img,
144
  examples_per_page=10,
 
146
  )
147
 
148
  with gr.Column():
149
+ garm_img = gr.Image(label="Garment", type="filepath", interactive=True)
150
  example = gr.Examples(
151
  inputs=garm_img,
152
  examples_per_page=8,
 
158
  try_button = gr.Button(value="Try-on", variant='primary')
159
 
160
  # Linking the button to the processing function
161
+ try_button.click(fn=process_image, inputs=[human_img, garm_img], outputs=image_out)
162
 
163
+ image_blocks.launch(show_error=True)
requirements.txt CHANGED
@@ -3,4 +3,5 @@ numpy==1.26.4
3
  opencv-contrib-python==4.11.0.86
4
  opencv-python==4.11.0.86
5
  gradio==5.23.3
6
- gradio_client==1.8.0
 
 
3
  opencv-contrib-python==4.11.0.86
4
  opencv-python==4.11.0.86
5
  gradio==5.23.3
6
+ gradio_client==1.8.0
7
+