File size: 1,233 Bytes
0fc4b93
 
 
c3a10c9
0fc4b93
dc45c07
c32fbbb
78be22c
2dfefac
 
 
 
 
 
 
 
 
 
 
83c123b
2dfefac
c3a10c9
 
7831c58
 
 
 
c3a10c9
83c123b
 
 
 
 
2dfefac
f10784f
c3a10c9
2dfefac
0fc4b93
 
 
 
e7d3991
7b83b49
0fc4b93
 
 
 
 
 
 
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from mmocr.ocr import MMOCR
import gradio as gr
import cv2
import sys

ocr = MMOCR(det_config='model/config.py',
            det_ckpt='model/model.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, score_threshold):
    draw_img = image_input.copy()
    print('image shape', image_input.shape, file=sys.stderr)

    try:
        output = ocr.readtext(image_input)
    except:
        output = dict(det_polygons=[], det_scores=[])

    polygons = output['det_polygons']
    scores = output['det_scores']
    for polygon, score in zip(polygons, scores):
        if score < score_threshold:
            continue
        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(), gr.Slider(0, 1, 0.8)],
        outputs=gr.components.Image(),
    )

    demo.launch(server_name="0.0.0.0", server_port=7860)


if __name__ == "__main__":
    run()