示例#1
0
    def output_book_2(self, test_pcgts_files, predictions, all_tp_staves,
                      all_fp_staves, all_fn_staves):
        global_args = self.args.global_args
        output_tp = True
        output_fp = True
        output_fn = True
        if global_args.output_only_tp:
            output_fp = False
            output_fn = False

        if global_args.output_only_fp:
            output_tp = False
            output_fn = False

        logger.info("Outputting music lines to {}".format(
            global_args.output_book))
        assert (predictions is not None)
        pred_book = DatabaseBook(global_args.output_book)
        output_pcgts_by_page_name = {}
        for pcgts in test_pcgts_files:
            o_pcgts = PcGts.from_file(
                pred_book.page(pcgts.page.location.page).file('pcgts'))
            output_pcgts_by_page_name[pcgts.page.location.page] = o_pcgts
            o_pcgts.page.music_regions.clear()

        for p, tp_staves, fp_staves, fn_staves in zip(predictions,
                                                      all_tp_staves,
                                                      all_fp_staves,
                                                      all_fn_staves):
            o_pcgts = output_pcgts_by_page_name[
                p.line.operation.page.location.page]
            if output_tp:
                for ml, gt_ml in [(ml, gt_ml) for ml, gt_ml, _ in tp_staves
                                  if ml in p.music_lines]:
                    if global_args.output_symbols:
                        ml.symbols = gt_ml.symbols[:]
                    o_pcgts.page.music_regions.append(
                        Block(block_type=BlockType.MUSIC, lines=[ml]))

            if output_fp:
                for ml in [ml for ml in fp_staves if ml in p.music_lines]:
                    ml.symbols.clear()
                    o_pcgts.page.music_regions.append(
                        Block(block_type=BlockType.MUSIC, lines=[ml]))

            if output_fn:
                for gt_ml in fn_staves:
                    if not global_args.output_symbols:
                        gt_ml.symbols.clear()

                    o_pcgts.page.music_regions.append(
                        Block(block_type=BlockType.MUSIC, lines=[gt_ml]))

        for _, o_pcgts in output_pcgts_by_page_name.items():
            o_pcgts.to_file(o_pcgts.page.location.file('pcgts').local_path())
    def test_pages_with_lock(self):
        book = DatabaseBook('demo')
        pages = book.pages_with_lock([LockState(Locks.STAFF_LINES, True)])
        self.assertListEqual([p.local_path() for p in pages],
                             [book.page('page_test_lock').local_path()])

        pages = book.pages_with_lock([
            LockState(Locks.STAFF_LINES, False),
            LockState(Locks.SYMBOLS, True)
        ])
        self.assertListEqual([p.local_path() for p in pages], [])
    def test_single_line_001(self):
        try:
            book = DatabaseBook('demo')
            file = book.page("page_test_monodi_export_001")
            pcgts = file.pcgts()
            root = PcgtsToMeiConverter(pcgts)
            self.assertTrue(root.is_valid)

            # test to string and write
            root.to_string()
            buffer = BytesIO()
            root.write(buffer, pretty_print=True)

        except Exception as e:
            logging.exception(e)
            raise e
    def test_single_line_001(self):
        try:
            book = DatabaseBook('demo')
            file = book.page("page_test_monodi_export_001")
            pcgts = file.pcgts()
            root = PcgtsToMonodiConverter([pcgts]).root
            j = root.to_json()
            drop_all_attributes(j, 'uuid')

            with open(file.local_file_path('monodi.json'), 'r') as f:
                ref = json.load(f)

            self.maxDiff = None
            self.assertEqual(ref, j)
        except Exception as e:
            logging.exception(e)
            raise e
    ]

    text_line = TextLine(
        polys,
        TextBoundaries(np.column_stack((center_x, cap_top_line)),
                       np.column_stack((center_x, top_line)),
                       np.column_stack((center_x, bot_line)),
                       np.column_stack((center_x, cap_bot_line))))

    if debug:
        import matplotlib.pyplot as plt
        canvas = np.stack(((canvas).astype(np.uint8), ) * 3, -1)
        text_line.draw(canvas)
        plt.imshow(canvas)
        plt.show()

    return text_line


if __name__ == '__main__':
    from database import DatabaseBook
    import pickle
    book = DatabaseBook('test')
    page = book.page('Graduel_de_leglise_de_Nevers_536')
    with open(
            page.file('connected_components_deskewed',
                      create_if_not_existing=True).local_path(), 'rb') as f:
        cc = pickle.load(f)
    line = np.array([[100, 383], [900, 380]])
    extract_text(cc, line, debug=True)
示例#6
0
    @abstractmethod
    def _predict(self, pcgts_files: List[PcGts], callback: Optional[PredictionCallback] = None)\
            -> PredictionType:
        pass


if __name__ == "__main__":
    from database import DatabaseBook
    from PIL import Image
    import matplotlib.pyplot as plt
    import numpy as np
    from omr.steps.step import Step, AlgorithmTypes

    b = DatabaseBook('demo')
    p = b.page('page00000001')
    img = np.array(Image.open(p.file('color_norm').local_path()))
    mask = np.zeros(img.shape, np.float) + 255
    val_pcgts = [PcGts.from_file(p.file('pcgts'))]

    settings = PredictorSettings()
    pred = Step.create_predictor(AlgorithmTypes.LAYOUT_SIMPLE_BOUNDING_BOXES, settings)

    def s(c):
        return val_pcgts[0].page.page_to_image_scale(c, settings.page_scale_reference)

    for p in pred.predict(val_pcgts):
        for i, mr_c in enumerate(p.blocks.get(BlockType.MUSIC, [])):
            s(mr_c.coords).draw(mask, (255, 0, 0), fill=True, thickness=0)

        for i, mr_c in enumerate(p.blocks.get(BlockType.LYRICS, [])):