Spaces:
Sleeping
Sleeping
initial app test
Browse files
app.py
CHANGED
@@ -135,54 +135,48 @@ def create_scene(mesh, img, focal_length=500, camera_center=250, img_res=500):
|
|
135 |
IMG.thumbnail((3000, 3000))
|
136 |
return IMG
|
137 |
|
138 |
-
def main(img_src, out_dir, model_path='checkpoint/deco_best.pth', mesh_colour=[130, 130, 130, 255], annot_colour=[0, 255, 0, 255]):
|
139 |
-
if os.path.isdir(img_src):
|
140 |
-
images = glob.iglob(img_src + '/*', recursive=True)
|
141 |
-
else:
|
142 |
-
images = [img_src]
|
143 |
-
|
144 |
deco_model = initiate_model(model_path)
|
145 |
|
146 |
smpl_path = os.path.join(constants.SMPL_MODEL_DIR, 'smpl_neutral_tpose.ply')
|
147 |
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
cont_smpl.append(indx)
|
161 |
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
|
167 |
-
|
168 |
-
|
169 |
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
|
179 |
-
|
180 |
-
|
181 |
|
182 |
-
|
183 |
-
|
184 |
|
185 |
-
return
|
186 |
|
187 |
with gr.Blocks(title="DECO", css=".gradio-container") as demo:
|
188 |
|
@@ -197,20 +191,17 @@ with gr.Blocks(title="DECO", css=".gradio-container") as demo:
|
|
197 |
|
198 |
gr.HTML("""<br/>""")
|
199 |
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
# send_btn.click(fn=main, inputs=[input_image, threshold], outputs=[output_image, output_meshes])
|
204 |
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
# inputs=[input_image, 0.6])
|
213 |
|
214 |
|
215 |
-
#demo.queue()
|
216 |
demo.launch(debug=True)
|
|
|
135 |
IMG.thumbnail((3000, 3000))
|
136 |
return IMG
|
137 |
|
138 |
+
def main(img_src, out_dir='demo_out', model_path='checkpoint/deco_best.pth', mesh_colour=[130, 130, 130, 255], annot_colour=[0, 255, 0, 255]):
|
|
|
|
|
|
|
|
|
|
|
139 |
deco_model = initiate_model(model_path)
|
140 |
|
141 |
smpl_path = os.path.join(constants.SMPL_MODEL_DIR, 'smpl_neutral_tpose.ply')
|
142 |
|
143 |
+
img = cv2.imread(img_src)
|
144 |
+
img = cv2.resize(img, (256, 256), cv2.INTER_CUBIC)
|
145 |
+
img = img.transpose(2,0,1)/255.0
|
146 |
+
img = img[np.newaxis,:,:,:]
|
147 |
+
img = torch.tensor(img, dtype = torch.float32).to(device)
|
148 |
+
|
149 |
+
cont, _, _ = deco_model(img)
|
150 |
+
cont = cont.detach().cpu().numpy().squeeze()
|
151 |
+
cont_smpl = []
|
152 |
+
for indx, i in enumerate(cont):
|
153 |
+
if i >= 0.5:
|
154 |
+
cont_smpl.append(indx)
|
|
|
155 |
|
156 |
+
img = img.detach().cpu().numpy()
|
157 |
+
img = np.transpose(img[0], (1, 2, 0))
|
158 |
+
img = img * 255
|
159 |
+
img = img.astype(np.uint8)
|
160 |
|
161 |
+
contact_smpl = np.zeros((1, 1, 6890))
|
162 |
+
contact_smpl[0][0][cont_smpl] = 1
|
163 |
|
164 |
+
body_model_smpl = trimesh.load(smpl_path, process=False)
|
165 |
+
for vert in range(body_model_smpl.visual.vertex_colors.shape[0]):
|
166 |
+
body_model_smpl.visual.vertex_colors[vert] = mesh_colour
|
167 |
+
body_model_smpl.visual.vertex_colors[cont_smpl] = annot_colour
|
168 |
|
169 |
+
rend = create_scene(body_model_smpl, img)
|
170 |
+
os.makedirs(os.path.join(out_dir, 'Renders'), exist_ok=True)
|
171 |
+
rend.save(os.path.join(out_dir, 'Renders', os.path.basename(img_src).split('.')[0] + '.png'))
|
172 |
|
173 |
+
mesh_out_dir = os.path.join(out_dir, 'Preds', os.path.basename(img_src).split('.')[0])
|
174 |
+
os.makedirs(mesh_out_dir, exist_ok=True)
|
175 |
|
176 |
+
print(f'Saving mesh to {mesh_out_dir}')
|
177 |
+
body_model_smpl.export(os.path.join(mesh_out_dir, 'pred.obj'))
|
178 |
|
179 |
+
return rend, os.path.join(mesh_out_dir, 'pred.obj')
|
180 |
|
181 |
with gr.Blocks(title="DECO", css=".gradio-container") as demo:
|
182 |
|
|
|
191 |
|
192 |
gr.HTML("""<br/>""")
|
193 |
|
194 |
+
with gr.Row():
|
195 |
+
send_btn = gr.Button("Infer")
|
196 |
+
send_btn.click(fn=main, inputs=[input_image], outputs=[output_image, output_meshes])
|
|
|
197 |
|
198 |
+
example_images = gr.Examples([
|
199 |
+
['/home/user/app/example_images/213.jpg'],
|
200 |
+
['/home/user/app/example_images/pexels-photo-207569.webp'],
|
201 |
+
['/home/user/app/example_images/pexels-photo-3622517.webp'],
|
202 |
+
['/home/user/app/example_images/pexels-photo-15732209.jpegtest4.jpg'],
|
203 |
+
],
|
204 |
+
inputs=[input_image])
|
|
|
205 |
|
206 |
|
|
|
207 |
demo.launch(debug=True)
|