Example #1
0
 def test_read_three_lines(self):
     reader = FileReader(
         os.path.dirname(__file__) + "/data/file_3lines.txt")
     self.assertEqual("abc", reader.read_line())
     self.assertEqual("def", reader.read_line())
     self.assertEqual("123", reader.read_line())
     self.assertIsNone(reader.read_line())
Example #2
0
    def __init__(self, parent=None, extension='.png'):
        super().__init__(parent)
        self.mode = utils.MODE_TRACK
        self.tracker_name = 'default'
        self.trackers = []
        self.current_class = 'default'
        self.current_color_index = 0
        self.class_colors = {}

        self.prev_index_frame = 0
        self.current_index_frame = 0

        self.folder_reader = FolderReader()

        self.image_directory = './imagen'
        self.extension = extension
        self.file_saver = FileSaver(self.image_directory)
        self.file_reader = FileReader()

        # self.filenames = ['out2.png', 'out19.png']
        self.filenames = utils.read_files(self.image_directory, self.extension)
        self.filenames = utils.sort_files(self.filenames)

        # Timer for run button
        self.run_timer = QTimer()
Example #3
0
    def __init__(self, width=1280, height=720, parent=None):
        QLabel.__init__(self, parent)
        self.xpos = []
        self.ypos = []
        self.widths = []
        self.heights = []
        self.rect_centers = []
        self.color_indices = []
        self.recent_draw = []  # Flag to indicate if bbox was created now.
        self.image_width = width
        self.image_height = height

        self.image = QPixmap(self.image_width, self.image_height)
        self.original_size = self.image.size()
        self.scaled_image = QPixmap()
        self.original_scaled_image = self.scaled_image.copy()
        self.scaled_image_to_draw = self.scaled_image.copy()
        self.setFixedSize(self.scaled_image.size())

        self.current_color_index = 0

        self.update_pixmap(self.image)

        self.drawing = False
        self.last_point = None

        self.file_reader = FileReader()
Example #4
0
File: main.py Project: bram1et/sdku
    info.preprocessing_start = time.time()
    info.preprocessing_done = time.time()
    if len(sys.argv) < 4:
        print("Not enough parameters specified. Quitting...")
        quit()
    else:
        input_info = InputInformation(sys.argv)

    try:
        input_file = open(input_info.input_file, "r")
        output_file = open(input_info.output_file, "w")
    except:
        print("Error opening file. Quitting...")
        quit()

    file_reader = FileReader(input_file)
    file_writer = FileWriter(output_file)
    N, P, Q, = file_reader.get_params()
    board = file_reader.get_board()
    sudoku_board = SudokuSolver(N, P, Q, board, input_info.tokens)
    info.search_start = time.time()
    sudoku_board.start_time = info.search_start
    sudoku_board.time_out_limit = float(input_info.timeout_limit)

    if False:
        sudoku_board.heap_test()
    else:
        if input_info.tokens["LCV"]:
            solved_board = sudoku_board.solve_board_value_heap(False)
        else:
            solved_board = sudoku_board.solve_board_heap(False)

from src.file_reader import FileReader
from src.sudoku_generator import SudokuBoardGenerator

from src.file_writer import FileWriter

if __name__ == '__main__':
    if len(sys.argv) != 3:
        print('Must specify input file and output file only')
        quit()

    else:
        input_file_str = sys.argv[1]
        output_file_str = sys.argv[2]

    try:
        input_file = open(input_file_str, 'r')
        output_file = open(output_file_str, 'w')
    except:
        print('Error opening file')
        quit()

    fileReader = FileReader(input_file)
    fileWriter = FileWriter(output_file)
    M, N, P, Q = fileReader.get_params_generator()

    sudoku_board =  SudokuBoardGenerator(M, N, P, Q)
    sudoku_board.generate_board()
    board_as_lists = sudoku_board.convert_board_to_list()
    fileWriter.write_generated_board_to_file(N, P, Q, board_as_lists)
Example #6
0
class Controller(QObject):

    change_color_index = pyqtSignal(int)
    request_bboxes = pyqtSignal()
    request_and_init_bboxes = pyqtSignal()
    remove_rectangle_signal = pyqtSignal(int)
    update_filename = pyqtSignal(str)
    rectangles_signal = pyqtSignal(list, list, list, list, list, list)
    update_image_folder = pyqtSignal(str)

    def __init__(self, parent=None, extension='.png'):
        super().__init__(parent)
        self.mode = utils.MODE_TRACK
        self.tracker_name = 'default'
        self.trackers = []
        self.current_class = 'default'
        self.current_color_index = 0
        self.class_colors = {}

        self.prev_index_frame = 0
        self.current_index_frame = 0

        self.folder_reader = FolderReader()

        self.image_directory = './imagen'
        self.extension = extension
        self.file_saver = FileSaver(self.image_directory)
        self.file_reader = FileReader()

        # self.filenames = ['out2.png', 'out19.png']
        self.filenames = utils.read_files(self.image_directory, self.extension)
        self.filenames = utils.sort_files(self.filenames)

        # Timer for run button
        self.run_timer = QTimer()


    @pyqtSlot(list, list, list, list, list, list)
    def process_rectangles(self, xs, ys, widths, heights, color_indices, recent_draw):
        self.save_rectangles(self.get_prev_filename(), xs, ys, widths, heights, color_indices)
        if self.mode == utils.MODE_TRACK:
            xs, ys, widths, heights, color_indices, recent_draw = self.update_trackers(recent_draw, xs, ys, widths, heights, color_indices)
            recent_draw = [False for i in recent_draw]
            self.rectangles_signal.emit(xs, ys, widths, heights, color_indices, recent_draw)
        elif self.mode == utils.MODE_RTRACK:
            self.send_saved_bboxes()
        elif self.mode == utils.MODE_COPYBBOX:
            recent_draws = [True for i in recent_draw]
            self.rectangles_signal.emit(xs, ys, widths, heights, color_indices, recent_draws)
        elif self.mode == utils.MODE_EMPTY:
            pass
        elif self.mode == utils.MODE_NOTHING:
            self.send_saved_bboxes()

    @pyqtSlot(int)
    def remove_rectangle_slot(self, index):
        self.remove_rectangle(index)

    def remove_rectangle(self, index):
        if index < len(self.trackers):
            self.trackers.pop(index)

    def update_trackers(self, recent_draws, xs, ys, widths, heights, color_indices):
        xs_out = []
        ys_out = []
        widths_out = []
        heights_out = []
        color_indices_out = []
        curr_draws = []
        trackers_to_remove = []
        prev_image = cv2.imread(self.get_prev_filepath() + self.extension)
        current_image = cv2.imread(self.get_current_filepath() + self.extension)
        for index, (recent_draw, x, y, w, h, color_idx) in enumerate(zip(recent_draws, xs, ys, widths, heights, color_indices)):
            if recent_draw:
                tracker = create_tracker(self.tracker_name)
                tracker.init(prev_image, (x, y, w, h))
                self.trackers.append(tracker)
                ret, bbox = tracker.update(prev_image)
            ret, bbox = self.trackers[index].update(current_image)
            if ret:
                xs_out.append(bbox[0])
                ys_out.append(bbox[1])
                widths_out.append(bbox[2])
                heights_out.append(bbox[3])
                color_indices_out.append(color_idx)
                curr_draws.append(True)
            else:
                trackers_to_remove.append(index)

        for index in trackers_to_remove[::-1]:
            self.remove_rectangle_signal.emit(index)

        return xs_out, ys_out, widths_out, heights_out, color_indices_out, curr_draws

    def send_saved_bboxes(self):
        xs, ys, widths, heights, color_indices = self.file_reader.read_bboxes(self.get_current_filepath())
        self.rectangles_signal.emit(xs, ys, widths, heights, color_indices, [False for i in color_indices])

    def save_rectangles(self, filename, xs, ys, widths, heights, color_indices):
        image = cv2.imread(self.get_prev_filepath() + self.extension)
        h, w = image.shape[:2]
        c = 1
        if len(image.shape) > 2:
            c = image.shape[2]
        self.file_saver.save_bboxes(filename, xs, ys, widths, heights, color_indices, w, h, c)

    @pyqtSlot(str)
    def set_tracker_name(self, tracker_name):
        self.tracker_name = tracker_name

        # Try 1
        for index in range(len(self.trackers))[::-1]:
            self.remove_rectangle_signal.emit(index)
        self.send_saved_bboxes()
        self.request_and_init_bboxes.emit()
        # Try 2
        # self.request_and_init_bboxes.emit()

    @pyqtSlot(str)
    def set_current_class(self, class_name):
        self.current_class = class_name
        self.current_color_index = self.class_colors[self.current_class]
        self.change_color_index.emit(self.current_color_index)

    def update_mode(self, mode):
        self.mode = mode

    @pyqtSlot()
    def update_mode_to_track(self):
        self.update_mode(utils.MODE_TRACK)
        self.set_tracker_name(self.tracker_name)

    @pyqtSlot()
    def update_mode_to_rtrack(self):
        self.update_mode(utils.MODE_RTRACK)

    @pyqtSlot()
    def update_mode_to_copybbox(self):
        self.update_mode(utils.MODE_COPYBBOX)

    @pyqtSlot()
    def update_mode_to_empty(self):
        self.update_mode(utils.MODE_EMPTY)

    @pyqtSlot()
    def update_mode_to_nothing(self):
        self.update_mode(utils.MODE_NOTHING)

    def set_classes(self, items):
        self.class_colors = {color: index for index, color in enumerate(items)}

    @pyqtSlot()
    def request_next(self):
        if self.current_index_frame < len(self.filenames) - 1:
            self.prev_index_frame = self.current_index_frame
            self.current_index_frame += 1
            self.update_filename.emit(self.get_current_frame())
            self.request_bboxes.emit()

    @pyqtSlot()
    def request_prev(self):
        if self.current_index_frame > 0:
            self.prev_index_frame = self.current_index_frame
            self.current_index_frame -= 1
            self.update_filename.emit(self.get_current_frame())
            self.request_bboxes.emit()

    def get_current_frame(self):
        if self.current_index_frame < 0 or self.current_index_frame >= len(self.filenames):
            return None
        else:
            path = os.path.join(self.image_directory, self.filenames[self.current_index_frame])
            return path

    def get_current_filename(self):
        return self.filenames[self.current_index_frame]

    def get_prev_filename(self):
        return self.filenames[self.prev_index_frame]

    def get_current_filepath(self):
        return os.path.join(self.image_directory, self.get_current_filename())

    def get_prev_filepath(self):
        return os.path.join(self.image_directory, self.get_prev_filename())

    def select_folder(self):
        folder = self.folder_reader.get_folder()
        self.image_directory = folder
        self.file_saver.set_folder(folder)
        self.filenames = utils.read_files(self.image_directory, self.extension)
        self.filenames = utils.sort_files(self.filenames)
        self.prev_index_frame = 0
        self.current_index_frame = 0

        for index in range(len(self.trackers))[::-1]:
            self.remove_rectangle_signal.emit(index)

        self.update_filename.emit(self.get_current_frame())
        self.send_saved_bboxes()
        self.request_and_init_bboxes.emit()

        self.update_image_folder.emit(folder)
        print('NEWFOLDER', folder)


    @pyqtSlot()
    def run_tracking(self):
        self.run_timer.start(50)

    @pyqtSlot()
    def stop_tracking(self):
        self.run_timer.stop()