# coding: utf-8 # Copyright (C) 2023, [Breezedeus](https://github.com/breezedeus). # Licensed to the Apache Software Foundation (ASF) under one # or more contributor license agreements. See the NOTICE file # distributed with this work for additional information # regarding copyright ownership. The ASF licenses this file # to you under the Apache License, Version 2.0 (the # "License"); you may not use this file except in compliance # with the License. You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, # software distributed under the License is distributed on an # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY # KIND, either express or implied. See the License for the # specific language governing permissions and limitations # under the License. # Ref: https://huggingface.co/spaces/hysts/Manga-OCR/blob/main/app.py import os import json import functools import random import string import time import yaml import gradio as gr import numpy as np # from cnstd.utils import pil_to_numpy, imsave from pix2text import Pix2Text from pix2text.utils import set_logger, merge_line_texts logger = set_logger() LANGUAGES = yaml.safe_load(open('languages.yaml', 'r', encoding='utf-8'))['languages'] def get_p2t_model(lan_list: list): p2t = Pix2Text(languages=lan_list) return p2t def latex_render(latex_str): return f"$$\n{latex_str}\n$$" # return latex_str def recognize(lang_list, rec_type, resized_shape, image_file): lang_list = [LANGUAGES[l] for l in lang_list] p2t = get_p2t_model(lang_list) if rec_type == 'Formula & Text': suffix = list(string.ascii_letters) random.shuffle(suffix) suffix = ''.join(suffix[:6]) out_det_fp = f'out-det-{time.time()}-{suffix}.jpg' outs = p2t( image_file, resized_shape=resized_shape, save_analysis_res=out_det_fp ) # To get just the text contents, use: only_text = merge_line_texts(outs, auto_line_break=True) # return only_text, latex_render(only_text) return only_text, out_det_fp elif rec_type == 'Only Formula': only_text = p2t.recognize_formula(image_file) return latex_render(only_text), None elif rec_type == 'Only Text': only_text = p2t.recognize_text(image_file) return only_text, None def main(): langs = list(LANGUAGES.keys()) langs.sort(key=lambda x: x.lower()) title = 'Demo' # example_func = functools.partial( # recognize, # new_size=768, # box_score_thresh=0.3, # min_box_size=10, # ) # examples = [ # [ # 'ch_PP-OCRv3_det::onnx', # True, # 'number-densenet_lite_136-fc', # False, # 'docs/examples/card1-s.jpg', # ], # [ # 'ch_PP-OCRv3_det::onnx', # True, # 'number-densenet_lite_136-fc', # False, # 'docs/examples/card2-s.jpg', # ], # [ # 'ch_PP-OCRv3_det::onnx', # True, # 'number-densenet_lite_136-fc', # False, # 'docs/examples/cy1-s.jpg', # ], # [ # 'ch_PP-OCRv3_det::onnx', # False, # 'densenet_lite_136-gru', # False, # 'docs/examples/huochepiao.jpeg', # ], # [ # 'ch_PP-OCRv3_det::onnx', # False, # 'densenet_lite_136-gru', # False, # 'docs/examples/1_res.jpg', # ], # [ # 'db_shufflenet_v2::pytorch', # False, # 'en_number_mobile_v2.0', # False, # 'docs/examples/en_book1.jpeg', # ], # [ # 'db_shufflenet_v2::pytorch', # False, # 'densenet_lite_136-gru', # True, # 'docs/examples/beauty0.jpg', # ], # ] table_desc = """