Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import argparse
|
|
| 2 |
import os
|
| 3 |
from pathlib import Path
|
| 4 |
import tempfile
|
|
|
|
| 5 |
import sys
|
| 6 |
import cv2
|
| 7 |
import gradio as gr
|
|
@@ -23,12 +24,22 @@ from hamer.models import HAMER
|
|
| 23 |
from hamer.utils import recursive_to
|
| 24 |
from hamer.utils.renderer import Renderer, cam_crop_to_full
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
#try:
|
| 34 |
# from vitpose_model import ViTPoseModel
|
|
@@ -57,35 +68,48 @@ model.eval()
|
|
| 57 |
|
| 58 |
|
| 59 |
# Load detector
|
| 60 |
-
from detectron2.config import LazyConfig
|
| 61 |
|
| 62 |
-
from hamer.utils.utils_detectron2 import DefaultPredictor_Lazy
|
| 63 |
|
| 64 |
-
detectron2_cfg = LazyConfig.load(f"vendor/detectron2/projects/ViTDet/configs/COCO/cascade_mask_rcnn_vitdet_h_75ep.py")
|
| 65 |
-
detectron2_cfg.train.init_checkpoint = "https://dl.fbaipublicfiles.com/detectron2/ViTDet/COCO/cascade_mask_rcnn_vitdet_h/f328730692/model_final_f05665.pkl"
|
| 66 |
-
for i in range(3):
|
| 67 |
-
detectron2_cfg.model.roi_heads.box_predictors[i].test_score_thresh = 0.25
|
| 68 |
-
detector = DefaultPredictor_Lazy(detectron2_cfg)
|
| 69 |
|
| 70 |
# Setup the renderer
|
| 71 |
renderer = Renderer(model_cfg, faces=model.mano.faces)
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
# keypoint detector
|
| 74 |
cpm = ViTPoseModel(device)
|
| 75 |
|
| 76 |
import numpy as np
|
| 77 |
|
| 78 |
-
def infer(in_pil_img, in_threshold=0.
|
|
|
|
|
|
|
| 79 |
|
| 80 |
open_cv_image = np.array(in_pil_img)
|
|
|
|
|
|
|
| 81 |
# Convert RGB to BGR
|
| 82 |
open_cv_image = open_cv_image[:, :, ::-1].copy()
|
| 83 |
print("EEEEE", open_cv_image.shape)
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
|
| 91 |
# Detect human keypoints for each person
|
|
@@ -121,6 +145,9 @@ def infer(in_pil_img, in_threshold=0.8, out_pil_img=None):
|
|
| 121 |
|
| 122 |
boxes = np.stack(bboxes)
|
| 123 |
right = np.stack(is_right)
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
|
| 126 |
# Run HaMeR on all detected humans
|
|
@@ -136,11 +163,14 @@ def infer(in_pil_img, in_threshold=0.8, out_pil_img=None):
|
|
| 136 |
|
| 137 |
for batch in dataloader:
|
| 138 |
batch = recursive_to(batch, device)
|
|
|
|
| 139 |
with torch.no_grad():
|
| 140 |
out = model(batch)
|
| 141 |
|
| 142 |
multiplier = (2*batch['right']-1)
|
| 143 |
pred_cam = out['pred_cam']
|
|
|
|
|
|
|
| 144 |
pred_cam[:,1] = multiplier*pred_cam[:,1]
|
| 145 |
box_center = batch["box_center"].float()
|
| 146 |
box_size = batch["box_size"].float()
|
|
@@ -204,9 +234,15 @@ def infer(in_pil_img, in_threshold=0.8, out_pil_img=None):
|
|
| 204 |
|
| 205 |
with gr.Blocks(title="HaMeR", css=".gradio-container") as demo:
|
| 206 |
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 210 |
You can also download the .obj files for each hand reconstruction.</div>""")
|
| 211 |
|
| 212 |
with gr.Row():
|
|
@@ -228,10 +264,9 @@ with gr.Blocks(title="HaMeR", css=".gradio-container") as demo:
|
|
| 228 |
['/home/user/app/assets/test1.jpg'],
|
| 229 |
['/home/user/app/assets/test2.jpg'],
|
| 230 |
['/home/user/app/assets/test3.jpg'],
|
| 231 |
-
['/home/user/app/assets/test4.jpg'],
|
| 232 |
['/home/user/app/assets/test5.jpg'],
|
| 233 |
],
|
| 234 |
-
inputs=
|
| 235 |
|
| 236 |
|
| 237 |
#demo.queue()
|
|
|
|
| 2 |
import os
|
| 3 |
from pathlib import Path
|
| 4 |
import tempfile
|
| 5 |
+
import tarfile
|
| 6 |
import sys
|
| 7 |
import cv2
|
| 8 |
import gradio as gr
|
|
|
|
| 24 |
from hamer.utils import recursive_to
|
| 25 |
from hamer.utils.renderer import Renderer, cam_crop_to_full
|
| 26 |
|
| 27 |
+
def extract_tar() -> None:
|
| 28 |
+
if Path('mmdet_configs/configs').exists():
|
| 29 |
+
return
|
| 30 |
+
with tarfile.open('mmdet_configs/configs.tar') as f:
|
| 31 |
+
f.extractall('mmdet_configs')
|
| 32 |
+
|
| 33 |
+
extract_tar()
|
| 34 |
+
|
| 35 |
+
#from vitpose_model import DetModel
|
| 36 |
+
|
| 37 |
+
#try:
|
| 38 |
+
# import detectron2
|
| 39 |
+
#except:
|
| 40 |
+
# import os
|
| 41 |
+
# os.system('pip install --upgrade pip')
|
| 42 |
+
# os.system('pip install git+https://github.com/facebookresearch/detectron2.git')
|
| 43 |
|
| 44 |
#try:
|
| 45 |
# from vitpose_model import ViTPoseModel
|
|
|
|
| 68 |
|
| 69 |
|
| 70 |
# Load detector
|
| 71 |
+
#from detectron2.config import LazyConfig
|
| 72 |
|
| 73 |
+
#from hamer.utils.utils_detectron2 import DefaultPredictor_Lazy
|
| 74 |
|
| 75 |
+
#detectron2_cfg = LazyConfig.load(f"vendor/detectron2/projects/ViTDet/configs/COCO/cascade_mask_rcnn_vitdet_h_75ep.py")
|
| 76 |
+
#detectron2_cfg.train.init_checkpoint = "https://dl.fbaipublicfiles.com/detectron2/ViTDet/COCO/cascade_mask_rcnn_vitdet_h/f328730692/model_final_f05665.pkl"
|
| 77 |
+
#for i in range(3):
|
| 78 |
+
# detectron2_cfg.model.roi_heads.box_predictors[i].test_score_thresh = 0.25
|
| 79 |
+
#detector = DefaultPredictor_Lazy(detectron2_cfg)
|
| 80 |
|
| 81 |
# Setup the renderer
|
| 82 |
renderer = Renderer(model_cfg, faces=model.mano.faces)
|
| 83 |
|
| 84 |
+
# mmdet detector
|
| 85 |
+
#det_model = DetModel()
|
| 86 |
+
det_model = torch.hub.load('ultralytics/yolov5', 'yolov5x6')
|
| 87 |
+
|
| 88 |
# keypoint detector
|
| 89 |
cpm = ViTPoseModel(device)
|
| 90 |
|
| 91 |
import numpy as np
|
| 92 |
|
| 93 |
+
def infer(in_pil_img, in_threshold=0.4, out_pil_img=None):
|
| 94 |
+
|
| 95 |
+
print(in_threshold)
|
| 96 |
|
| 97 |
open_cv_image = np.array(in_pil_img)
|
| 98 |
+
det_out = det_model(open_cv_image)
|
| 99 |
+
det_out = det_out.xyxy[0]
|
| 100 |
# Convert RGB to BGR
|
| 101 |
open_cv_image = open_cv_image[:, :, ::-1].copy()
|
| 102 |
print("EEEEE", open_cv_image.shape)
|
| 103 |
+
print(det_out)
|
| 104 |
+
#det_out = detector(open_cv_image)
|
| 105 |
+
scores = det_out[:,4]
|
| 106 |
+
det_instances = det_out[:,5]
|
| 107 |
+
print(scores)
|
| 108 |
+
print(det_instances)
|
| 109 |
+
valid_idx = (det_instances==0) & (scores > in_threshold)
|
| 110 |
+
print(valid_idx)
|
| 111 |
+
pred_bboxes=det_out[valid_idx,:4].cpu().numpy()
|
| 112 |
+
pred_scores=scores[valid_idx].cpu().numpy()
|
| 113 |
|
| 114 |
|
| 115 |
# Detect human keypoints for each person
|
|
|
|
| 145 |
|
| 146 |
boxes = np.stack(bboxes)
|
| 147 |
right = np.stack(is_right)
|
| 148 |
+
print(boxes)
|
| 149 |
+
print(right)
|
| 150 |
+
print(open_cv_image)
|
| 151 |
|
| 152 |
|
| 153 |
# Run HaMeR on all detected humans
|
|
|
|
| 163 |
|
| 164 |
for batch in dataloader:
|
| 165 |
batch = recursive_to(batch, device)
|
| 166 |
+
print(batch['img'])
|
| 167 |
with torch.no_grad():
|
| 168 |
out = model(batch)
|
| 169 |
|
| 170 |
multiplier = (2*batch['right']-1)
|
| 171 |
pred_cam = out['pred_cam']
|
| 172 |
+
print(out['pred_vertices'])
|
| 173 |
+
print(pred_cam)
|
| 174 |
pred_cam[:,1] = multiplier*pred_cam[:,1]
|
| 175 |
box_center = batch["box_center"].float()
|
| 176 |
box_size = batch["box_size"].float()
|
|
|
|
| 234 |
|
| 235 |
with gr.Blocks(title="HaMeR", css=".gradio-container") as demo:
|
| 236 |
|
| 237 |
+
#title="HaMeR"
|
| 238 |
+
#description="Gradio Demo for HaMeR."
|
| 239 |
+
|
| 240 |
+
#gr.HTML("""<h1>HaMeR</h1>""")
|
| 241 |
+
#gr.HTML("""<h3>Gradio Demo for HaMeR. You can select an </h3>""")
|
| 242 |
+
|
| 243 |
+
gr.HTML("""<div style="font-weight:bold; text-align:center; font-size: 30px;">HaMeR</div>""")
|
| 244 |
+
gr.HTML("""<div style="text-align:left; font-size: 20px;">Demo for HaMeR. You can drop an image at the top-left panel
|
| 245 |
+
(or select one of the examples) and you will get the 3D reconstructions of the detected hands on the right.
|
| 246 |
You can also download the .obj files for each hand reconstruction.</div>""")
|
| 247 |
|
| 248 |
with gr.Row():
|
|
|
|
| 264 |
['/home/user/app/assets/test1.jpg'],
|
| 265 |
['/home/user/app/assets/test2.jpg'],
|
| 266 |
['/home/user/app/assets/test3.jpg'],
|
|
|
|
| 267 |
['/home/user/app/assets/test5.jpg'],
|
| 268 |
],
|
| 269 |
+
inputs=input_image)
|
| 270 |
|
| 271 |
|
| 272 |
#demo.queue()
|