프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / WebServer / app.py @ a91e2268

이력 | 보기 | 이력해설 | 다운로드 (3.98 KB)

1
from flask import Flask, jsonify, request, render_template
2
import cv2
3
import numpy as np
4
import sys, os
5
import json, base64
6

    
7
# craft
8
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + '\\CRAFT_pytorch_master')
9
# service streamer
10
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + '\\service_streamer_master')
11
# deep ocr
12
#sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + '\\deep_text_recognition_benchmark_master')
13

    
14
app = Flask(__name__)
15

    
16
try:
17
    #from model import get_prediction, batch_prediction
18
    import text_craft
19
    from service_streamer import ThreadedStreamer
20

    
21
    # for error at 3.8
22
    import ctypes
23
    ctypes.cdll.LoadLibrary('caffe2_nvrtc.dll')
24

    
25
    streamer = ThreadedStreamer(text_craft.get_text_box_batch, batch_size=64)
26
except ImportError as ex:
27
    ex
28
    pass
29

    
30
@app.route('/')
31
def index():
32
    return 'Hello ID2'
33

    
34
@app.route('/requset_license_key/')
35
def requset_license_key():
36
    return render_template('license.html')
37

    
38
@app.route('/gen_key', methods=['POST'])
39
def gen_key():
40
    import base64
41

    
42
    if request.method == 'POST':
43
        r = request
44

    
45
        key = 'Image Drawing to Intelligent Drawing'
46

    
47
        pw = r.form['Authorization']
48
        clear = r.form['Computer_Name']
49

    
50
        if pw != 'admin':
51
            return 'Invalid Authorization'
52

    
53
        enc = []
54
        for i in range(len(clear)):
55
            key_c = key[i % len(key)]
56
            enc_c = (ord(clear[i]) + ord(key_c)) % 256
57
            enc.append(enc_c)
58

    
59
        new_key = base64.urlsafe_b64encode(bytes(enc))
60

    
61
        return 'License Key for ' + clear + ' : ' + new_key.decode('utf-8')
62

    
63
@app.route('/symbol_box', methods=['POST'])
64
def symbol_box():
65
    if request.method == 'POST':
66
        r = request
67
        nparr = np.fromstring(r.data, np.uint8)
68

    
69
        img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
70

    
71
        boxes = text_craft.get_text_box(img, img_path=None, score_path=None, trained_model=os.path.dirname(os.path.realpath(__file__)) + '\\CRAFT_pytorch_master\\weights\\craft_mlt_25k.pth')
72

    
73
        return jsonify({'symbol_box': boxes})
74

    
75
    
76
@app.route('/text_box', methods=['POST'])
77
def text_box():
78
    if request.method == 'POST':
79
        r = request
80
        nparr = np.fromstring(r.data, np.uint8)
81

    
82
        img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
83
        #img = img.reshape(1, -1)
84

    
85
        boxes = text_craft.get_text_box(img, img_path=None, score_path=None, trained_model=os.path.dirname(os.path.realpath(__file__)) + '\\CRAFT_pytorch_master\\weights\\craft_mlt_25k.pth')
86

    
87
        return jsonify({'text_box': boxes})
88

    
89
@app.route('/stream_text_box', methods=['POST'])
90
def stream_text_box():
91
    if request.method == 'POST':
92
        r = request
93
        str_imgs = json.loads(r.data)
94
        imgs = []
95
        for str_img in str_imgs:
96
            str_img = base64.b64decode(str_img)
97
            nparr = np.fromstring(str_img, np.uint8)
98
            img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
99
            imgs.append(img)
100

    
101
        boxes_list = []
102
        '''
103
        for img in imgs:
104
            # faster
105
            #boxes = streamer.predict([[img, None, None, os.path.dirname(os.path.realpath(__file__)) + '\\CRAFT_pytorch_master\\weights\\craft_ic15_20k.pth']])
106

107
            # More accurate
108
            boxes = streamer.predict([[img, None, None, os.path.dirname(os.path.realpath(__file__)) + '\\CRAFT_pytorch_master\\weights\\craft_mlt_25k.pth']])
109
            boxes_list.append(boxes[0])
110
        '''
111

    
112
        '''
113
        infos = []
114
        for img in imgs:
115
            infos.append([img, None, None, os.path.dirname(os.path.realpath(__file__)) + '\\CRAFT_pytorch_master\\weights\\craft_mlt_25k.pth'])
116
        boxes = streamer.predict(infos)
117
        boxes_list = boxes
118
        '''
119

    
120
        infos = [[None, None, os.path.dirname(os.path.realpath(__file__)) + '\\CRAFT_pytorch_master\\weights\\craft_mlt_25k.pth', imgs]]
121
        boxes = streamer.predict(infos)
122
        boxes_list = boxes[0]
123

    
124
        return jsonify({'text_box_list': boxes_list})
125

    
126
if __name__ == '__main__':
127
    app.run(debug=False)
128
    #app.run(host='0,0,0,0')