File size: 3,493 Bytes
83edfed
4ea50ff
4ee6c86
 
8e8a354
b962d1a
12c0c35
 
83edfed
2aff18a
 
b962d1a
 
2aff18a
 
83edfed
b962d1a
 
 
 
 
e370097
 
 
b962d1a
 
5a18bf5
b962d1a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33fa924
b962d1a
 
 
 
 
 
 
 
 
 
33fa924
b962d1a
675da53
83edfed
675da53
 
e370097
675da53
 
 
 
8e8a354
48f48ad
8e8a354
 
 
675da53
8e8a354
675da53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b962d1a
 
 
da9a957
b962d1a
da9a957
d83a405
675da53
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import os
os.system('pip install --upgrade --no-cache-dir gdown')
os.system('gdown -O ./model_ctw.pth 16qgtD4UOhp0q5e2RYXE1dvuTz_ylZMyb')
#os.system('unzip model_ctw.zip')
os.system('gdown -O ./workdir.zip 10HxLehcJMY9rLd_OyH40HmrySZItuNDt')
os.system('unzip workdir.zip')
os.system('pip install "git+https://github.com/philferriere/cocoapi.git#egg=pycocotools&subdirectory=PythonAPI"')
os.system('python setup.py build develop --user')

import cv2
import pandas as pd
import gradio as gr

from det_demo import DetDemo
from maskrcnn_benchmark.config import cfg

from demo import get_model, preprocess, postprocess, load
from utils import Config, Logger, CharsetMapper
import torch


def infer(img):
    filepath = './input.png'
    img.save(filepath)
    config = Config('configs/rec/train_abinet.yaml')
    config.model_vision_checkpoint = None
    model = get_model(config)
    model = load(model, 'workdir/train-abinet/best-train-abinet.pth')
    charset = CharsetMapper(filename=config.dataset_charset_path, max_length=config.dataset_max_length + 1)
    
    cfg.merge_from_file('./configs/det/r50_baseline.yaml')
    # manual override some options
    cfg.merge_from_list(["MODEL.DEVICE", "cpu"])

    det_demo = DetDemo(
        cfg,
        min_image_size=800,
        confidence_threshold=0.7,
        output_polygon=True
    )
    
    image = cv2.imread(filepath)
    print(image.shape)
    result_polygons, result_masks, result_boxes = det_demo.run_on_opencv_image(image)
    
    patchs = [image[box[1]:box[3], box[0]:box[2], :] for box in result_boxes]
    patchs = [preprocess(patch, config.dataset_image_width, config.dataset_image_height) for patch in patchs]
    patchs = torch.cat(patchs, dim=0)
    res = model(patchs)
    result_words = postprocess(res, charset, 'alignment')[0]
    
    visual_image = det_demo.visualization(image.copy(), result_polygons, result_masks, result_boxes, result_words)
    
    print(visual_image.shape)
    cv2.imwrite('result.jpg', visual_image)
    return ['result.jpg', pd.DataFrame(result_words)]

blocks = gr.Blocks()

input_image = gr.Image(label="image", type="pil")
output_image = gr.Image(label="out_img", type="filepath")
output_word = gr.Dataframe(label="out_word", headers=['word'])

with blocks:
    gr.Markdown('''
    <center><h1 id="title">张博强毕设展示</h1></center>
    <center> 西北工业大学 航海学院本科 张博强 </center>
    <center> 毕设题目:自然场景中任意形状文字的检测与识别 </center>
    <center> 检测:基于<a href="https://github.com/wangyuxin87/ContourNet">ContourNet</a> 识别:基于<a href="https://github.com/FangShancheng/ABINet">ABINet</a> </center>
    ''')
    
    with gr.Row():
        with gr.Column():
            input_image.render()
            button = gr.Button("Submit")
            button.click(fn=infer, inputs=[input_image],
                         outputs=[output_image, output_word],)
        with gr.Column():
            output_image.render()
    with gr.Row():
        output_word.render()
    
    
if __name__ == "__main__":
    blocks.launch(debug=True)
'''
iface = gr.Interface(
    fn=infer,
    title="张博强毕设展示",
    description=description,
    inputs=[gr.inputs.Image(label="image", type="filepath")],
    outputs=[gr.outputs.Image(), gr.outputs.Dataframe(headers=['word'])],
    examples=['figs/test/CANDY.png', 'figs/test/ESPLANADE.png', 'figs/test/KAPPA.png'],
).launch(enable_queue=True, cache_examples=True)
'''