-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpredict.py
More file actions
252 lines (216 loc) · 6.41 KB
/
predict.py
File metadata and controls
252 lines (216 loc) · 6.41 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import argparse
from pathlib import Path
import cv2
import keras
import numpy as np
def label_to_pixel(
x: float,
y: float,
width: int,
height: int,
*,
invert_y: bool,
) -> tuple[float, float]:
if invert_y:
y = -y
cx, cy = width / 2.0, height / 2.0
px = cx + x * (width / 2.0)
py = cy + y * (height / 2.0)
return px, py
def prepare_frame(
frame_bgr: np.ndarray,
input_size: int,
) -> np.ndarray:
resized = cv2.resize(
frame_bgr, (input_size, input_size), interpolation=cv2.INTER_AREA
)
resized = cv2.cvtColor(resized, cv2.COLOR_BGR2RGB)
arr = resized.astype(np.float32) / 255.0
arr = (arr - 0.5) * 2.0
return arr
def run_prediction(model: keras.Model, input_frame: np.ndarray) -> tuple[float, float]:
batch = np.expand_dims(input_frame, axis=0)
pred = model.predict(batch, verbose=0)
if isinstance(pred, (list, tuple)):
pred = pred[0]
pred = np.asarray(pred).squeeze()
if pred.size < 2:
raise RuntimeError(f"Model output has unexpected shape: {pred.shape}")
return float(pred[0]), float(pred[1])
def draw_prediction(
frame: np.ndarray,
pred_xy: tuple[float, float] | None,
*,
invert_y: bool,
label: str | None,
) -> None:
h, w = frame.shape[:2]
center = (int(round(w / 2.0)), int(round(h / 2.0)))
cv2.drawMarker(
frame,
center,
(255, 255, 0),
markerType=cv2.MARKER_CROSS,
markerSize=max(12, w // 15),
thickness=1,
)
if pred_xy is None:
if label:
cv2.putText(
frame,
label,
(10, 28),
cv2.FONT_HERSHEY_SIMPLEX,
0.7,
(0, 0, 255),
2,
cv2.LINE_AA,
)
return
x, y = pred_xy
px, py = label_to_pixel(
x,
y,
w,
h,
invert_y=invert_y,
)
target = (int(round(px)), int(round(py)))
cv2.arrowedLine(frame, center, target, (0, 255, 0), 2, tipLength=0.15)
cv2.drawMarker(
frame,
target,
(0, 255, 0),
markerType=cv2.MARKER_TILTED_CROSS,
markerSize=max(12, w // 14),
thickness=2,
)
text = f"x={x:.3f} y={y:.3f}"
if label:
text = f"{label} | {text}"
cv2.putText(
frame,
text,
(10, 28),
cv2.FONT_HERSHEY_SIMPLEX,
0.7,
(0, 255, 0),
2,
cv2.LINE_AA,
)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Run gaze prediction on a video and visualize the results."
)
parser.add_argument("--video", required=True, help="Path to the input video.")
parser.add_argument(
"--model",
default="trained_models/best_overall.keras",
help="Path to the trained Keras model.",
)
parser.add_argument(
"--input-size",
type=int,
default=96,
help="Resize frames to this square size before inference.",
)
parser.add_argument(
"--invert-y",
action=argparse.BooleanOptionalAction,
default=False,
help="Invert the Y sign when drawing.",
)
parser.add_argument(
"--output",
default=None,
help="Optional path to save the annotated video (e.g., output.mp4).",
)
parser.add_argument(
"--no-display",
action="store_true",
help="Disable the on-screen preview window.",
)
parser.add_argument(
"--max-frames",
type=int,
default=None,
help="Stop after processing this many frames.",
)
return parser.parse_args()
def main() -> None:
args = parse_args()
model_path = Path(args.model)
print("Loading model:", model_path)
if not model_path.exists():
raise FileNotFoundError(f"Model not found: {model_path}")
video_path = Path(args.video)
if not video_path.exists():
raise FileNotFoundError(f"Video not found: {video_path}")
model = keras.models.load_model(model_path)
cap = cv2.VideoCapture(str(video_path))
if not cap.isOpened():
raise RuntimeError(f"Could not open video {video_path}")
fps = cap.get(cv2.CAP_PROP_FPS)
if not fps or fps <= 0:
fps = 30.0
writer = None
if args.output:
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
if width == 0 or height == 0:
raise RuntimeError("Failed to read video dimensions.")
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
writer = cv2.VideoWriter(str(args.output), fourcc, fps, (width, height))
if not writer.isOpened():
raise RuntimeError(f"Could not open video writer for {args.output}")
if not args.no_display:
cv2.namedWindow("Gaze Prediction", cv2.WINDOW_NORMAL)
frame_index = 0
pred_xy: tuple[float, float] | None = None
paused = False
try:
while True:
if paused:
key = cv2.waitKey(50) & 0xFF
if key == ord("p"):
paused = False
elif key in (ord("q"), 27):
break
continue
ret, frame = cap.read()
if not ret:
break
if args.max_frames is not None and frame_index >= args.max_frames:
break
input_frame = prepare_frame(
frame,
args.input_size,
)
pred_xy = run_prediction(model, input_frame)
display_frame = frame.copy()
draw_prediction(
display_frame,
pred_xy,
invert_y=args.invert_y,
label=None,
)
if writer is not None:
writer.write(display_frame)
if not args.no_display:
cv2.imshow("Gaze prediction", display_frame)
delay_ms = max(1, int(1000 / fps))
key = cv2.waitKey(delay_ms) & 0xFF
if key == ord("p"):
paused = True
elif key in (ord("q"), 27):
break
frame_index += 1
finally:
cap.release()
if writer is not None:
writer.release()
if not args.no_display:
cv2.destroyAllWindows()
print(f"Processed {frame_index} frames.")
if __name__ == "__main__":
main()