ac5113 commited on
Commit
539ba7c
1 Parent(s): 8df2830

code cleanup

Browse files
Files changed (1) hide show
  1. app.py +1 -109
app.py CHANGED
@@ -13,14 +13,11 @@ subprocess.run(
13
  import gradio as gr
14
 
15
  import trimesh
16
- import pyrender
17
  import plotly.graph_objects as go
18
 
19
  from models.deco import DECO
20
  from common import constants
21
 
22
- # os.environ['PYOPENGL_PLATFORM'] = 'osmesa'
23
-
24
  if torch.cuda.is_available():
25
  device = torch.device('cuda')
26
  else:
@@ -83,107 +80,7 @@ def initiate_model(model_path):
83
 
84
  deco_model.eval()
85
 
86
- return deco_model
87
-
88
- # def render_image(scene, img_res, img=None, viewer=False):
89
- # '''
90
- # Render the given pyrender scene and return the image. Can also overlay the mesh on an image.
91
- # '''
92
- # if viewer:
93
- # pyrender.Viewer(scene, use_raymond_lighting=True)
94
- # return 0
95
- # else:
96
- # r = pyrender.OffscreenRenderer(viewport_width=img_res,
97
- # viewport_height=img_res,
98
- # point_size=1.0)
99
- # color, _ = r.render(scene, flags=pyrender.RenderFlags.RGBA)
100
- # color = color.astype(np.float32) / 255.0
101
-
102
- # if img is not None:
103
- # valid_mask = (color[:, :, -1] > 0)[:, :, np.newaxis]
104
- # input_img = img.detach().cpu().numpy()
105
- # output_img = (color[:, :, :-1] * valid_mask +
106
- # (1 - valid_mask) * input_img)
107
- # else:
108
- # output_img = color
109
- # return output_img
110
-
111
- # def create_scene(mesh, img, focal_length=500, camera_center=250, img_res=500):
112
- # # Setup the scene
113
- # scene = pyrender.Scene(bg_color=[1.0, 1.0, 1.0, 1.0],
114
- # ambient_light=(0.3, 0.3, 0.3))
115
- # # add mesh for camera
116
- # camera_pose = np.eye(4)
117
- # camera_rotation = np.eye(3, 3)
118
- # camera_translation = np.array([0., 0, 2.5])
119
- # camera_pose[:3, :3] = camera_rotation
120
- # camera_pose[:3, 3] = camera_rotation @ camera_translation
121
- # pyrencamera = pyrender.camera.IntrinsicsCamera(
122
- # fx=focal_length, fy=focal_length,
123
- # cx=camera_center, cy=camera_center)
124
- # scene.add(pyrencamera, pose=camera_pose)
125
- # # create and add light
126
- # light = pyrender.PointLight(color=[1.0, 1.0, 1.0], intensity=1)
127
- # light_pose = np.eye(4)
128
- # for lp in [[1, 1, 1], [-1, 1, 1], [1, -1, 1], [-1, -1, 1]]:
129
- # light_pose[:3, 3] = mesh.vertices.mean(0) + np.array(lp)
130
- # # out_mesh.vertices.mean(0) + np.array(lp)
131
- # scene.add(light, pose=light_pose)
132
- # # add body mesh
133
- # material = pyrender.MetallicRoughnessMaterial(
134
- # metallicFactor=0.0,
135
- # alphaMode='OPAQUE',
136
- # baseColorFactor=(1.0, 1.0, 0.9, 1.0))
137
- # mesh_images = []
138
-
139
- # # resize input image to fit the mesh image height
140
- # img_height = img_res
141
- # img_width = int(img_height * img.shape[1] / img.shape[0])
142
- # img = cv2.resize(img, (img_width, img_height))
143
- # mesh_images.append(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
144
-
145
- # for sideview_angle in [0, 90, 180, 270]:
146
- # out_mesh = mesh.copy()
147
- # rot = trimesh.transformations.rotation_matrix(
148
- # np.radians(sideview_angle), [0, 1, 0])
149
- # out_mesh.apply_transform(rot)
150
- # out_mesh = pyrender.Mesh.from_trimesh(
151
- # out_mesh,
152
- # material=material)
153
- # mesh_pose = np.eye(4)
154
- # scene.add(out_mesh, pose=mesh_pose, name='mesh')
155
- # output_img = render_image(scene, img_res)
156
- # output_img = pil_img.fromarray((output_img * 255).astype(np.uint8))
157
- # output_img = np.asarray(output_img)[:, :, :3]
158
- # mesh_images.append(output_img)
159
- # # delete the previous mesh
160
- # prev_mesh = scene.get_nodes(name='mesh').pop()
161
- # scene.remove_node(prev_mesh)
162
-
163
- # # show upside down view
164
- # for topview_angle in [90, 270]:
165
- # out_mesh = mesh.copy()
166
- # rot = trimesh.transformations.rotation_matrix(
167
- # np.radians(topview_angle), [1, 0, 0])
168
- # out_mesh.apply_transform(rot)
169
- # out_mesh = pyrender.Mesh.from_trimesh(
170
- # out_mesh,
171
- # material=material)
172
- # mesh_pose = np.eye(4)
173
- # scene.add(out_mesh, pose=mesh_pose, name='mesh')
174
- # output_img = render_image(scene, img_res)
175
- # output_img = pil_img.fromarray((output_img * 255).astype(np.uint8))
176
- # output_img = np.asarray(output_img)[:, :, :3]
177
- # mesh_images.append(output_img)
178
- # # delete the previous mesh
179
- # prev_mesh = scene.get_nodes(name='mesh').pop()
180
- # scene.remove_node(prev_mesh)
181
-
182
- # # stack images
183
- # IMG = np.hstack(mesh_images)
184
- # IMG = pil_img.fromarray(IMG)
185
- # IMG.thumbnail((3000, 3000))
186
- # return IMG
187
 
188
  def create_layout(dummy, camera=None):
189
  if camera is None:
@@ -345,11 +242,6 @@ def main(pil_img, out_dir='demo_out', model_path='checkpoint/deco_best.pth', mes
345
  for vert in range(body_model_smpl.visual.vertex_colors.shape[0]):
346
  body_model_smpl.visual.vertex_colors[vert] = mesh_colour
347
  body_model_smpl.visual.vertex_colors[cont_smpl] = annot_colour
348
-
349
- # rend = create_scene(body_model_smpl, img)
350
- # os.makedirs(os.path.join(out_dir, 'Renders'), exist_ok=True)
351
- # rend.save(os.path.join(out_dir, 'Renders', 'pred.png'))
352
- rend = img
353
 
354
  mesh_out_dir = os.path.join(out_dir, 'Preds')
355
  os.makedirs(mesh_out_dir, exist_ok=True)
 
13
  import gradio as gr
14
 
15
  import trimesh
 
16
  import plotly.graph_objects as go
17
 
18
  from models.deco import DECO
19
  from common import constants
20
 
 
 
21
  if torch.cuda.is_available():
22
  device = torch.device('cuda')
23
  else:
 
80
 
81
  deco_model.eval()
82
 
83
+ return deco_model
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84
 
85
  def create_layout(dummy, camera=None):
86
  if camera is None:
 
242
  for vert in range(body_model_smpl.visual.vertex_colors.shape[0]):
243
  body_model_smpl.visual.vertex_colors[vert] = mesh_colour
244
  body_model_smpl.visual.vertex_colors[cont_smpl] = annot_colour
 
 
 
 
 
245
 
246
  mesh_out_dir = os.path.join(out_dir, 'Preds')
247
  os.makedirs(mesh_out_dir, exist_ok=True)