File size: 2,236 Bytes
2366e36
 
 
 
 
4377988
2366e36
fe4aa9b
2366e36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import os
import torch

print(torch.__version__)
torch_ver, cuda_ver = torch.__version__.split('+')
os.system(f'pip install pycocotools==2.0.0 mmdet mmcv-full==1.5.0 -f https://download.openmmlab.com/mmcv/dist/{cuda_ver}/torch1.10.0/index.html --no-cache-dir')
os.system('wget -c https://download.openmmlab.com/mmocr/data/wildreceipt.tar; mkdir -p data; tar -xf wildreceipt.tar --directory data; rm -f wildreceipt.tar')
os.system(f'pip install pycocotools==2.0.0 mmdet mmcv-full==1.5.0 -f https://download.openmmlab.com/mmcv/dist/{cuda_ver}/torch1.10.0/index.html --no-cache-dir')

import datetime
import gradio as gr
import pandas as pd
from mmocr.utils.ocr import MMOCR

def inference(img):
    print(datetime.datetime.now(), 'start')
    ocr = MMOCR(det='PS_CTW', recog='SAR', kie='SDMGR')
    print(datetime.datetime.now(), 'start read')
    results = ocr.readtext(img.name, details=True, output='result.png')
    print(datetime.datetime.now(), results)
    return ['result.png', pd.DataFrame(results[0]['result']).iloc[: , 2:]]

description = 'Gradio demo for MMOCR. MMOCR is an open-source toolbox based on PyTorch and mmdetection for text detection, text recognition, and the corresponding downstream tasks including key information extraction. To use it, simply upload your image or click one of the examples to load them. Read more at the links below.'
article = "<p style='text-align: center'><a href='https://mmocr.readthedocs.io/en/latest/'>MMOCR is an open-source toolbox based on PyTorch and mmdetection for text detection, text recognition, and the corresponding downstream tasks including key information extraction.</a> | <a href='https://github.com/open-mmlab/mmocr'>Github Repo</a></p>"
gr.Interface(inference,
    gr.inputs.Image(type='file', label='Input'),
    [gr.outputs.Image(type='file', label='Output'), gr.outputs.Dataframe(headers=['text', 'text_score', 'label', 'label_score'])],
    title='MMOCR',
    description=description,
    article=article,
    examples=['demo/demo_kie.jpeg', 'demo/demo_text_ocr.jpg', 'demo/demo_text_det.jpg', 'demo/demo_densetext_det.jpg'],
    css=".output_image, .input_image {height: 40rem !important; width: 100% !important;}",
    enable_queue=True
    ).launch(debug=True)