This repository has been archived by the owner on Jul 18, 2021. It is now read-only.
forked from rasmusakerlund/juggling-vision-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plotballs.py
74 lines (63 loc) · 2.12 KB
/
plotballs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import argparse
import cv2
import matplotlib.pyplot as plt
import numpy as np
from scipy.ndimage.filters import gaussian_filter
from scipy.signal import find_peaks
from tqdm import tqdm
from gridmodel import GridModel
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("path")
parser.add_argument("--n_balls", type=int, nargs="?", default=3)
args = parser.parse_args()
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()
ax2.set_title("Blurred ball positions")
ax2.set_xlabel("Frame")
ax2.set_ylabel("y position")
for i in range(args.n_balls):
ax2.plot(xs, ball_ys_blurred[:, i], "-o", label=f"Ball {i}")
ax2.legend()
ax3.set_title("Ball troughs")
ax3.set_xlabel("Frame")
ax3.set_ylabel("y position")
for i in range(args.n_balls):
ax3.plot(xs, ball_ys_blurred[:, i], "--", label=f"Ball {i}")
for i in range(args.n_balls):
troughs, _ = find_peaks(-ball_ys_blurred[:, i])
ax3.plot(
xs[troughs],
ball_ys_blurred[troughs, i],
"o",
label=f"Ball {i} troughs",
color=f"C{i}",
)
ax3.legend()
plt.show()