프로젝트

일반

사용자정보

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

hytos / DTI_PID / WebServer / app.py @ bd43d199

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

1 60998ab9 esham21
from flask import Flask, jsonify, request
2
import cv2
3
import numpy as np
4
import sys, os
5 9bdbbda1 esham21
import json, base64
6 60998ab9 esham21
7 9bdbbda1 esham21
# craft
8 c64be242 esham21
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + '\\CRAFT_pytorch_master')
9 9bdbbda1 esham21
# service streamer
10
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + '\\service_streamer_master')
11
# deep ocr
12 4d2aa82f esham21
#sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + '\\deep_text_recognition_benchmark_master')
13 c64be242 esham21
14 60998ab9 esham21
app = Flask(__name__)
15
16 9bdbbda1 esham21
try:
17
    #from model import get_prediction, batch_prediction
18
    import text_craft
19
    from service_streamer import ThreadedStreamer
20
21 6e1bfbcb esham21
    # for error at 3.8
22
    import ctypes
23
    ctypes.cdll.LoadLibrary('caffe2_nvrtc.dll')
24
25 9bdbbda1 esham21
    streamer = ThreadedStreamer(text_craft.get_text_box_batch, batch_size=64)
26
except ImportError as ex:
27
    ex
28
    pass
29
30 60998ab9 esham21
@app.route('/')
31
def index():
32
    return 'Hello Flask'
33
    
34
@app.route('/text_box', methods=['POST'])
35
def text_box():
36 9bdbbda1 esham21
    if request.method == 'POST':
37
        r = request
38
        nparr = np.fromstring(r.data, np.uint8)
39
40
        img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
41
        #img = img.reshape(1, -1)
42
43
        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')
44
45
        return jsonify({'text_box': boxes})
46
47
@app.route('/stream_text_box', methods=['POST'])
48
def stream_text_box():
49
    if request.method == 'POST':
50
        r = request
51
        str_imgs = json.loads(r.data)
52
        imgs = []
53
        for str_img in str_imgs:
54
            str_img = base64.b64decode(str_img)
55
            nparr = np.fromstring(str_img, np.uint8)
56
            img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
57
            imgs.append(img)
58
59
        boxes_list = []
60 d8e1a15d esham21
        '''
61 9bdbbda1 esham21
        for img in imgs:
62 d8e1a15d esham21
            # faster
63
            #boxes = streamer.predict([[img, None, None, os.path.dirname(os.path.realpath(__file__)) + '\\CRAFT_pytorch_master\\weights\\craft_ic15_20k.pth']])
64

65
            # More accurate
66 9bdbbda1 esham21
            boxes = streamer.predict([[img, None, None, os.path.dirname(os.path.realpath(__file__)) + '\\CRAFT_pytorch_master\\weights\\craft_mlt_25k.pth']])
67
            boxes_list.append(boxes[0])
68 d8e1a15d esham21
        '''
69
70
        '''
71
        infos = []
72
        for img in imgs:
73
            infos.append([img, None, None, os.path.dirname(os.path.realpath(__file__)) + '\\CRAFT_pytorch_master\\weights\\craft_mlt_25k.pth'])
74
        boxes = streamer.predict(infos)
75
        boxes_list = boxes
76
        '''
77
78 f0cfc15f esham21
        infos = [[None, None, os.path.dirname(os.path.realpath(__file__)) + '\\CRAFT_pytorch_master\\weights\\craft_mlt_25k.pth', imgs]]
79 d8e1a15d esham21
        boxes = streamer.predict(infos)
80 f0cfc15f esham21
        boxes_list = boxes[0]
81 d8e1a15d esham21
82 9bdbbda1 esham21
        return jsonify({'text_box_list': boxes_list})
83 60998ab9 esham21
84
if __name__ == '__main__':
85 b9e6a7c5 esham21
    app.run(debug=False)
86
    #app.run(host='0,0,0,0')
클립보드 이미지 추가 (최대 크기: 500 MB)