ac5113 commited on
Commit
1addd25
·
1 Parent(s): 3e09bb6

initial app test

Browse files
Files changed (1) hide show
  1. app.py +41 -50
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
- for img_name in images:
149
- img = cv2.imread(img_name)
150
- img = cv2.resize(img, (256, 256), cv2.INTER_CUBIC)
151
- img = img.transpose(2,0,1)/255.0
152
- img = img[np.newaxis,:,:,:]
153
- img = torch.tensor(img, dtype = torch.float32).to(device)
154
-
155
- cont, _, _ = deco_model(img)
156
- cont = cont.detach().cpu().numpy().squeeze()
157
- cont_smpl = []
158
- for indx, i in enumerate(cont):
159
- if i >= 0.5:
160
- cont_smpl.append(indx)
161
 
162
- img = img.detach().cpu().numpy()
163
- img = np.transpose(img[0], (1, 2, 0))
164
- img = img * 255
165
- img = img.astype(np.uint8)
166
 
167
- contact_smpl = np.zeros((1, 1, 6890))
168
- contact_smpl[0][0][cont_smpl] = 1
169
 
170
- body_model_smpl = trimesh.load(smpl_path, process=False)
171
- for vert in range(body_model_smpl.visual.vertex_colors.shape[0]):
172
- body_model_smpl.visual.vertex_colors[vert] = mesh_colour
173
- body_model_smpl.visual.vertex_colors[cont_smpl] = annot_colour
174
 
175
- rend = create_scene(body_model_smpl, img)
176
- os.makedirs(os.path.join(out_dir, 'Renders'), exist_ok=True)
177
- rend.save(os.path.join(out_dir, 'Renders', os.path.basename(img_name).split('.')[0] + '.png'))
178
 
179
- mesh_out_dir = os.path.join(out_dir, 'Preds', os.path.basename(img_name).split('.')[0])
180
- os.makedirs(mesh_out_dir, exist_ok=True)
181
 
182
- print(f'Saving mesh to {mesh_out_dir}')
183
- body_model_smpl.export(os.path.join(mesh_out_dir, 'pred.obj'))
184
 
185
- return out_dir
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
- # with gr.Row():
201
- # threshold = gr.Slider(0, 1.0, value=0.6, label='Detection Threshold')
202
- # send_btn = gr.Button("Infer")
203
- # send_btn.click(fn=main, inputs=[input_image, threshold], outputs=[output_image, output_meshes])
204
 
205
- # example_images = gr.Examples([
206
- # ['/home/user/app/assets/test1.png'],
207
- # ['/home/user/app/assets/test2.jpg'],
208
- # ['/home/user/app/assets/test3.jpg'],
209
- # ['/home/user/app/assets/test4.jpg'],
210
- # ['/home/user/app/assets/test5.jpg'],
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)