프로젝트

일반

사용자정보

통계
| 개정판:

hytos / DTI_PID / WebServer / app.py @ f0cfc15f

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

1
from flask import Flask, jsonify, request
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
    streamer = ThreadedStreamer(text_craft.get_text_box_batch, batch_size=64)
22
except ImportError as ex:
23
    ex
24
    pass
25

    
26
@app.route('/')
27
def index():
28
    return 'Hello Flask'
29
    
30
@app.route('/text_box', methods=['POST'])
31
def text_box():
32
    if request.method == 'POST':
33
        r = request
34
        nparr = np.fromstring(r.data, np.uint8)
35

    
36
        img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
37
        #img = img.reshape(1, -1)
38

    
39
        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')
40

    
41
        return jsonify({'text_box': boxes})
42

    
43
@app.route('/stream_text_box', methods=['POST'])
44
def stream_text_box():
45
    if request.method == 'POST':
46
        r = request
47
        str_imgs = json.loads(r.data)
48
        imgs = []
49
        for str_img in str_imgs:
50
            str_img = base64.b64decode(str_img)
51
            nparr = np.fromstring(str_img, np.uint8)
52
            img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
53
            imgs.append(img)
54

    
55
        boxes_list = []
56
        '''
57
        for img in imgs:
58
            # faster
59
            #boxes = streamer.predict([[img, None, None, os.path.dirname(os.path.realpath(__file__)) + '\\CRAFT_pytorch_master\\weights\\craft_ic15_20k.pth']])
60

61
            # More accurate
62
            boxes = streamer.predict([[img, None, None, os.path.dirname(os.path.realpath(__file__)) + '\\CRAFT_pytorch_master\\weights\\craft_mlt_25k.pth']])
63
            boxes_list.append(boxes[0])
64
        '''
65

    
66
        '''
67
        infos = []
68
        for img in imgs:
69
            infos.append([img, None, None, os.path.dirname(os.path.realpath(__file__)) + '\\CRAFT_pytorch_master\\weights\\craft_mlt_25k.pth'])
70
        boxes = streamer.predict(infos)
71
        boxes_list = boxes
72
        '''
73

    
74
        infos = [[None, None, os.path.dirname(os.path.realpath(__file__)) + '\\CRAFT_pytorch_master\\weights\\craft_mlt_25k.pth', imgs]]
75
        boxes = streamer.predict(infos)
76
        boxes_list = boxes[0]
77

    
78
        return jsonify({'text_box_list': boxes_list})
79

    
80
if __name__ == '__main__':
81
    app.run(debug=True)
클립보드 이미지 추가 (최대 크기: 500 MB)