|
import csv |
|
import cv2 |
|
from gaze_tracking import GazeTracking |
|
|
|
|
|
video_path = 'real.mp4' |
|
|
|
|
|
cap = cv2.VideoCapture(video_path) |
|
|
|
|
|
if not cap.isOpened(): |
|
print("Error opening video!") |
|
exit() |
|
|
|
|
|
output_filename = "fake_gaze_tracked_output.avi" |
|
|
|
|
|
fourcc = cv2.VideoWriter_fourcc(*'XVID') |
|
out = cv2.VideoWriter(output_filename, fourcc, 20.0, (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))) |
|
|
|
gaze = GazeTracking() |
|
|
|
frame_count = 0 |
|
|
|
while True: |
|
|
|
ret, frame = cap.read() |
|
|
|
|
|
if not ret: |
|
print("No frame captured from video. Exiting...") |
|
break |
|
|
|
|
|
with open('fake_gaze_data.csv', 'a', newline='') as csvfile: |
|
csv_writer = csv.writer(csvfile) |
|
|
|
|
|
gaze.refresh(frame) |
|
|
|
|
|
annotated_frame = gaze.annotated_frame() |
|
|
|
text = "" |
|
|
|
if gaze.is_blinking(): |
|
text = "Blinking" |
|
elif gaze.is_right(): |
|
text = "Looking right" |
|
elif gaze.is_left(): |
|
text = "Looking left" |
|
elif gaze.is_center(): |
|
text = "Looking center" |
|
|
|
|
|
cv2.putText(annotated_frame, text, (90, 60), cv2.FONT_HERSHEY_DUPLEX, 1.6, (147, 58, 31), 2) |
|
|
|
|
|
left_pupil = gaze.pupil_left_coords() |
|
right_pupil = gaze.pupil_right_coords() |
|
|
|
|
|
cv2.putText(annotated_frame, "Left pupil: " + str(left_pupil), (90, 130), cv2.FONT_HERSHEY_DUPLEX, 0.9, (147, 58, 31), 1) |
|
cv2.putText(annotated_frame, "Right pupil: " + str(right_pupil), (90, 165), cv2.FONT_HERSHEY_DUPLEX, 0.9, (147, 58, 31), 1) |
|
|
|
|
|
frame_count += 1 |
|
|
|
|
|
csv_writer.writerow([frame_count, text, left_pupil, right_pupil]) |
|
|
|
|
|
cv2.imshow("Demo", annotated_frame) |
|
|
|
|
|
out.write(annotated_frame) |
|
|
|
|
|
if cv2.waitKey(1) == 27: |
|
break |
|
|
|
|
|
cap.release() |
|
out.release() |
|
cv2.destroyAllWindows() |
|
|