forked from antoinelame/GazeTracking
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ad07fc1
commit 76aa16e
Showing
4 changed files
with
110 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
from __future__ import division | ||
import cv2 | ||
from .pupil import Pupil | ||
|
||
|
||
class Calibration(object): | ||
""" | ||
This class calibrates the pupil detection algorithm by finding | ||
the best threshold value for the person and the webcam. | ||
""" | ||
|
||
def __init__(self): | ||
self.nb_frames = 20 | ||
self.thresholds_left = [] | ||
self.thresholds_right = [] | ||
|
||
def is_complete(self): | ||
"""Returns true if the calibration is completed""" | ||
return len(self.thresholds_left) >= self.nb_frames and len(self.thresholds_right) >= self.nb_frames | ||
|
||
def threshold(self, side): | ||
"""Returns the threshold value for the given eye. | ||
Argument: | ||
side: 0 for left and 1 for right | ||
""" | ||
if side == 0: | ||
return int(sum(self.thresholds_left) / len(self.thresholds_left)) | ||
elif side == 1: | ||
return int(sum(self.thresholds_right) / len(self.thresholds_right)) | ||
|
||
@staticmethod | ||
def iris_size(frame): | ||
"""Returns the percentage of space that the iris takes up on | ||
the surface of the eye. | ||
Argument: | ||
frame: the iris frame | ||
""" | ||
frame = frame[5:-5, 5:-5] | ||
height, width = frame.shape[:2] | ||
nb_pixels = height * width | ||
nb_blacks = nb_pixels - cv2.countNonZero(frame) | ||
return nb_blacks / nb_pixels | ||
|
||
@staticmethod | ||
def find_best_threshold(eye_frame): | ||
"""Calculates the optimal threshold for the given eye. | ||
Argument: | ||
eye_frame: eye's frame to analyse | ||
""" | ||
average_iris_size = 0.48 | ||
trials = {} | ||
|
||
for threshold in range(5, 100, 5): | ||
iris_frame = Pupil.image_processing(eye_frame, threshold) | ||
trials[threshold] = Calibration.iris_size(iris_frame) | ||
|
||
best_threshold, iris_size = min(trials.items(), key=(lambda p: abs(p[1] - average_iris_size))) | ||
return best_threshold | ||
|
||
def evaluate(self, eye_frame, side): | ||
"""Improves calibration by taking into consideration the | ||
given image. | ||
Arguments: | ||
eye_frame: eye's frame | ||
side: 0 for left and 1 for right | ||
""" | ||
threshold = self.find_best_threshold(eye_frame) | ||
|
||
if side == 0: | ||
self.thresholds_left.append(threshold) | ||
elif side == 1: | ||
self.thresholds_right.append(threshold) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters