def keypoint_detection(img, detector, pose_net, ctx=mx.cpu()): x, scaled_img = gcv.data.transforms.presets.yolo.transform_test( img, short=480, max_size=1024) x = x.as_in_context(ctx) class_IDs, scores, bounding_boxs = detector(x) pose_input, upscale_bbox = detector_to_simple_pose(scaled_img, class_IDs, scores, bounding_boxs, output_shape=(128, 96), ctx=ctx) if len(upscale_bbox) > 0: predicted_heatmap = pose_net(pose_input) pred_coords, confidence = heatmap_to_coord(predicted_heatmap, upscale_bbox) scale = 1.0 * img.shape[0] / scaled_img.shape[0] img = cv_plot_keypoints(img.asnumpy(), pred_coords, confidence, class_IDs, bounding_boxs, scores, box_thresh=1, keypoint_thresh=0.3, scale=scale) return img
def process_pose_frame(np_frame, resolution): width, height = resolution if np_frame is None: return mxnet.nd.zeros((height, width, 3), ctx=gpu) frame = mxnet.nd.array(np_frame, ctx=gpu) x, img = data.transforms.presets.yolo.transform_test(frame, short=512) class_IDs, scores, bounding_boxs = detector(x) pose_input, upscale_bbox = detector_to_simple_pose( img, class_IDs, scores, bounding_boxs) if pose_input is None: return predicted_heatmap = pose_net(pose_input) pred_coords, confidence = heatmap_to_coord(predicted_heatmap, upscale_bbox) img = cv_plot_keypoints(img, pred_coords, confidence, class_IDs, bounding_boxs, scores, box_thresh=0.5, keypoint_thresh=0.2, scale=1.0, **kwargs) print(img.size) # for j in range(len(pred_coords)): # for i in range(len(pred_coords[0])): # x, y = pred_coords[j][i].astype(int).asnumpy() # cv2.circle(img, (x,y), 2, (0, 255, 0), thickness=-1, lineType=cv2.FILLED) return img
def keypoint_detection(self, frame): img = mx.nd.array(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)).astype('uint8') x, scaled_img = gcv.data.transforms.presets.yolo.transform_test( img, short=480, max_size=1024) x = x.as_in_context(self.ctx) class_IDs, scores, bounding_boxs = self.person_detector(x) pred_coords = np.zeros(1) pose_input, upscale_bbox = detector_to_simple_pose(scaled_img, class_IDs, scores, bounding_boxs, output_shape=(128, 96), ctx=self.ctx) if len(upscale_bbox) > 0: predicted_heatmap = self.pose_estimator(pose_input) pred_coords, confidence = heatmap_to_coord(predicted_heatmap, upscale_bbox) scale = 1.0 * img.shape[0] / scaled_img.shape[0] img = cv_plot_keypoints(frame, pred_coords, confidence, class_IDs, bounding_boxs, scores, box_thresh=1, keypoint_thresh=0.3, scale=scale) pred_coords *= scale if isinstance(img, mx.nd.NDArray): img = frame if isinstance(pred_coords, mx.nd.NDArray): pred_coords = pred_coords.asnumpy() return pred_coords, img
num_frames = 100 for i in range(num_frames): ret, frame = cap.read() frame = mx.nd.array(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)).astype('uint8') x, frame = gcv.data.transforms.presets.ssd.transform_test(frame, short=512, max_size=350) x = x.as_in_context(ctx) class_IDs, scores, bounding_boxs = detector(x) pose_input, upscale_bbox = detector_to_simple_pose(frame, class_IDs, scores, bounding_boxs, output_shape=(128, 96), ctx=ctx) if len(upscale_bbox) > 0: predicted_heatmap = estimator(pose_input) pred_coords, confidence = heatmap_to_coord(predicted_heatmap, upscale_bbox) img = cv_plot_keypoints(frame, pred_coords, confidence, class_IDs, bounding_boxs, scores, box_thresh=0.5, keypoint_thresh=0.2) cv_plot_image(img) cv2.waitKey(1) '''We release the webcam before exiting: .. code-block:: python''' cap.release() '''Results ------- Download the script to run the demo
x = x.as_in_context(ctx) class_IDs, scores, bounding_boxs = detector(x) pose_input, upscale_bbox = detector_to_simple_pose(img, class_IDs, scores, bounding_boxs, output_shape=(128, 96), ctx=ctx) # 检测到人时 if len(upscale_bbox) > 0: predicted_heatmap = estimator(pose_input) pred_coords, confidence = heatmap_to_coord(predicted_heatmap, upscale_bbox) img = cv_plot_keypoints(img, pred_coords, confidence, class_IDs, bounding_boxs, scores) # 动作识别 X = predicted_heatmap.asnumpy().flatten().reshape( (len(upscale_bbox), -1)) X = pca.transform(X) action = le.inverse_transform(cf.predict(X)) cv_plot_image(img, upperleft_txt=f"FPS:{(1.0 / (time.time() - fps_time)):.2f}", upperleft_txt_corner=(10, 25), left_txt_list=action) fps_time = time.time() # ESC键退出 if cv.waitKey(1) == 27: