# copyright (c) 2024 PaddlePaddle Authors. All Rights Reserve. # # Licensed 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. import os import re import numpy as np from pathlib import Path from scipy.ndimage import rotate def get_ocr_res(pipeline, input): """get ocr res""" ocr_res_list = [] if isinstance(input, list): img = [im["img"] for im in input] elif isinstance(input, dict): img = input["img"] else: img = input for ocr_res in pipeline(img): ocr_res_list.append(ocr_res) if len(ocr_res_list) == 1: return ocr_res_list[0] else: return ocr_res_list def get_oriclas_results(inputs, predictor): results = [] img_list = [img_info["img"] for img_info in inputs] for input, pred in zip(inputs, predictor(img_list)): results.append(pred) angle = int(pred["label_names"][0]) input["img"] = rotate_image(input["img"], angle) return results def get_uvdoc_results(inputs, predictor): results = [] img_list = [img_info["img"] for img_info in inputs] for input, pred in zip(inputs, predictor(img_list)): results.append(pred) img = np.array(pred["doctr_img"], dtype=np.uint8) input["img"] = img return results def get_predictor_res(predictor, input): """get ocr res""" result_list = [] if isinstance(input, list): img = [im["img"] for im in input] elif isinstance(input, dict): img = input["img"] else: img = input for res in predictor(img): result_list.append(res) if len(result_list) == 1: return result_list[0] else: return result_list def rotate_image(image_array, rotate_angle): """rotate image""" assert ( rotate_angle >= 0 and rotate_angle < 360 ), "rotate_angle must in [0-360), but get {rotate_angle}." return rotate(image_array, rotate_angle, reshape=True) def get_table_text_from_html(all_table_res): all_table_ocr_res = [] structure_res = [] for table_res in all_table_res: table_list = [] table_lines = re.findall("