Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -20,7 +20,7 @@ from wilor.models import WiLoR, load_wilor
|
|
20 |
from wilor.utils import recursive_to
|
21 |
from wilor.datasets.vitdet_dataset import ViTDetDataset, DEFAULT_MEAN, DEFAULT_STD
|
22 |
from wilor.utils.renderer import Renderer, cam_crop_to_full
|
23 |
-
device = torch.device('
|
24 |
|
25 |
LIGHT_PURPLE=(0.25098039, 0.274117647, 0.65882353)
|
26 |
|
@@ -59,70 +59,70 @@ def run_wilow_model(image, conf, IoU_threshold=0.5):
|
|
59 |
cv2.rectangle(img_vis, (int(Bbox[0]), int(Bbox[1]) - 20), (int(Bbox[0]) + w, int(Bbox[1])), color, -1)
|
60 |
cv2.putText(img_vis, label, (int(Bbox[0]), int(Bbox[1]) - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0), 2)
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
|
73 |
-
|
74 |
-
|
75 |
|
76 |
-
|
77 |
-
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
|
94 |
-
|
95 |
-
|
96 |
|
97 |
-
|
98 |
-
|
99 |
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
|
104 |
-
|
105 |
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
|
119 |
-
|
120 |
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
|
125 |
-
image =
|
126 |
return image, f'{len(detections)} hands detected'
|
127 |
|
128 |
|
|
|
20 |
from wilor.utils import recursive_to
|
21 |
from wilor.datasets.vitdet_dataset import ViTDetDataset, DEFAULT_MEAN, DEFAULT_STD
|
22 |
from wilor.utils.renderer import Renderer, cam_crop_to_full
|
23 |
+
device = torch.device('cpu') #if torch.cuda.is_available() else torch.device('cpu')
|
24 |
|
25 |
LIGHT_PURPLE=(0.25098039, 0.274117647, 0.65882353)
|
26 |
|
|
|
59 |
cv2.rectangle(img_vis, (int(Bbox[0]), int(Bbox[1]) - 20), (int(Bbox[0]) + w, int(Bbox[1])), color, -1)
|
60 |
cv2.putText(img_vis, label, (int(Bbox[0]), int(Bbox[1]) - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0), 2)
|
61 |
|
62 |
+
if len(bboxes) != 0:
|
63 |
+
boxes = np.stack(bboxes)
|
64 |
+
right = np.stack(is_right)
|
65 |
+
dataset = ViTDetDataset(model_cfg, img_cv2, boxes, right, rescale_factor=2.0 )
|
66 |
+
dataloader = torch.utils.data.DataLoader(dataset, batch_size=32, shuffle=False, num_workers=0)
|
67 |
|
68 |
+
all_verts = []
|
69 |
+
all_cam_t = []
|
70 |
+
all_right = []
|
71 |
+
all_joints= []
|
72 |
|
73 |
+
for batch in dataloader:
|
74 |
+
batch = recursive_to(batch, device)
|
75 |
|
76 |
+
with torch.no_grad():
|
77 |
+
out = model(batch)
|
78 |
|
79 |
+
multiplier = (2*batch['right']-1)
|
80 |
+
pred_cam = out['pred_cam']
|
81 |
+
pred_cam[:,1] = multiplier*pred_cam[:,1]
|
82 |
+
box_center = batch["box_center"].float()
|
83 |
+
box_size = batch["box_size"].float()
|
84 |
+
img_size = batch["img_size"].float()
|
85 |
+
scaled_focal_length = model_cfg.EXTRA.FOCAL_LENGTH / model_cfg.MODEL.IMAGE_SIZE * img_size.max()
|
86 |
+
pred_cam_t_full = cam_crop_to_full(pred_cam, box_center, box_size, img_size, scaled_focal_length).detach().cpu().numpy()
|
87 |
|
88 |
+
# Render the result
|
89 |
+
all_verts = []
|
90 |
+
all_cam_t = []
|
91 |
+
all_right = []
|
92 |
+
all_joints = []
|
93 |
|
94 |
+
batch_size = batch['img'].shape[0]
|
95 |
+
for n in range(batch_size):
|
96 |
|
97 |
+
verts = out['pred_vertices'][n].detach().cpu().numpy()
|
98 |
+
joints = out['pred_keypoints_3d'][n].detach().cpu().numpy()
|
99 |
|
100 |
+
is_right = batch['right'][n].cpu().numpy()
|
101 |
+
verts[:,0] = (2*is_right-1)*verts[:,0]
|
102 |
+
joints[:,0] = (2*is_right-1)*joints[:,0]
|
103 |
|
104 |
+
cam_t = pred_cam_t_full[n]
|
105 |
|
106 |
+
all_verts.append(verts)
|
107 |
+
all_cam_t.append(cam_t)
|
108 |
+
all_right.append(is_right)
|
109 |
+
all_joints.append(joints)
|
110 |
+
# Render front view
|
111 |
|
112 |
+
misc_args = dict(
|
113 |
+
mesh_base_color=LIGHT_PURPLE,
|
114 |
+
scene_bg_color=(1, 1, 1),
|
115 |
+
focal_length=scaled_focal_length,
|
116 |
+
)
|
117 |
+
cam_view = renderer.render_rgba_multiple(all_verts, cam_t=all_cam_t, render_res=img_size[n], is_right=all_right, **misc_args)
|
118 |
|
119 |
+
# Overlay image
|
120 |
|
121 |
+
input_img = img_vis.astype(np.float32)/255.0
|
122 |
+
input_img = np.concatenate([input_img, np.ones_like(input_img[:,:,:1])], axis=2) # Add alpha channel
|
123 |
+
input_img_overlay = input_img[:,:,:3] * (1-cam_view[:,:,3:]) + cam_view[:,:,:3] * cam_view[:,:,3:]
|
124 |
|
125 |
+
image = input_img_overlay
|
126 |
return image, f'{len(detections)} hands detected'
|
127 |
|
128 |
|