hytos / DTI_PID / WebServer / app / recognition / index.py @ 6c9a3ed5
이력 | 보기 | 이력해설 | 다운로드 (4.07 KB)
1 | 78a9d434 | humkyung | 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 | 2e744c88 | esham21 | sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + '\\..\\..\\CRAFT_pytorch_master') |
10 | 78a9d434 | humkyung | # service streamer
|
11 | 2e744c88 | esham21 | sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + '\\..\\..\\service_streamer_master') |
12 | 78a9d434 | humkyung | # deep ocr
|
13 | # sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + '\\deep_text_recognition_benchmark_master')
|
||
14 | # symbol
|
||
15 | 2e744c88 | esham21 | sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)) + '\\..\\..\\symbol_recognition') |
16 | 78a9d434 | humkyung | |
17 | recognition_service = Blueprint('recognition', __name__, url_prefix='/recognition') |
||
18 | |||
19 | |||
20 | @recognition_service.route('/', methods=['GET']) |
||
21 | def index(): |
||
22 | 5f6cb499 | humkyung | return render_template("/recognition/index.html") |
23 | |||
24 | 78a9d434 | humkyung | |
25 | def recognition(params=None): |
||
26 | fa83a646 | esham21 | if params[0][0] == 'Text Area': |
27 | res = text_craft.get_text_box_batch(params[0][1]) |
||
28 | return [res]
|
||
29 | 78a9d434 | humkyung | 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 | edcd2d38 | esham21 | data = json.loads(r.data) |
54 | 78a9d434 | humkyung | |
55 | edcd2d38 | esham21 | str_img = base64.b64decode(data['img'])
|
56 | nparr = np.fromstring(str_img, np.uint8) |
||
57 | 78a9d434 | humkyung | imgs = [] |
58 | imgs.append(Image.fromarray(cv2.imdecode(nparr, cv2.IMREAD_COLOR))) |
||
59 | |||
60 | edcd2d38 | esham21 | data_path = os.path.join(os.path.dirname(os.path.realpath(__file__)) + '\\..\\..\\symbol_training\\Data\\', data['name']) |
61 | 6374c2c6 | esham21 | |
62 | db5c22f1 | esham21 | boxes = test_doftech_all_images.get_symbol(imgs, data['name'], root_path=data_path, trained_model1=os.path.join(data_path, 'checkpoint', data['name'] + "_only_params.pth"), \ |
63 | 6c9a3ed5 | esham21 | trained_model2=None, save=False) |
64 | 78a9d434 | humkyung | |
65 | return jsonify({'symbol_box': boxes[0]}) |
||
66 | |||
67 | |||
68 | @recognition_service.route('/text_box', methods=['POST']) |
||
69 | def text_box(): |
||
70 | if request.method == 'POST': |
||
71 | r = request |
||
72 | nparr = np.fromstring(r.data, np.uint8) |
||
73 | |||
74 | img = cv2.imdecode(nparr, cv2.IMREAD_COLOR) |
||
75 | # img = img.reshape(1, -1)
|
||
76 | |||
77 | boxes = text_craft.get_text_box(img, img_path=None, score_path=None, trained_model=os.path.dirname( |
||
78 | os.path.realpath(__file__)) + '\\CRAFT_pytorch_master\\weights\\craft_mlt_25k.pth')
|
||
79 | |||
80 | return jsonify({'text_box': boxes}) |
||
81 | |||
82 | |||
83 | @recognition_service.route('/stream_text_box', methods=['POST']) |
||
84 | def stream_text_box(): |
||
85 | if request.method == 'POST': |
||
86 | r = request |
||
87 | str_imgs = json.loads(r.data) |
||
88 | imgs = [] |
||
89 | for str_img in str_imgs: |
||
90 | str_img = base64.b64decode(str_img) |
||
91 | nparr = np.fromstring(str_img, np.uint8) |
||
92 | img = cv2.imdecode(nparr, cv2.IMREAD_COLOR) |
||
93 | imgs.append(img) |
||
94 | |||
95 | boxes_list = [] |
||
96 | '''
|
||
97 | for img in imgs:
|
||
98 | # faster
|
||
99 | #boxes = streamer.predict([[img, None, None, os.path.dirname(os.path.realpath(__file__)) + '\\CRAFT_pytorch_master\\weights\\craft_ic15_20k.pth']])
|
||
100 |
|
||
101 | # More accurate
|
||
102 | boxes = streamer.predict([[img, None, None, os.path.dirname(os.path.realpath(__file__)) + '\\CRAFT_pytorch_master\\weights\\craft_mlt_25k.pth']])
|
||
103 | boxes_list.append(boxes[0])
|
||
104 | '''
|
||
105 | |||
106 | '''
|
||
107 | infos = []
|
||
108 | for img in imgs:
|
||
109 | infos.append([img, None, None, os.path.dirname(os.path.realpath(__file__)) + '\\CRAFT_pytorch_master\\weights\\craft_mlt_25k.pth'])
|
||
110 | boxes = streamer.predict(infos)
|
||
111 | boxes_list = boxes
|
||
112 | '''
|
||
113 | |||
114 | 02bb6172 | esham21 | infos = [['Text Area', [False, os.path.dirname(os.path.realpath(__file__)) + '\\..\\..\\CRAFT_pytorch_master\\weights\\craft_mlt_25k.pth', imgs]]] |
115 | 78a9d434 | humkyung | boxes = streamer.predict(infos) |
116 | fa83a646 | esham21 | boxes_list = boxes[0][0] |
117 | 78a9d434 | humkyung | |
118 | 2e744c88 | esham21 | return jsonify({'text_box_list': boxes_list}) |