Skip to content

Commit

Permalink
adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhomb committed Jul 22, 2024
1 parent 33d60e1 commit 7aa02f3
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 5 deletions.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/GazeTracking.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added data/left_23_XX_2024-07-22_10-45-39.npy
Binary file not shown.
Binary file added data/left_23_XX_2024-07-22_10-54-11.npy
Binary file not shown.
64 changes: 59 additions & 5 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,54 @@
Demonstration of the GazeTracking library.
Check the README.md for complete documentation.
"""

import datetime
import os
import cv2
from gaze_tracking import GazeTracking
import time
import utils
import numpy as np

stim_dir = "left"
stim_dist = "23"
stim_trial = "XX"
stim_time = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
savefile = f"{stim_dir}_{stim_dist}_{stim_trial}_{stim_time}.npy"

print_interval = 1000 # n frames to update to console
new_width = 400
new_height = 300

center = (new_width, new_height)
x = center[1] / 2 - new_width / 2
y = center[0] / 2 - new_height / 2

gaze = GazeTracking()
webcam = cv2.VideoCapture(0)
webcam = cv2.VideoCapture(max(utils.list_webcams()))
init_time = time.perf_counter()
framecount = 0

# TODO: get monitor resolution, adjust params
monitor_res_y = 1080
monitor_res_x = 720
patient_distance = 40 # cm

study_eye = "left"
study_eye_positions = []

while True:
loop_start_time = time.perf_counter()
framecount += 1

# We get a new frame from the webcam
_, frame = webcam.read()

# We send this frame to GazeTracking to analyze it
gaze.refresh(frame)
# Resize the image
frame = cv2.resize(frame, (new_width, new_height))
crop_img = frame[int(y):int(y + new_height), int(x):int(x + new_width)]

# We send this frame to GazeTracking to analyze it
gaze.refresh(frame) # this is the most expensive operation
frame = gaze.annotated_frame()
text = ""

Expand All @@ -32,13 +66,33 @@

left_pupil = gaze.pupil_left_coords()
right_pupil = gaze.pupil_right_coords()
if left_pupil is None and right_pupil is None:
print ("No pupils detected")
if study_eye == "left":
if left_pupil is not None:
study_eye_positions.append(left_pupil)
deviation = utils.distance_from_fixation((crop_img.shape[0] // 2, crop_img.shape[1] // 2), left_pupil)
# study_eye_deviations.append(deviation)
elif study_eye == "right":
if right_pupil is not None:
study_eye_positions.append(right_pupil)
deviation = utils.distance_from_fixation((crop_img.shape[0] // 2, crop_img.shape[1] // 2), right_pupil)
# study_eye_deviations.append(deviation)
cv2.putText(frame, "Left pupil: " + str(left_pupil), (90, 130), cv2.FONT_HERSHEY_DUPLEX, 0.9, (147, 58, 31), 1)
cv2.putText(frame, "Right pupil: " + str(right_pupil), (90, 165), cv2.FONT_HERSHEY_DUPLEX, 0.9, (147, 58, 31), 1)

cv2.imshow("Demo", frame)

if cv2.waitKey(1) == 27:
break

fps = 1/(time.perf_counter() - loop_start_time)
if framecount % print_interval == 0:
print(f"FPS: {fps}")

study_eye_deviations = [utils.distance_from_fixation((crop_img.shape[0] // 2, crop_img.shape[1] // 2), pos) for pos in study_eye_positions]
print ("Average deviation of study eye from fixation: ", np.round(np.mean(study_eye_deviations), 4), "over ", framecount, "frames")
print ("Std dev of deviation of study eye from fixation: ", np.round(np.std(study_eye_deviations), 4), "over ", framecount, "frames")
print (os.getcwd())
np.save(os.path.join("data", savefile), study_eye_positions)
webcam.release()
cv2.destroyAllWindows()
20 changes: 20 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@


import cv2

def list_webcams():
index = 0
arr = []
while True:
cap = cv2.VideoCapture(index)
if not cap.read()[0]:
break
else:
arr.append(index)
cap.release()
index += 1
return arr


def distance_from_fixation(origin, point):
return ((origin[0] - point[0]) ** 2 + (origin[1] - point[1]) ** 2) ** 0.5

0 comments on commit 7aa02f3

Please sign in to comment.