hytos / DTI_PID / WebServer / app.py @ bd43d199
이력 | 보기 | 이력해설 | 다운로드 (2.79 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 |
# 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 Flask' |
33 |
|
34 |
@app.route('/text_box', methods=['POST']) |
35 |
def text_box(): |
36 |
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 |
'''
|
61 |
for img in imgs:
|
62 |
# 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 |
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 |
'''
|
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 |
infos = [[None, None, os.path.dirname(os.path.realpath(__file__)) + '\\CRAFT_pytorch_master\\weights\\craft_mlt_25k.pth', imgs]] |
79 |
boxes = streamer.predict(infos) |
80 |
boxes_list = boxes[0]
|
81 |
|
82 |
return jsonify({'text_box_list': boxes_list}) |
83 |
|
84 |
if __name__ == '__main__': |
85 |
app.run(debug=False)
|
86 |
#app.run(host='0,0,0,0')
|