예제 #1
0
    def load_cfg(self, config_file_path=None):

        if config_file_path is None:
            config_file_path = self._cfg_file_path

        if os.path.isfile(config_file_path):
            print('load existing config file.')
            with open(config_file_path, 'r') as cfg_f:
                params = yaml.load(cfg_f)
            self.detector = dt.PupilLedDetector()
            self.detector.load_parameters(**params)
        else:
            print('did not find config file. load default config.')
            self.detector = dt.PupilLedDetector()

        print(self.detector.param_str)
예제 #2
0
    def test_PupilLedDetector_detect3(self):

        cap = cv2.VideoCapture(os.path.join('test_files', 'test2.avi'))
        cap.set(cv2.CAP_PROP_POS_FRAMES, 35)
        _, frame0 = cap.read()
        cap.set(cv2.CAP_PROP_POS_FRAMES, 36)
        _, frame1 = cap.read()

        det = dt.PupilLedDetector(led_binary_threshold=200,
                                  led_blur=2,
                                  led_mask_dilation=5,
                                  led_max_size=1000.0,
                                  led_min_size=1.0,
                                  led_openclose_iter=1,
                                  led_roi=[209, 293, 224, 321],
                                  pupil_binary_threshold=240,
                                  pupil_blur=2,
                                  pupil_is_equalize=True,
                                  pupil_min_size=500.0,
                                  pupil_openclose_iter=15,
                                  pupil_roi=[102, 352, 131, 431])

        det.load_frame(frame0)
        det.detect(last_pupil=None)

        last_pupil = det.pupil

        det.load_frame(frame1)
        det.detect(last_pupil=None)
        det.show_results()
        plt.show()

        det.detect(last_pupil=last_pupil)
        det.show_results()
        plt.show()
예제 #3
0
    def _load_config_clicked(self):

        # print('self._load_config_clicked()')

        if self.movie_path is None:
            # self.led_roi.setPos((40, 40))
            # self.led_roi.setSize((20, 20))
            # self.pupil_roi.setPos((20, 20))
            # self.pupil_roi.setSize((60, 60))
            print('self.movie_path is None. Cannot load config file.')

        else:
            if (self.config_path is not None) and (os.path.isfile(
                    self.config_path)):
                print('\nloading existing config file: \n{}'.format(
                    self.config_path))
                with open(self.config_path, 'r') as config_f:
                    param_dict = yaml.load(config_f)
            else:
                print(
                    '\ncannnot find existing config file. load the default detector parameters.'
                )
                detector = dt.PupilLedDetector()
                param_dict = detector.get_parameter_dict()

            if param_dict['led_roi'] is None:
                self.led_roi.setPos((0, 0))
                self.led_roi.setSize(
                    (self.movie_frame_shape[1], self.movie_frame_shape[0]))
                param_dict['led_roi'] = self._qt_roi_2_detector_roi(
                    self.led_roi)
            else:
                led_roi_pos, led_roi_size = self._detector_roi_2_qt_roi(
                    param_dict['led_roi'])
                self.led_roi.setPos(led_roi_pos)
                self.led_roi.setSize(led_roi_size)

            if param_dict['pupil_roi'] is None:
                self.pupil_roi.setPos((0, 0))
                self.pupil_roi.setSize(
                    (self.movie_frame_shape[1], self.movie_frame_shape[0]))
                param_dict['pupil_roi'] = self._qt_roi_2_detector_roi(
                    self.pupil_roi)
            else:
                pupil_roi_pos, pupil_roi_size = self._detector_roi_2_qt_roi(
                    param_dict['pupil_roi'])
                self.pupil_roi.setPos(pupil_roi_pos)
                self.pupil_roi.setSize(pupil_roi_size)

            self.detector.load_parameters(**param_dict)
            self._show_detector_parameters()
            print(self.detector.param_str)
예제 #4
0
    def test_PupilLedDetector_detect4(self):

        frame3 = np.load(os.path.join('test_files', 'test_img3.npy'))
        det = dt.PupilLedDetector(led_roi=(275, 375, 233, 353),
                                  pupil_roi=(176, 426, 146, 446),
                                  led_binary_threshold=240,
                                  led_openclose_iter=1,
                                  led_mask_dilation=20,
                                  pupil_blur=10,
                                  pupil_binary_threshold=250,
                                  pupil_openclose_iter=5,
                                  pupil_min_size=500,
                                  pupil_is_equalize=True)
        det.load_frame(frame=frame3)
        det.detect()
        det.show_results()
        plt.show()
        source1 = det.pupil_blurred
        th1_o = det.pupil_thresholded

        frame1 = np.load(os.path.join('test_files', 'test_img.npy'))
        det = dt.PupilLedDetector(led_roi=(200, 300, 280, 400),
                                  pupil_roi=(100, 350, 200, 500),
                                  led_binary_threshold=240,
                                  led_openclose_iter=1,
                                  led_mask_dilation=20,
                                  pupil_blur=10,
                                  pupil_binary_threshold=250,
                                  pupil_openclose_iter=10,
                                  pupil_min_size=500,
                                  pupil_is_equalize=True)

        det.load_frame(frame=frame1)
        det.detect()
        det.show_results()
        plt.show()
        source2 = det.pupil_blurred
        th2_o = det.pupil_thresholded
예제 #5
0
 def test_PupilLedDetector_detect(self):
     img = np.load(os.path.join('test_files', 'test_img.npy'))
     det = dt.PupilLedDetector(led_roi=(200, 300, 280, 400),
                               pupil_roi=(100, 350, 200, 500),
                               led_binary_threshold=200,
                               led_openclose_iter=1,
                               led_mask_dilation=20,
                               pupil_binary_threshold=240,
                               pupil_openclose_iter=10,
                               pupil_min_size=500)
     det.load_frame(frame=img)
     det.detect()
     det.show_results()
     plt.show()
예제 #6
0
 def test_PupilLedDetector_detect2(self):
     img = np.load(os.path.join('test_files', 'test_img2.npy'))
     det = dt.PupilLedDetector(led_roi=(80, 140, 130, 200),
                               pupil_roi=(0, 240, 0, 320),
                               led_binary_threshold=250,
                               led_openclose_iter=1,
                               pupil_binary_threshold=200,
                               pupil_openclose_iter=5,
                               pupil_min_size=500,
                               led_mask_dilation=20,
                               pupil_is_equalize=False)
     det.load_frame(frame=img)
     det.detect()
     det.show_results()
     plt.show()
예제 #7
0
    def test_PupilLedDetector_detect5(self):

        frame4 = np.load(os.path.join('test_files', 'test_img4.npy'))
        det = dt.PupilLedDetector(led_roi=[220, 291, 295, 362],
                                  pupil_roi=[157, 333, 241, 439],
                                  led_binary_threshold=200,
                                  led_openclose_iter=5,
                                  led_mask_dilation=20,
                                  pupil_blur=10,
                                  pupil_binary_threshold=180,
                                  pupil_openclose_iter=10,
                                  pupil_min_size=500,
                                  pupil_is_equalize=True)

        det.load_frame(frame=frame4)
        det.detect()
        det.show_results()
        plt.show()
예제 #8
0
    def clear(self):

        # print('self.clear()')

        if hasattr(self, 'status') and self.status == 2:  # if movie is playing
            self._pause_movie()

        # set slider
        self.ui.horizontalSlider_currentFrame.setRange(0, 1)
        self.ui.horizontalSlider_currentFrame.setValue(0)
        self.ui.horizontalSlider_currentFrame.setEnabled(False)

        # set current frame
        self.ui.lineEdit_currframeNum.setText('0')
        self.ui.lineEdit_currframeNum.setEnabled(False)

        # setup pupil and led rois
        if hasattr(self, 'pupil_roi'):
            self.pupil_roi.setPos((20, 20))
            self.pupil_roi.setSize((60, 60))
            self.pupil_roi.setPen((0, 255, 0))
            self.pupil_roi.removable = False
        else:
            self.pupil_roi = pg.ROI([20, 20], [60, 60],
                                    pen=(0, 255, 0),
                                    removable=False)
            self.pupil_roi.handleSize = 10
            self.pupil_roi.addScaleHandle([1, 0], [0, 1])
            self.pupil_roi.addScaleHandle([0, 0], [1, 1])
            self.pupil_roi.addScaleHandle([1, 1], [0, 0])
            self.pupil_roi.addScaleHandle([0, 1], [1, 0])
            self.movie_view.addItem(self.pupil_roi)

        if hasattr(self, 'led_roi'):
            self.led_roi.setPos((40, 40))
            self.led_roi.setSize((20, 20))
            self.led_roi.setPen((255, 0, 0))
            self.led_roi.removable = False
        else:
            self.led_roi = pg.ROI(
                [40, 40],
                [20, 20],
                pen=(255, 0, 0),
                removable=False,
            )
            self.led_roi.handleSize = 10
            self.led_roi.addScaleHandle([1, 0], [0, 1])
            self.led_roi.addScaleHandle([0, 0], [1, 1])
            self.led_roi.addScaleHandle([1, 1], [0, 0])
            self.led_roi.addScaleHandle([0, 1], [1, 0])
            self.movie_view.addItem(self.led_roi)

        # set background image
        # print('resetting image')
        self.movie.setImage(np.zeros((100, 100), dtype=np.uint8))

        # setup movie properties
        self.video_capture = None
        self.movie_path = None
        self.movie_frame_num = 0
        self.movie_frame_shape = (100, 100)  # (height, width)
        self.movie_fps = None
        self.curr_frame_ind = None
        self.last_pupil = (None, None)  # (frame ind, pupil Ellipse)
        self._show_movie_info()

        # set buttons
        self.ui.pushButton_pauseplay.setIcon(
            QtGui.QIcon(os.path.join(PACKAGE_DIR, "res", "play.png")))
        self.ui.pushButton_pauseplay.setEnabled(False)
        self.ui.pushButton_saveConfig.setEnabled(False)
        self.ui.pushButton_showResult.setEnabled(False)
        self.ui.pushButton_process.setEnabled(False)

        # initiated detector
        self.detector = dt.PupilLedDetector(
            led_roi=self._qt_roi_2_detector_roi(self.led_roi),
            pupil_roi=self._qt_roi_2_detector_roi(self.pupil_roi))
        self._show_detector_parameters()

        # remove result
        self.ui.textBrowser_results.setText('')

        self.status = 0