示例#1
0
    parser.add_argument("--balls", type=int, default=3)
    args = parser.parse_args()

    handleTensorflowSession(memoryLimit=0.2)

    gridModel = GridModel("../grid_models/grid_model_submovavg_64x64.h5",
                          args.balls)
    cap = cv2.VideoCapture(0)
    framerateChecker = FramerateChecker(expected_fps=30)

    while True:
        framerateChecker.check()
        ret, original_img = cap.read()
        if not ret:
            print("Couldn't get frame from camera.")
            break
        else:
            height, width, channels = original_img.shape
            tocrop = int((width - height) / 2)
            original_img = original_img[:, tocrop:-tocrop]
            ballsAndHands = gridModel.predict(original_img.copy())

            img = cv2.resize(original_img, (256, 256), cv2.INTER_CUBIC)
            drawBallsAndHands(img, ballsAndHands)

            cv2.imshow('Webcam', img)
            cv2.waitKey(1)

    cap.release()
    cv2.destroyAllWindows()
    model = GridModel(
        "../grid_models/grid_model_submovavg_64x64_light.h5",
        nBalls=args.n_balls,
        preprocessType="SUBMOVAVG",
        flip=False,
        postprocess=True,
    )

    cap = cv2.VideoCapture(args.path)
    ball_ys = np.empty((0, args.n_balls))
    with tqdm(total=int(cap.get(cv2.CAP_PROP_FRAME_COUNT))) as pbar:
        while cap.isOpened():
            ret, frame = cap.read()
            if not ret:
                break
            balls_and_hands = model.predict(frame)
            balls = balls_and_hands["balls"]
            ball_ys = np.vstack((ball_ys, balls[:, 1]))
            pbar.update()
    cap.release()

    fig, (ax1, ax2, ax3) = plt.subplots(3)
    xs = np.arange(ball_ys.shape[0])
    ball_ys_blurred = gaussian_filter(ball_ys, sigma=(2, 0))

    ax1.set_title("Original ball positions")
    ax1.set_xlabel("Frame")
    ax1.set_ylabel("y position")
    for i in range(args.n_balls):
        ax1.plot(xs, ball_ys[:, i], "-o", label=f"Ball {i}")
    ax1.legend()