프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

hytos / DTI_PID / WebServer / app.py @ ed7877e8

이력 | 보기 | 이력해설 | 다운로드 (4.18 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
def recognition(params=None):
17
    if params[0] == 'Text Area':
18
        res = text_craft.get_text_box_batch(params[1:])
19
        return ['Text Area', res]
20
    elif params[0] == 'Symbol':
21
        pass
22

    
23
try:
24
    #from model import get_prediction, batch_prediction
25
    import text_craft
26
    from service_streamer import ThreadedStreamer
27

    
28
    # for error at 3.8
29
    #import ctypes
30
    #ctypes.cdll.LoadLibrary('caffe2_nvrtc.dll')
31

    
32
    streamer = ThreadedStreamer(recognition, batch_size=64)
33
except ImportError as ex:
34
    ex
35
    pass
36

    
37
@app.route('/')
38
def index():
39
    return 'Hello ID2'
40

    
41
@app.route('/requset_license_key/')
42
def requset_license_key():
43
    return render_template('license.html')
44

    
45
@app.route('/gen_key', methods=['POST'])
46
def gen_key():
47
    import base64
48

    
49
    if request.method == 'POST':
50
        r = request
51

    
52
        key = 'Image Drawing to Intelligent Drawing'
53

    
54
        pw = r.form['Authorization']
55
        clear = r.form['Computer_Name']
56

    
57
        if pw != 'admin':
58
            return 'Invalid Authorization'
59

    
60
        enc = []
61
        for i in range(len(clear)):
62
            key_c = key[i % len(key)]
63
            enc_c = (ord(clear[i]) + ord(key_c)) % 256
64
            enc.append(enc_c)
65

    
66
        new_key = base64.urlsafe_b64encode(bytes(enc))
67

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

    
70
@app.route('/symbol_box', methods=['POST'])
71
def symbol_box():
72
    if request.method == 'POST':
73
        r = request
74
        nparr = np.fromstring(r.data, np.uint8)
75

    
76
        img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
77

    
78
        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')
79

    
80
        return jsonify({'symbol_box': boxes})
81

    
82
    
83
@app.route('/text_box', methods=['POST'])
84
def text_box():
85
    if request.method == 'POST':
86
        r = request
87
        nparr = np.fromstring(r.data, np.uint8)
88

    
89
        img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
90
        #img = img.reshape(1, -1)
91

    
92
        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')
93

    
94
        return jsonify({'text_box': boxes})
95

    
96
@app.route('/stream_text_box', methods=['POST'])
97
def stream_text_box():
98
    if request.method == 'POST':
99
        r = request
100
        str_imgs = json.loads(r.data)
101
        imgs = []
102
        for str_img in str_imgs:
103
            str_img = base64.b64decode(str_img)
104
            nparr = np.fromstring(str_img, np.uint8)
105
            img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
106
            imgs.append(img)
107

    
108
        boxes_list = []
109
        '''
110
        for img in imgs:
111
            # faster
112
            #boxes = streamer.predict([[img, None, None, os.path.dirname(os.path.realpath(__file__)) + '\\CRAFT_pytorch_master\\weights\\craft_ic15_20k.pth']])
113

114
            # More accurate
115
            boxes = streamer.predict([[img, None, None, os.path.dirname(os.path.realpath(__file__)) + '\\CRAFT_pytorch_master\\weights\\craft_mlt_25k.pth']])
116
            boxes_list.append(boxes[0])
117
        '''
118

    
119
        '''
120
        infos = []
121
        for img in imgs:
122
            infos.append([img, None, None, os.path.dirname(os.path.realpath(__file__)) + '\\CRAFT_pytorch_master\\weights\\craft_mlt_25k.pth'])
123
        boxes = streamer.predict(infos)
124
        boxes_list = boxes
125
        '''
126

    
127
        infos = ['Text Area', [None, None, os.path.dirname(os.path.realpath(__file__)) + '\\CRAFT_pytorch_master\\weights\\craft_mlt_25k.pth', imgs]]
128
        boxes = streamer.predict(infos)
129
        boxes_list = boxes[1][0]
130

    
131
        return jsonify({'text_box_list': boxes_list})
132

    
133
if __name__ == '__main__':
134
    app.run(debug=False)
135
    #app.run(host='0,0,0,0')
클립보드 이미지 추가 (최대 크기: 500 MB)