from mmocr.ocr import MMOCR import gradio as gr import cv2 model_dir = 'model' ocr = MMOCR(det_config=f'{model_dir}/config.py', det_ckpt=f'{model_dir}/epoch_40.pth', device='cpu') def get_rec(points): xs = [] ys = [] for ix, iv in enumerate(points): if ix % 2: ys.append(iv) else: xs.append(iv) return (min(xs), min(ys)), (max(xs), max(ys)) def predict(image_input): draw_img = image_input.copy() print(image_input.shape) output = ocr.readtext(image_input) for polygon in output['det_polygons']: p0, p1 = get_rec([int(i) for i in polygon]) draw_img = cv2.rectangle(draw_img, p0, p1, (255,0,0), 2) return draw_img def run(): demo = gr.Interface( fn=predict, inputs=gr.components.Image(), outputs=gr.components.Image(), ) demo.launch(server_name="0.0.0.0", server_port=7860) if __name__ == "__main__": run()