Esempio n. 1
0
class Main:
    def __init__(self):
        # Global variables for executions
        self._title_exam = ''
        self._path_dataset_out = ''

        # Dependences
        self._process = ProcessImage()
        self._pupil = Pupil()
        self._eye = Eye()

        # Limit cash dependences
        self._max_execution_with_cash = 20

        # Directoris
        self._projects_path = '/media/marcos/Dados/Projects'

        self._dataset_path = '{}/Datasets/exams'.format(self._projects_path)
        self._dataset_out = '{}/Results/PupilDeep/Frames'.format(
            self._projects_path)
        self._dataset_label = '{}/Results/PupilDeep/Labels'.format(
            self._projects_path)

        # Stops and executions
        self._frame_stop = 150
        self._frame_start = 100

        self._movie_stop = 0
        self._list_not_available = []
        self._list_available = [
            '25080325_08_2019_08_48_58', '25080425_08_2019_08_53_48'
        ]
        # self._list_available = ['25080325_08_2019_08_48_58', '25080425_08_2019_08_53_48', '25080425_08_2019_08_55_59', '25080425_08_2019_09_05_40', '25080425_08_2019_09_08_25']
        # self._list_available = ['new_benchmark']

        # Params color
        self._white_color = (255, 255, 0)
        self._gray_color = (170, 170, 0)

        # Params text and circle print image
        self._position_text = (30, 30)
        self._font_text = cv2.FONT_HERSHEY_DUPLEX
        self._size_point_pupil = 5

        # Params dataset labels out
        self._title_label = 'frame,center_x,center_y,radius,flash,eye_size,img_mean,img_std,img_median'

    def _add_label(self, information):
        with open('{}/{}_label.csv'.format(self._dataset_label,
                                           self._title_exam),
                  'a',
                  newline='') as file:
            file.write('{}\n'.format(information))
            file.close()

    def _make_path(self, path=''):
        try:
            if path == '':
                os.mkdir(self._path_dataset_out)
            else:
                os.mkdir(path)
        except FileExistsError:
            pass

    def _show_image(self, image, label, number_frame, color=None):
        system_continue = True
        paint = self._white_color if color is None else color
        cv2.putText(image, label, self._position_text, self._font_text, 0.9,
                    paint)

        # cv2.namedWindow('Analysis', cv2.WINDOW_NORMAL)
        # cv2.imshow('Analysis', image)
        # order = cv2.waitKey(1)
        #
        # if order == 32:
        #     time.sleep(2)
        # elif order == ord('q'):
        #     system_continue = False

        self._save_images({'final': image}, number_frame)
        return system_continue

    def _save_images(self, images, number_frame, center=(0, 0)):
        for key, value in images.items():
            if 'binary' in key:
                image = self._mark_center(value, center)
            else:
                image = value

            out = '{}/{}_{}.png'.format(self._path_dataset_out, key,
                                        number_frame)
            cv2.imwrite(out, image)

    def _save_histogram(self, histogram, number_frame):
        pl.hist(histogram, bins='auto')
        pl.title('Histogram Frame: {}'.format(number_frame))
        pl.xlabel("Value")
        pl.ylabel("Frequency")
        pl.savefig("{}/histogram_{}.png".format(self._path_dataset_out,
                                                number_frame))

    def _mark_eye(self, image, right, left):
        cv2.line(image, (right[0], right[1]), (left[0], left[1]),
                 self._white_color, 1)
        return image

    def _mark_center(self, image, center):
        color = self._white_color
        cv2.line(image, (center[0] - 10, center[1]),
                 (center[0] + 10, center[1]), color, 1)
        cv2.line(image, (center[0], center[1] - 10),
                 (center[0], center[1] + 10), color, 1)
        return image

    def _draw_circles(self, image, points, radius=0, color=None):
        for point in points:
            rad = radius if radius > 0 else self._size_point_pupil
            paint = self._gray_color if color is None else color
            cv2.circle(image, (point[0], point[1]), rad, paint, 2)

        return image

    def _pupil_process(self, path_exam):
        number_frame = 0
        system_continue = True

        exam = cv2.VideoCapture(path_exam)

        while system_continue:
            _, frame = exam.read()

            if (frame is None) or ((self._frame_stop > 0) and
                                   (number_frame >= self._frame_stop)):
                break

            if (self._frame_start > 0) and (number_frame < self._frame_start):
                number_frame += 1
                continue

            original = np.copy(frame)
            img_orig_gray = cv2.cvtColor(original, cv2.COLOR_BGR2GRAY)
            self._save_images({'original': original}, number_frame)

            img_process, flash = self._process.process_image(original)
            self._save_images({'process': img_process}, number_frame)

            img_mean, img_std, img_median = img_process.mean(
            ), img_process.std(), np.median(img_process)

            center, radius, points, images = self._pupil.pupil_detect(
                img_process)

            binary = images['binary_pre_process']
            binary = self._mark_center(binary, center)
            binary = self._draw_circles(binary, points, 2, self._white_color)
            self._save_images({'binary': binary}, number_frame, center)

            img_process = self._mark_center(img_process, center)
            img_process = self._draw_circles(img_process, points, 2,
                                             self._white_color)
            img_process = self._draw_circles(img_process,
                                             [(center[0], center[1])], radius,
                                             self._white_color)
            self._save_images({'img_process': img_process}, number_frame)

            self._save_histogram(images['histogram'], number_frame)

            img_presentation = cv2.hconcat(
                [img_orig_gray, binary, img_process])
            label = 'Frame=%d;Radius=%d;Center=(%d,%d);Eye=(%d);Flash=(%d)' % (
                number_frame, radius, center[0], center[1], 0, flash)

            system_continue = self._show_image(img_presentation, label,
                                               number_frame)

            self._add_label("{},{},{},{},{},{},{},{},{}".format(
                number_frame, center[0], center[1], radius, flash, 0, img_mean,
                img_std, img_median))

            number_frame += 1

        cv2.destroyAllWindows()
        exam.release()

    def run(self):
        files = os.listdir(self._dataset_path)

        number_movie = 0
        for file in files:
            if (self._movie_stop > 0) and (number_movie >= self._movie_stop):
                break

            self._title_exam = file.replace('.mp4', '')
            self._path_dataset_out = '{}/{}'.format(self._dataset_out,
                                                    self._title_exam)

            if (len(self._list_available) >
                    0) and (self._title_exam not in self._list_available):
                continue

            if self._title_exam in self._list_not_available:
                continue

            self._add_label(self._title_label)
            self._make_path()

            start_time = time.time()

            path_exam = '{}/{}'.format(self._dataset_path, file)
            self._pupil_process(path_exam)

            end_time = time.time()

            self._add_label('Execition time: {} minuts'.format(
                (end_time - start_time) / 60))

            number_movie += 1
Esempio n. 2
0
    def pupil_process(self, paths):
        pupil_deep = PupilDeep()
        process = ProcessImage()
        pupil = Pupil(pupil_deep)
        draw = DrawImages()
        # information = Information()

        self._path_label = paths['path_label']
        self._add_label(self._title_label)

        exam = cv2.VideoCapture(paths['path_exam'])
        fps = exam.get(cv2.CAP_PROP_FPS)

        # patient_exam, param_exam = information.get_information_exam(paths['path_information'], fps)

        number_frame = 0
        while True:
            _, frame = exam.read()

            if (frame is None) or ((self._frame_stop > 0) and (number_frame >= self._frame_stop)):
                break

            if (self._frame_start > 0) and (number_frame < self._frame_start):
                number_frame += 1
                continue

            original = np.copy(frame)
            img_orig_gray = cv2.cvtColor(original, cv2.COLOR_BGR2GRAY)
            # self._save_images({'original': original}, number_frame, paths['path_out'])

            img_process, flash = process.process_image(original)
            # self._save_images({'process': img_process}, number_frame, paths['path_out'])

            img_mean, img_std, img_median = img_process.mean(), img_process.std(), np.median(img_process)

            center, radius, points, images, mean_binary = pupil.pupil_detect(img_process)

            # binary = images['binary_pre_process']
            # binary = draw.mark_center(binary, center)
            # binary = draw.draw_circles(binary, points, 2, self._white_color)
            # self._save_images({'binary': binary}, number_frame, paths['path_out'], center)

            img_process = draw.mark_center(img_process, center)
            img_process = draw.draw_circles(img_process, points, 2, self._white_color)
            img_process = draw.draw_circles(img_process, [(center[0], center[1])], radius, self._white_color)
            self._save_images({'img_process': img_process}, number_frame, paths['path_out'])

            # self._save_histogram(images['histogram'], number_frame, paths['path_out'])

            # img_presentation = cv2.hconcat([img_orig_gray, binary, img_process])

            # label = 'Frame=%d;Radius=%d;Center=(%d,%d);BinMean=(%f)' % (
            #     number_frame, radius, center[0], center[1], mean_binary)

            # self._show_image(img_presentation, label, number_frame, paths['path_out'])

            # flash_information, color_information = information.get_information_params(number_frame)

            params = ['patient_exam', 'param_exam', number_frame, center[0], center[1], radius, flash, 'flash_information',
                      'color_information', 0, img_mean, img_std, img_median]

            self._add_params_label(params)

            number_frame += 1

        cv2.destroyAllWindows()
        exam.release()
Esempio n. 3
0
class Main:
    def __init__(self):
        self.dataset_path = r'C:\Projects\Datasets\eye_test\movies'
        self.dataset_out = r'C:\Projects\Datasets\eye_test\out'
        self.dataset_label = r'C:\Projects\Datasets\eye_test\label'

        self.title = ''
        self.dataset_out_exam = ''

        self.eye_tracker = deepeye.DeepEye()
        self.pupil = Pupil()
        self.reflections = Reflections()

    def _add_label(self, information):
        with open(r'{}\{}_label.csv'.format(self.dataset_label, self.title),
                  'a',
                  newline='') as file:
            file.write('{}\n'.format(information))
            file.close()

    def _make_dir(self, dir):
        try:
            os.mkdir(dir)
        except FileExistsError:
            pass

    def _pupil_process(self, exam):
        number_frame = 0

        while True:
            _, frame = exam.read()

            if frame is None:
                break

            original = np.copy(frame[:, :, 0])

            yuv = cv2.cvtColor(frame, cv2.COLOR_BGR2YUV)
            yuv[:, :, 0] = cv2.equalizeHist(yuv[:, :, 0])
            bgr = cv2.cvtColor(yuv, cv2.COLOR_YUV2BGR)

            gray = cv2.cvtColor(bgr, cv2.COLOR_BGR2GRAY)
            gaussian = cv2.GaussianBlur(gray, (9, 9), 3)
            median = cv2.medianBlur(gaussian, 3)

            kernel = np.ones((5, 5), np.uint8)
            erode = cv2.erode(median, kernel=kernel, iterations=1)
            dilate = cv2.dilate(erode, kernel=kernel, iterations=1)
            threshold = cv2.threshold(dilate, 25, 255, cv2.THRESH_BINARY)[1]

            final = np.copy(dilate)

            center = self.eye_tracker.run(final)
            default = dilate[center[0], center[1]]

            img_filter = []
            for line in dilate:
                img_filter.append(
                    [x if x not in range(180, 255) else default for x in line])

            final = np.copy(img_filter)

            edges = self.pupil.pupil_detect(final, center)

            lin, col = gray.shape
            if 0 < center[0] < lin and 0 < center[1] < col:
                cv2.circle(final, (int(center[0]), int(center[1])), 10,
                           (255, 255, 0), 2)

            for i in range(len(edges) - 1):
                if 0 < edges[i][0] < lin and 0 < edges[i][1] < col:
                    if 0 < edges[i + 1][0] < lin and 0 < edges[i + 1][1] < col:
                        cv2.line(final, (edges[i][0], edges[i][1]),
                                 (edges[i + 1][0], edges[i + 1][1]),
                                 color=(255, 0, 0))

            cv2.line(final,
                     (edges[len(edges) - 1][0], edges[len(edges) - 1][1]),
                     (edges[0][0], edges[0][1]),
                     color=(255, 0, 0))

            text = 'frame={}, x={}, y={}'.format(number_frame, center[0],
                                                 center[1])
            cv2.putText(final, text, (25, 25), cv2.FONT_HERSHEY_SIMPLEX, 1,
                        (0, 0, 0), 2, cv2.LINE_AA)

            number_frame += 1
            original_out = r'{}\original_{}.png'.format(
                self.dataset_out_exam, number_frame)
            final_out = r'{}\final_{}.png'.format(self.dataset_out_exam,
                                                  number_frame)
            presentation = cv2.hconcat([original, final, threshold])

            cv2.namedWindow('Analysis', cv2.WINDOW_NORMAL)
            cv2.imshow('Analysis', presentation)
            cv2.waitKey(1)

            cv2.imwrite(original_out, final)
            cv2.imwrite(final_out, final)

            self._add_label("{},{},{}".format(number_frame, center[0],
                                              center[1]))

        exam.release()
        cv2.destroyAllWindows

    def run(self):
        files = os.listdir(self.dataset_path)
        for file in files:
            self.title = file.replace('.mp4', '')
            self._add_label('frame,x,y')

            self.dataset_out_exam = r'{}\{}'.format(self.dataset_out,
                                                    self.title)
            self._make_dir(self.dataset_out_exam)

            exam = cv2.VideoCapture('{}/{}'.format(self.dataset_path, file))
            self._pupil_process(exam)