# Install dependencies import os os.system('python -m mim install mmocr') os.system('python -m mim install "mmcv==2.0.0rc4"') os.system('python -m mim install mmengine') os.system('python -m mim install "mmdet>=3.0.0rc5"') os.system('cd mmocr-dev-1.x/') os.system('pip install -v -e .') os.system('cd ..') import cv2 import argparse import gradio as gr import numpy as np # MMOCR from mmocr.apis.inferencers import MMOCRInferencer def arg_parse(): parser = argparse.ArgumentParser(description='MMOCR demo for gradio app') parser.add_argument( '--rec_config', type=str, default='mmocr-dev-1.x/configs/textrecog/maerec/maerec_b_union14m.py', help='The recognition config file.') parser.add_argument( '--rec_weight', type=str, default= 'maerec_b_union14m.pth', help='The recognition weight file.') parser.add_argument( '--det_config', type=str, default= 'mmocr-dev-1.x/configs/textdet/dbnetpp/dbnetpp_resnet50-oclip_fpnc_1200e_icdar2015.py', # noqa, help='The detection config file.') parser.add_argument( '--det_weight', type=str, default='dbnetpp.pth', help='The detection weight file.') parser.add_argument( '--device', type=str, default='cpu', help='The device used for inference.') args = parser.parse_args() return args def run_mmocr(img: np.ndarray, use_detector: bool = True): """Run MMOCR and SAM Args: img (np.ndarray): Input image use_detector (bool, optional): Whether to use detector. Defaults to True. """ if use_detector: mode = 'det_rec' else: mode = 'rec' # Build MMOCR mmocr_inferencer.mode = mode result = mmocr_inferencer(img, return_vis=True) visualization = result['visualization'][0] result = result['predictions'][0] if mode == 'det_rec': rec_texts = result['rec_texts'] det_polygons = result['det_polygons'] det_results = [] for rec_text, det_polygon in zip(rec_texts, det_polygons): det_polygon = np.array(det_polygon).astype(np.int32).tolist() det_results.append(f'{rec_text}: {det_polygon}') out_results = '\n'.join(det_results) visualization = cv2.cvtColor( np.array(visualization), cv2.COLOR_RGB2BGR) else: rec_text = result['rec_texts'][0] rec_score = result['rec_scores'][0] out_results = f'pred: {rec_text} \n score: {rec_score:.2f}' visualization = None return visualization, out_results if __name__ == '__main__': args = arg_parse() mmocr_inferencer = MMOCRInferencer( args.det_config, args.det_weight, args.rec_config, args.rec_weight, device=args.device) with gr.Blocks() as demo: with gr.Row(): with gr.Column(scale=1): gr.HTML("""