Spaces:
Build error
Build error
from __future__ import annotations | |
import os | |
import numpy as np | |
import torch | |
import torch.nn as nn | |
from mmdet.apis import inference_detector, init_detector | |
from mmpose.apis import inference_top_down_pose_model, init_pose_model, process_mmdet_results | |
#os.environ["PYOPENGL_PLATFORM"] = "egl" | |
# project root directory | |
ROOT_DIR = "./" | |
VIT_DIR = os.path.join(ROOT_DIR, "vendor/ViTPose") | |
class ViTPoseModel(object): | |
MODEL_DICT = { | |
'ViTPose+-G (multi-task train, COCO)': { | |
'config': f'{VIT_DIR}/configs/wholebody/2d_kpt_sview_rgb_img/topdown_heatmap/coco-wholebody/ViTPose_huge_wholebody_256x192.py', | |
'model': f'{ROOT_DIR}/_DATA/vitpose_ckpts/vitpose+_huge/wholebody.pth', | |
}, | |
} | |
def __init__(self, device: str | torch.device): | |
self.device = torch.device(device) | |
self.model_name = 'ViTPose+-G (multi-task train, COCO)' | |
self.model = self._load_model(self.model_name) | |
def _load_all_models_once(self) -> None: | |
for name in self.MODEL_DICT: | |
self._load_model(name) | |
def _load_model(self, name: str) -> nn.Module: | |
dic = self.MODEL_DICT[name] | |
ckpt_path = dic['model'] | |
model = init_pose_model(dic['config'], ckpt_path, device=self.device) | |
return model | |
def set_model(self, name: str) -> None: | |
if name == self.model_name: | |
return | |
self.model_name = name | |
self.model = self._load_model(name) | |
def predict_pose( | |
self, | |
image: np.ndarray, | |
det_results: list[np.ndarray], | |
box_score_threshold: float = 0.5) -> list[dict[str, np.ndarray]]: | |
image = image[:, :, ::-1] # RGB -> BGR | |
person_results = process_mmdet_results(det_results, 1) | |
out, _ = inference_top_down_pose_model(self.model, | |
image, | |
person_results=person_results, | |
bbox_thr=box_score_threshold, | |
format='xyxy') | |
return out |