Spaces:
Runtime error
Runtime error
Commit ·
335ccf7
1
Parent(s): 2d6f0ce
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
os.system("pip install --upgrade pip")
|
| 3 |
+
os.system("pip install torch torchvision")
|
| 4 |
+
os.system("pip install numpy==1.21.0 mmcv-full openmim requests")
|
| 5 |
+
os.system("mim install mmdet")
|
| 6 |
+
os.system('git clone https://github.com/open-mmlab/mmocr.git')
|
| 7 |
+
os.system('pip install -r mmocr/requirements.txt')
|
| 8 |
+
os.system('pip install -v -e mmocr')
|
| 9 |
+
os.system('export PYTHONPATH=$(pwd)/mmocr:$PYTHONPATH')
|
| 10 |
+
os.system('pip freeze')
|
| 11 |
+
import sys
|
| 12 |
+
import requests
|
| 13 |
+
sys.path.append('./mmocr')
|
| 14 |
+
from gradio import inputs,outputs,Interface
|
| 15 |
+
from PIL import Image
|
| 16 |
+
from mmocr.utils.ocr import MMOCR
|
| 17 |
+
|
| 18 |
+
# load image examples
|
| 19 |
+
urls = ['https://i0.wp.com/www.kento.com.my/wp-content/uploads/2020/09/ikhsan-sports-shop-3D-stainless-steel-kento.jpg', 'https://automology.s3.ap-southeast-1.amazonaws.com/wp-content/uploads/2019/08/12224246/P.-Mampos.jpg']
|
| 20 |
+
for idx, url in enumerate(urls):
|
| 21 |
+
print(url)
|
| 22 |
+
image = Image.open(requests.get(url, stream=True).raw)
|
| 23 |
+
image.save(f"image_{idx}.png")
|
| 24 |
+
det = ["DB_r18","DB_r50","DRRG","FCE_IC15","MaskRCNN_CTW","MaskRCNN_IC15","MaskRCNN_IC17","PS_CTW","PS_IC15","TextSnake"]
|
| 25 |
+
rec = ["CRNN","SAR","NRTR_1/16-1/8","NRTR_1/8-1/4","RobustScanner","SATRN","SATRN_sm","ABINet","SEG","CRNN_TPS"]
|
| 26 |
+
# [MMOCR(config_dir="mmocr/configs/",det=i,recog=None,device="cpu") for i in det]
|
| 27 |
+
# [MMOCR(config_dir="mmocr/configs/",det=None,recog=i,device="cpu") for i in rec]
|
| 28 |
+
|
| 29 |
+
def process_image(detection_model_name,recognizer_model_name,image_path):
|
| 30 |
+
ocr = MMOCR(config_dir="mmocr/configs/",det=detection_model_name,recog=recognizer_model_name,device="cpu")
|
| 31 |
+
results = ocr.readtext(image_path,output='demo/det_out.jpg')
|
| 32 |
+
img = Image.open("demo/det_out.jpg")
|
| 33 |
+
return [img,results[0]["text"]]
|
| 34 |
+
title = "Interactive demo: MMOCR"
|
| 35 |
+
description = "OpenMMLab Text Detection, Recognition and Understanding Toolbox"
|
| 36 |
+
article = "Github repo: https://github.com/open-mmlab/mmocr"
|
| 37 |
+
examples =[["TextSnake","RobustScanner",f"image_{i}.png"] for i in range(len(urls))]
|
| 38 |
+
#css = """.output_image, .input_image {height: 600px !important}"""
|
| 39 |
+
iface = Interface(fn=process_image,
|
| 40 |
+
inputs=[
|
| 41 |
+
inputs.Dropdown(det,type="value"),
|
| 42 |
+
inputs.Dropdown(rec,type="value"),
|
| 43 |
+
inputs.Image(type="filepath")
|
| 44 |
+
],
|
| 45 |
+
outputs=[outputs.Image(type="pil"),outputs.Textbox()],
|
| 46 |
+
title=title,
|
| 47 |
+
description=description,
|
| 48 |
+
article=article,
|
| 49 |
+
examples=examples
|
| 50 |
+
)
|
| 51 |
+
iface.launch(debug=False)
|