diff --git a/EyeTrackApp/blob.py b/EyeTrackApp/blob.py index 3c59400..f6b71db 100644 --- a/EyeTrackApp/blob.py +++ b/EyeTrackApp/blob.py @@ -36,58 +36,49 @@ class EyeId(IntEnum): BOTH = 2 SETTINGS = 3 +def circle_crop(self): + global cct + print(cct) + if cct == 0: + try: + ht, wd = self.current_image_gray.shape[:2] + radius = int(float(self.lkg_projected_sphere["axes"][0])) + self.xc = int(float(self.lkg_projected_sphere["center"][0])) + self.yc = int(float(self.lkg_projected_sphere["center"][1])) + if radius < 8: #minimum size + radius = 8 + # draw filled circle in white on black background as mask + mask = np.zeros((ht, wd), dtype=np.uint8) + mask = cv2.circle(mask, (self.xc, self.yc), radius, 255, -1) + # create white colored background + color = np.full_like(self.current_image_gray, (255)) + # apply mask to image + masked_img = cv2.bitwise_and(self.current_image_gray, self.current_image_gray, mask=mask) + # apply inverse mask to colored image + masked_color = cv2.bitwise_and(color, color, mask=255 - mask) + # combine the two masked images + self.current_image_gray = cv2.add(masked_img, masked_color) + return self.current_image_gray + except: + return self.current_image_gray + pass + else: + cct = cct - 1 + return self.current_image_gray def BLOB(self): - + global cct # define circle - if self.eye_id in [EyeId.LEFT]: - if self.gui_circular_crop_left: - if self.cct == 0: - try: - ht, wd = self.current_image_gray.shape[:2] - - radius = int(float(self.lkg_projected_sphere["axes"][0])) - - # draw filled circle in white on black background as mask - mask = np.zeros((ht, wd), dtype=np.uint8) - mask = cv2.circle(mask, (self.xc, self.yc), radius, 255, -1) - # create white colored background - color = np.full_like(self.current_image_gray, (255)) - # apply mask to image - masked_img = cv2.bitwise_and(self.current_image_gray, self.current_image_gray, mask=mask) - # apply inverse mask to colored image - masked_color = cv2.bitwise_and(color, color, mask=255 - mask) - # combine the two masked images - self.current_image_gray = cv2.add(masked_img, masked_color) - except: - pass - else: - self.cct = self.cct - 1 - _, larger_threshold = cv2.threshold(self.current_image_gray, int(self.settings.gui_threshold + 12), 255, cv2.THRESH_BINARY) + if self.eye_id in [EyeId.LEFT] and self.settings.gui_circular_crop_left: + self.current_image_gray = circle_crop(self) else: - if self.gui_circular_crop_right: - if self.cct == 0: - try: - ht, wd = self.current_image_gray.shape[:2] + pass - radius = int(float(self.lkg_projected_sphere["axes"][0])) - - # draw filled circle in white on black background as mask - mask = np.zeros((ht, wd), dtype=np.uint8) - mask = cv2.circle(mask, (self.xc, self.yc), radius, 255, -1) - # create white colored background - color = np.full_like(self.current_image_gray, (255)) - # apply mask to image - masked_img = cv2.bitwise_and(self.current_image_gray, self.current_image_gray, mask=mask) - # apply inverse mask to colored image - masked_color = cv2.bitwise_and(color, color, mask=255 - mask) - # combine the two masked images - self.current_image_gray = cv2.add(masked_img, masked_color) - except: - pass - else: - self.cct = self.cct - 1 - _, larger_threshold = cv2.threshold(self.current_image_gray, int(self.settings.gui_threshold + 12), 255, - cv2.THRESH_BINARY) + if self.eye_id in [EyeId.RIGHT] and self.settings.gui_circular_crop_right: + self.current_image_gray = circle_crop(self) + else: + pass + _, larger_threshold = cv2.threshold(self.current_image_gray, int(self.settings.gui_threshold), 255, + cv2.THRESH_BINARY) try: # Try rebuilding our contours @@ -104,46 +95,29 @@ def BLOB(self): pass rows, cols = larger_threshold.shape - + for cnt in contours: (x, y, w, h) = cv2.boundingRect(cnt) - # if our blob width/height are within suitable (yet arbitrary) boundaries, call that good. - # - # TODO This should be scaled based on camera resolution. + # if our blob width/height are within boundaries, call that good. if not self.settings.gui_blob_minsize <= h <= self.settings.gui_blob_maxsize or not self.settings.gui_blob_minsize <= w <= self.settings.gui_blob_maxsize: continue cx = x + int(w / 2) - cy = y + int(h / 2) - # cv2.line( - # self.current_image_gray, - # (x + int(w / 2), 0), - # (x + int(w / 2), rows), - # (255, 0, 0), - # 1, - # ) # visualizes eyetracking on thresh - # cv2.line( - # self.current_image_gray, - # (0, y + int(h / 2)), - # (cols, y + int(h / 2)), - # (255, 0, 0), - # 1, - # ) - cv2.drawContours(self.current_image_gray, [cnt], -1, (255, 0, 0), 3) + cv2.drawContours(self.current_image_gray, [cnt], -1, (0, 0, 0), 3) cv2.rectangle( - self.current_image_gray, (x, y), (x + w, y + h), (255, 0, 0), 2 + self.current_image_gray, (x, y), (x + w, y + h), (0, 0, 0), 2 ) #out_x, out_y = cal_osc(self, cx, cy) #filter and calibrate values - + print("S:KLJGHLDKIJGHLKS") self.failed = 0 return cx, cy, larger_threshold - + print("S:KLJGHLDKIJGHLKS") self.failed = self.failed + 1 return 0, 0, larger_threshold \ No newline at end of file diff --git a/EyeTrackApp/eye_processor.py b/EyeTrackApp/eye_processor.py index 7204bc7..8536d35 100644 --- a/EyeTrackApp/eye_processor.py +++ b/EyeTrackApp/eye_processor.py @@ -295,7 +295,9 @@ class EyeProcessor: self.current_algorithm = EyeInfoOrigin.RANSAC def BLOBM(self): + print("LSKDGFHL") self.rawx, self.rawy, self.thresh = BLOB(self) + self.out_x, self.out_y = cal.cal_osc(self, self.rawx, self.rawy) self.current_algorithm = EyeInfoOrigin.BLOB @@ -326,7 +328,7 @@ class EyeProcessor: else: self.failed = 0 # we have reached last possible algo and it is disabled, move to first algo - + diff --git a/EyeTrackApp/hsrac.py b/EyeTrackApp/hsrac.py index 99a324f..297d62c 100644 --- a/EyeTrackApp/hsrac.py +++ b/EyeTrackApp/hsrac.py @@ -488,7 +488,7 @@ class HSRAC_cls(object): # cv_end_time = timeit.default_timer() # self.timedict["ransac"].append(cv_end_time - ransac_start_time) # self.timedict["total_cv"].append(cv_end_time - cv_start_time) - print(radius) +# print(radius) try: y, x = ori_frame.shape th_frame = cv2.resize(th_frame, (x, y)) diff --git a/EyeTrackApp/intensity_based_openness.py b/EyeTrackApp/intensity_based_openness.py index 053068a..01baed4 100644 --- a/EyeTrackApp/intensity_based_openness.py +++ b/EyeTrackApp/intensity_based_openness.py @@ -352,5 +352,5 @@ class IntensityBasedOpeness: #print(eyevec) if eyevec > 0.4: print("BLINK LCOK") - print(eyeopen) + # print(eyeopen) return eyeopen diff --git a/EyeTrackApp/ransac.py b/EyeTrackApp/ransac.py index 3268f8d..3d017aa 100644 --- a/EyeTrackApp/ransac.py +++ b/EyeTrackApp/ransac.py @@ -27,7 +27,13 @@ Copyright (c) 2023 EyeTrackVR <3 ''' import cv2 import numpy as np -from enums import EyeLR +from enum import IntEnum + +class EyeId(IntEnum): + RIGHT = 0 + LEFT = 1 + BOTH = 2 + SETTINGS = 3 def ellipse_model(data, y, f): @@ -135,15 +141,17 @@ def fit_rotated_ellipse(data, P): return (cx, cy, w, h, theta) - +cct = 300 def circle_crop(self): - if self.cct == 0: + global cct + print(cct) + if cct == 0: try: ht, wd = self.current_image_gray.shape[:2] radius = int(float(self.lkg_projected_sphere["axes"][0])) self.xc = int(float(self.lkg_projected_sphere["center"][0])) self.yc = int(float(self.lkg_projected_sphere["center"][1])) - if radius < 8: #minimum size TODO: make shure this size is enough + if radius < 8: #minimum size radius = 8 # draw filled circle in white on black background as mask mask = np.zeros((ht, wd), dtype=np.uint8) @@ -156,13 +164,18 @@ def circle_crop(self): masked_color = cv2.bitwise_and(color, color, mask=255 - mask) # combine the two masked images self.current_image_gray = cv2.add(masked_img, masked_color) + return self.current_image_gray except: + return self.current_image_gray pass else: - self.cct = self.cct - 1 + cct = cct - 1 + return self.current_image_gray + def RANSAC3D(self): f = False + global cct kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)) thresh_add = 10 rng = np.random.default_rng() @@ -176,15 +189,15 @@ def RANSAC3D(self): # crop the image earlier; it gives us less possible dark area to get confused about in the # next step. - if self.eye_id == EyeId.LEFT and self.config.gui_circular_crop_left == True: #TODO TEST function - circle_crop(self) + if self.eye_id in [EyeId.LEFT] and self.settings.gui_circular_crop_left: + self.current_image_gray = circle_crop(self) else: - self.cct = 300 + pass - if self.eye_id == EyeId.RIGHT and self.config.gui_circular_crop_right == True: - circle_crop(self) + if self.eye_id in [EyeId.RIGHT] and self.settings.gui_circular_crop_right: + self.current_image_gray = circle_crop(self) else: - self.cct = 300 + pass # Crop first to reduce the amount of data to process. newFrame2 = self.current_image_gray.copy() diff --git a/EyeTrackApp/settings_widget.py b/EyeTrackApp/settings_widget.py index df953e9..3d27968 100644 --- a/EyeTrackApp/settings_widget.py +++ b/EyeTrackApp/settings_widget.py @@ -508,12 +508,12 @@ class SettingsWidget: self.config.gui_IBO = values[self.gui_IBO] changed = True - if self.config.gui_circular_crop_left != values[self.gui_circular_crop_left]: + if self.config.gui_circular_crop_left != values[self.gui_circular_crop_left]: self.config.gui_circular_crop_left = values[self.gui_circular_crop_left] changed = True if self.config.gui_circular_crop_right != values[self.gui_circular_crop_right]: - self.gui_circular_crop_right = values[self.gui_circular_crop_right] + self.config.gui_circular_crop_right = values[self.gui_circular_crop_right] changed = True if self.config.gui_HSF_radius != int(values[self.gui_HSF_radius]):