File size: 2,571 Bytes
2bf3ef1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import csv
import cv2
from gaze_tracking import GazeTracking

# Replace 'video.mp4' with the path to your video file
video_path = 'real.mp4'

# Create a video capture object
cap = cv2.VideoCapture(video_path)

# Check if video opened successfully
if not cap.isOpened():
    print("Error opening video!")
    exit()

# Define output filename (change extension if needed)
output_filename = "fake_gaze_tracked_output.avi"

# Define video writer settings (adjust as needed)
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:
    # Read a frame from the video
    ret, frame = cap.read()

    # Check if frame is read correctly
    if not ret:
        print("No frame captured from video. Exiting...")
        break

    # Open CSV file for appending gaze data
    with open('fake_gaze_data.csv', 'a', newline='') as csvfile:
        csv_writer = csv.writer(csvfile)

        # Send the frame to GazeTracking for analysis
        gaze.refresh(frame)

        # Get the annotated frame with gaze information
        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"

        # Add text overlay on the frame
        cv2.putText(annotated_frame, text, (90, 60), cv2.FONT_HERSHEY_DUPLEX, 1.6, (147, 58, 31), 2)

        # Get pupil coordinates
        left_pupil = gaze.pupil_left_coords()
        right_pupil = gaze.pupil_right_coords()

        # Add pupil coordinates overlay
        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)

        # Update frame count
        frame_count += 1

        # Write gaze data to CSV file
        csv_writer.writerow([frame_count, text, left_pupil, right_pupil])

        # Display the frame
        cv2.imshow("Demo", annotated_frame)

        # Write the annotated frame to the output video
        out.write(annotated_frame)

        # Exit on 'Esc' key press
        if cv2.waitKey(1) == 27:
            break

# Release resources
cap.release()
out.release()
cv2.destroyAllWindows()