예제 #1
0
    def detect_morphological(
        self, images: List[np.ndarray]
    ) -> Generator[List[List[List[List[int]]]], None, None]:

        for img in images:

            image_data = ImageData()
            image_data.image = img.astype(float) / 255
            gray = image_data.image.copy()

            if np.sum(np.histogram(gray)[0][1:-2]) != 0:
                gray = enhance(image_data.image)

            binary = binarize(gray)
            binarized = 1 - binary
            morph = binary_erosion(binarized, structure=np.full((5, 1), 1))
            morph = binary_dilation(morph, structure=np.full((5, 1), 1))
            staffs = (binarized ^ morph)
            image_data.staff_space_height, image_data.staff_line_height = vertical_runs(
                binary)
            image_data.binary_image = binary
            self.callback.update_total_state()
            image_data.horizontal_runs_img = calculate_horizontal_runs(
                (1 - staffs), self.settings.horizontal_min_length)
            self.callback.update_total_state()
            yield self.detect_staff_lines(image_data)
            self.callback.update_total_state()
            self.callback.update_page_counter()
예제 #2
0
def create_data(image: np.ndarray, line_space_height: int) -> ImageData:
    norm_img = image.astype(np.float32) / 255
    binary_image = gauss_threshold(image.astype(np.uint8)) / 255
    if line_space_height == 0:
        staff_space_height, staff_line_height = vertical_runs(binary_image)
        space_height = staff_space_height + staff_line_height
    else:
        space_height = line_space_height
        staff_line_height = max(1, int(np.round(0.2 * line_space_height)))
        staff_space_height = space_height - staff_line_height

    image_data = ImageData(height=space_height,
                           image=norm_img,
                           staff_line_height=staff_line_height,
                           staff_space_height=staff_space_height,
                           binary_image=binary_image)
    return image_data
예제 #3
0
    def detect_fcn(
        self, images: List[np.ndarray]
    ) -> Generator[List[List[List[List[int]]]], None, None]:
        self.callback.set_total_pages(len(images))
        create_data_partial = partial(
            create_data, line_space_height=self.settings.line_space_height)
        if len(images) <= 1:
            data = list(map(create_data_partial, images))
        else:
            with multiprocessing.Pool(processes=self.settings.processes) as p:
                data = [
                    v for v in tqdm.tqdm(p.imap(create_data_partial, images),
                                         total=len(images))
                ]
        for i, prob in enumerate(self.predictor.predict(data)):
            prob = prob
            if self.settings.model_use_argmax:
                pred = np.argmax(prob, axis=-1)
            else:
                prob = prob[:, :, 1]
                norm = prob / np.max(
                    prob) if self.settings.model_foreground_normalize else prob
                pred = (norm > self.settings.model_foreground_threshold)
            self.callback.update_total_state()
            if data[i].staff_space_height is None or data[
                    i].staff_line_height is None:
                data[i].staff_space_height, data[
                    i].staff_line_height = vertical_runs(data[i].binary_image)
            data[i].horizontal_runs_img = calculate_horizontal_runs(
                1 - pred, self.settings.horizontal_min_length)
            data[i].pixel_classifier_prediction = prob

            self.callback.update_total_state()
            if self.settings.debug_model:
                f, ax = plt.subplots(2, 3, sharex='all', sharey='all')
                ax[0, 0].imshow(1 - prob, cmap='gray')
                ax[0, 1].imshow(1 - pred, cmap='gray')
                ax[0, 2].imshow(255 - data[i].horizontal_runs_img, cmap='gray')
                ax[1, 0].imshow(data[i].image, cmap='gray')
                plt.show()
            yield self.detect_staff_lines(data[i])
            self.callback.update_total_state()
            self.callback.update_page_counter()
예제 #4
0
    def detect_advanced(
        self, images: List[np.ndarray]
    ) -> Generator[List[List[List[List[int]]]], None, None]:
        create_data_partial = partial(
            create_data, line_space_height=self.settings.line_space_height)
        with multiprocessing.Pool(processes=self.settings.processes) as p:
            data = [
                v for v in tqdm.tqdm(p.imap(create_data_partial, images),
                                     total=len(images))
            ]
        self.callback.set_total_pages(len(images))

        if self.text_predictor:
            for i, pred in enumerate(
                    zip(self.predictor.predict(data),
                        self.text_predictor.predict(data))):
                line_prediction = pred[0]
                region_prediction = pred[1]
                t_region = np.clip(pred[1], 0, 1) * 255
                line_prediction[line_prediction > 0] = 255
                region_prediction[region_prediction < 255] = 0
                line_prediction_pruned = line_prediction * region_prediction * 255
                self.callback.update_total_state()

                if self.settings.debug:
                    x, y = plt.subplots(1, 3, sharex=True, sharey=True)
                    y[0].imshow(region_prediction)
                    y[1].imshow(line_prediction)
                    y[2].imshow(line_prediction_pruned)
                    plt.show()
                self.callback.update_total_state()

                data[i].staff_space_height, data[
                    i].staff_line_height = vertical_runs(
                        1 - line_prediction_pruned)
                self.callback.update_total_state()

                data[i].horizontal_runs_img = calculate_horizontal_runs(
                    (1 - (line_prediction_pruned / 255)),
                    self.settings.horizontal_min_length)
                self.callback.update_total_state()
                yield self.detect_staff_lines_rest(
                    data[i], get_text_borders(t_region - region_prediction))
                self.callback.update_total_state()
                self.callback.update_page_counter()
        else:
            for i, prob in enumerate(self.predictor.predict(data)):
                background = prob[:, :, 0]
                drawn_lines = prob[:, :, 1]
                virtual_lines = prob[:, :, 2]

                #prob = pred.probabilities[:, :, 1]

                norm_vl = virtual_lines / np.max(virtual_lines) if self.settings.model_foreground_normalize else\
                    virtual_lines
                pred_vl = (norm_vl > self.settings.model_foreground_threshold)
                norm_dl = drawn_lines / np.max(
                    drawn_lines
                ) if self.settings.model_foreground_normalize else drawn_lines
                pred_dl = (norm_dl > self.settings.model_foreground_threshold)

                pred = pred_dl + pred_vl
                self.callback.update_total_state()

                data[i].staff_space_height, data[
                    i].staff_line_height = vertical_runs(1 - pred)
                print(vertical_runs(1 - pred))
                self.callback.update_total_state()
                data[i].horizontal_runs_img = calculate_horizontal_runs(
                    (1 - pred), self.settings.horizontal_min_length)
                plt.imshow(data[i].horizontal_runs_img)
                plt.show()
                self.callback.update_total_state()

                data[i].image = images[i] / 255
                binary = np.array(binarize(data[i].image), dtype='uint8')
                text_borders = get_text_borders((1 - binary) * 255,
                                                preprocess=True)
                self.callback.update_total_state()
                if self.settings.debug_model:
                    f, ax = plt.subplots(2, 3, sharex='all', sharey='all')
                    ax[0, 0].imshow(1 - prob, cmap='gray')
                    ax[0, 1].imshow(1 - pred, cmap='gray')
                    ax[0, 2].imshow(255 - data[i].horizontal_runs_img,
                                    cmap='gray')
                    ax[1, 0].imshow(data[i].image, cmap='gray')
                    plt.show()
                yield self.detect_staff_lines_rest(data[i], text_borders)
                self.callback.update_total_state()
                self.callback.update_page_counter()