hytos / DTI_PID / WebServer / app / recognition / index.py @ b751b562
이력 | 보기 | 이력해설 | 다운로드 (4.05 KB)
1 |
from flask import Flask, Blueprint, jsonify, request, render_template |
---|---|
2 |
import cv2 |
3 |
import numpy as np |
4 |
import sys, os |
5 |
import json, base64 |
6 |
from PIL import Image |
7 |
|
8 |
# craft
|
9 |
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + '\\..\\..\\CRAFT_pytorch_master') |
10 |
# service streamer
|
11 |
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + '\\..\\..\\service_streamer_master') |
12 |
# deep ocr
|
13 |
# sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + '\\deep_text_recognition_benchmark_master')
|
14 |
# symbol
|
15 |
sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + '\\..\\..\\symbol_recognition') |
16 |
|
17 |
recognition_service = Blueprint('recognition', __name__, url_prefix='/recognition') |
18 |
|
19 |
|
20 |
@recognition_service.route('/', methods=['GET']) |
21 |
def index(): |
22 |
return render_template("/recognition/index.html") |
23 |
|
24 |
|
25 |
def recognition(params=None): |
26 |
if params[0][0] == 'Text Area': |
27 |
res = text_craft.get_text_box_batch(params[0][1]) |
28 |
return [res]
|
29 |
elif params[0] == 'Symbol': |
30 |
pass
|
31 |
|
32 |
|
33 |
try:
|
34 |
# from model import get_prediction, batch_prediction
|
35 |
import text_craft |
36 |
import test_doftech_all_images |
37 |
from service_streamer import ThreadedStreamer |
38 |
|
39 |
# for error at 3.8
|
40 |
# import ctypes
|
41 |
# ctypes.cdll.LoadLibrary('caffe2_nvrtc.dll')
|
42 |
|
43 |
streamer = ThreadedStreamer(recognition, batch_size=64)
|
44 |
except ImportError as ex: |
45 |
ex |
46 |
pass
|
47 |
|
48 |
|
49 |
@recognition_service.route('/symbol_box', methods=['POST']) |
50 |
def symbol_box(): |
51 |
if request.method == 'POST': |
52 |
r = request |
53 |
nparr = np.fromstring(r.data, np.uint8) |
54 |
|
55 |
imgs = [] |
56 |
imgs.append(Image.fromarray(cv2.imdecode(nparr, cv2.IMREAD_COLOR))) |
57 |
|
58 |
boxes = test_doftech_all_images.get_symbol(imgs, trained_model1=os.path.dirname( |
59 |
os.path.realpath(__file__)) + '\\..\\..\\symbol_recognition\\MODEL\\f_doftech_all_class_only_params.pth', \
|
60 |
trained_model2=os.path.dirname(os.path.realpath( |
61 |
__file__)) + '\\..\\..\\symbol_recognition\\MODEL\\doftech_all_class_only_params_opc.pth')
|
62 |
|
63 |
return jsonify({'symbol_box': boxes[0]}) |
64 |
|
65 |
|
66 |
@recognition_service.route('/text_box', methods=['POST']) |
67 |
def text_box(): |
68 |
if request.method == 'POST': |
69 |
r = request |
70 |
nparr = np.fromstring(r.data, np.uint8) |
71 |
|
72 |
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR) |
73 |
# img = img.reshape(1, -1)
|
74 |
|
75 |
boxes = text_craft.get_text_box(img, img_path=None, score_path=None, trained_model=os.path.dirname( |
76 |
os.path.realpath(__file__)) + '\\CRAFT_pytorch_master\\weights\\craft_mlt_25k.pth')
|
77 |
|
78 |
return jsonify({'text_box': boxes}) |
79 |
|
80 |
|
81 |
@recognition_service.route('/stream_text_box', methods=['POST']) |
82 |
def stream_text_box(): |
83 |
if request.method == 'POST': |
84 |
r = request |
85 |
str_imgs = json.loads(r.data) |
86 |
imgs = [] |
87 |
for str_img in str_imgs: |
88 |
str_img = base64.b64decode(str_img) |
89 |
nparr = np.fromstring(str_img, np.uint8) |
90 |
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR) |
91 |
imgs.append(img) |
92 |
|
93 |
boxes_list = [] |
94 |
'''
|
95 |
for img in imgs:
|
96 |
# faster
|
97 |
#boxes = streamer.predict([[img, None, None, os.path.dirname(os.path.realpath(__file__)) + '\\CRAFT_pytorch_master\\weights\\craft_ic15_20k.pth']])
|
98 |
|
99 |
# More accurate
|
100 |
boxes = streamer.predict([[img, None, None, os.path.dirname(os.path.realpath(__file__)) + '\\CRAFT_pytorch_master\\weights\\craft_mlt_25k.pth']])
|
101 |
boxes_list.append(boxes[0])
|
102 |
'''
|
103 |
|
104 |
'''
|
105 |
infos = []
|
106 |
for img in imgs:
|
107 |
infos.append([img, None, None, os.path.dirname(os.path.realpath(__file__)) + '\\CRAFT_pytorch_master\\weights\\craft_mlt_25k.pth'])
|
108 |
boxes = streamer.predict(infos)
|
109 |
boxes_list = boxes
|
110 |
'''
|
111 |
|
112 |
infos = [['Text Area', [True, os.path.dirname( |
113 |
os.path.realpath(__file__)) + '\\..\\..\\CRAFT_pytorch_master\\weights\\craft_mlt_25k.pth', imgs]]]
|
114 |
boxes = streamer.predict(infos) |
115 |
boxes_list = boxes[0][0] |
116 |
|
117 |
return jsonify({'text_box_list': boxes_list}) |