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 if __name__ == '__main__': ctx = mx.cpu() detector_name = "ssd_512_mobilenet1.0_coco" detector = get_model(detector_name, pretrained=True, ctx=ctx) detector.reset_class(classes=['person'], reuse_weights={'person':'person'}) net = get_model('simple_pose_resnet18_v1b', pretrained='ccd24037', ctx=ctx) cap = cv2.VideoCapture(0) time.sleep(1) ### letting the camera autofocus for i in range(opt.num_frames): ret, frame = cap.read() frame = mx.nd.array(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)).astype('uint8') img = keypoint_detection(frame, detector, net, ctx=ctx) cv_plot_image(img) cv2.waitKey(1) cap.release() cv2.destroyAllWindows()
pred_coords, confidence = heatmap_to_coord(predicted_heatmap, upscale_bbox) img = cv_plot_keypoints(img, pred_coords, confidence, class_IDs, bounding_boxs, scores) # 动作对比 angles = AngeleCal.cal(pred_coords, confidence) results = angeleCal.compare(angles).astype('U5') # 缩放示例视频并合并显示 width = int(img.shape[1]) height = int(width * frame2.shape[0] / frame2.shape[1]) frame2 = cv.resize(frame2, (width, height), cv.INTER_AREA) img = np.vstack((img, frame2)) cv_plot_image(img, upperleft_txt=FPS.fps(), upperleft_txt_corner=(10, 25), left_txt_list=results) # ESC键退出 if cv.waitKey(1) == 27: break ret1, frame1 = cap1.read() ret2, frame2 = cap2.read() cv.destroyAllWindows() cap1.release() cap2.release()
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: break ret, frame = cap.read() cv.destroyAllWindows() cap.release()
detect_time_sum += (time.time() - starttime) if pose_input is not None: starttime = time.time() predicted_heatmap = pose_net(pose_input) pred_coords, confidence = heatmap_to_coord(predicted_heatmap, upscale_bbox) jsonOutput.write(confidence, pred_coords, scores, frame_count + 1) img = cv_plot_keypoints(frame, pred_coords, confidence, class_IDs, bounding_boxs, scores, box_thresh=0.5, keypoint_thresh=0.2) pose_time_sum += (time.time() - starttime) active_pose_frame_count += 1 if showimage: cv_plot_image(img) vidout.write(cv2.cvtColor(img, cv2.COLOR_RGB2BGR)) else: if showimage: cv_plot_image(frame) vidout.write(cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)) else: if showimage: cv2.imshow('Frame', frame) vidout.write(frame)