def main(): args = parse_args() reset_config(config, args) image_path = '/home/xyliu/Pictures/pose/soccer.png' ########## 加载human detecotor model # from lib.detector.yolo.human_detector2 import load_model as yolo_model # human_model = yolo_model() from lib.detector.mmdetection.high_api import load_model as mm_model human_model = mm_model() # from lib.detector.yolo.human_detector2 import human_bbox_get as yolo_det from lib.detector.mmdetection.high_api import human_boxes_get as mm_det # load MODEL pose_model = model_load(config) pose_model.eval() pose_model.cuda() from pycocotools.coco import COCO annFile = '/ssd/xyliu/data/coco/annotations/instances_val2017.json' im_root = '/ssd/xyliu/data/coco/images/val2017/' coco = COCO(annFile) catIds = coco.getCatIds(catNms=['person']) # 所有人体图片的id imgIds = coco.getImgIds(catIds=catIds) kpts_result = [] detected_image_num = 0 box_num = 0 for imgId in tqdm(imgIds): img = coco.loadImgs(imgId)[0] im_name = img['file_name'] img = im_root + im_name img_input = cv2.imread( img, cv2.IMREAD_COLOR | cv2.IMREAD_IGNORE_ORIENTATION) try: # bboxs, scores = yolo_det(img_input, human_model) bboxs, scores = mm_det(human_model, img_input, 0.1) inputs, origin_img, center, scale = PreProcess( img_input, bboxs, scores, config) except Exception as e: print(e) continue detected_image_num += 1 with torch.no_grad(): output = pose_model(inputs.cuda()) preds, maxvals = get_final_preds(config, output.clone().cpu().numpy(), np.asarray(center), np.asarray(scale)) # vis = np.ones(shape=maxvals.shape,) vis = maxvals preds = preds.astype(np.float16) keypoints = np.concatenate((preds, vis), -1) for k, s in zip(keypoints, scores.tolist()): box_num += 1 k = k.flatten().tolist() item = { "image_id": imgId, "category_id": 1, "keypoints": k, "score": s } kpts_result.append(item) num_joints = 17 in_vis_thre = 0.2 oks_thre = 0.5 oks_nmsed_kpts = [] for i in range(len(kpts_result)): img_kpts = kpts_result[i]['keypoints'] kpt = np.array(img_kpts).reshape(17, 3) box_score = kpts_result[i]['score'] kpt_score = 0 valid_num = 0 # each joint for bbox for n_jt in range(0, num_joints): # score t_s = kpt[n_jt][2] if t_s > in_vis_thre: kpt_score = kpt_score + t_s valid_num = valid_num + 1 if valid_num != 0: kpt_score = kpt_score / valid_num # rescoring 关节点的置信度 与 box的置信度的乘积 kpts_result[i]['score'] = kpt_score * box_score import json data = json.dumps(kpts_result) print( 'image num is {} \tdetected_image num is {}\t person num is {}'.format( len(imgIds), detected_image_num, box_num)), # data = json.dumps(str(kpts_result)) with open('person_keypoints.json', 'wt') as f: # pass f.write(data)
def ckpt_time(t0=None, display=None): if not t0: return time.time() else: t1 = time.time() if display: print('consume {:2f} second'.format(t1 - t0)) return t1 - t0, t1 ###### 加载human detecotor model from lib.detector.yolo.human_detector import load_model as yolo_model # human_model = yolo_model() from lib.detector.mmdetection.high_api import load_model as mm_model human_model = mm_model() def main(): args = parse_args() update_config(cfg, args) if not args.camera: # handle video cam = cv2.VideoCapture(args.video_input) video_length = int(cam.get(cv2.CAP_PROP_FRAME_COUNT)) else: cam = cv2.VideoCapture(0) video_length = 30000 ret_val, input_image = cam.read()