hytos / DTI_PID / DTI_PID / AppWebService.py @ a02da871
이력 | 보기 | 이력해설 | 다운로드 (7.8 KB)
1 |
# coding: utf-8
|
---|---|
2 |
""" This is AppWebService module """
|
3 |
|
4 |
# import urllib.request
|
5 |
# import urllib.parse
|
6 |
import json |
7 |
import requests |
8 |
import cv2 |
9 |
import sys, os |
10 |
import base64 |
11 |
import numpy as np |
12 |
from AppDocData import AppDocData |
13 |
from PyQt5 import QtCore, QtGui, QtWidgets |
14 |
from PyQt5.QtWidgets import * |
15 |
|
16 |
class AppWebService: |
17 |
""" This is AppDatabase class """
|
18 |
|
19 |
def __init__(self, url=None): |
20 |
if not url: |
21 |
configs = AppDocData.instance().getConfigs('Engine', 'Address') |
22 |
self._url = configs[0].value if configs else None |
23 |
else:
|
24 |
self._url = url
|
25 |
#self._url = 'http://192.168.0.21:80'
|
26 |
|
27 |
def test_connection(self): |
28 |
try:
|
29 |
if not self._url: |
30 |
return False |
31 |
url = self._url + '/Test_Connection' |
32 |
response = requests.get(url) |
33 |
if response.status_code == 200: |
34 |
return True |
35 |
else:
|
36 |
mb = QMessageBox() |
37 |
mb.setIcon(QMessageBox.Critical) |
38 |
mb.setWindowTitle('Error')
|
39 |
mb.setText('status code({}) : {}'.format(str(response.status_code), response.text)) |
40 |
mb.exec_() |
41 |
return False |
42 |
|
43 |
except Exception as ex:#requests.exceptions.RequestException as ex: |
44 |
mb = QMessageBox() |
45 |
mb.setIcon(QMessageBox.Critical) |
46 |
mb.setWindowTitle('Error')
|
47 |
mb.setText('{}'.format(ex))
|
48 |
mb.exec_() |
49 |
return False |
50 |
|
51 |
def request_symbol_box(self, name, img): |
52 |
# send uncroped image
|
53 |
try:
|
54 |
if not self.test_connection(): |
55 |
return []
|
56 |
|
57 |
data = { 'name':name, 'img':None } |
58 |
symbol_box = '/recognition/symbol_box'
|
59 |
|
60 |
_, bts = cv2.imencode('.png', img)
|
61 |
bts = base64.b64encode(bts).decode() |
62 |
data['img'] = bts
|
63 |
|
64 |
response = requests.post(self._url + symbol_box, data=json.dumps(data))
|
65 |
|
66 |
return response.json()['symbol_box'] |
67 |
except Exception as ex: |
68 |
from App import App |
69 |
from AppDocData import MessageType |
70 |
message = 'error occurred({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, |
71 |
sys.exc_info()[-1].tb_lineno)
|
72 |
App.mainWnd().addMessage.emit(MessageType.Error, message) |
73 |
return []
|
74 |
|
75 |
def request_text_box(self, img): |
76 |
# send uncroped image, test code
|
77 |
try:
|
78 |
if not self.test_connection(): |
79 |
return []
|
80 |
|
81 |
text_box = '/recognition/text_box'
|
82 |
|
83 |
_, bts = cv2.imencode('.png', img)
|
84 |
bts = bts.tostring() |
85 |
|
86 |
response = requests.post(self._url + text_box, data=bts)
|
87 |
|
88 |
return response.json()['text_box'] |
89 |
except Exception as ex: |
90 |
from App import App |
91 |
from AppDocData import MessageType |
92 |
message = 'error occurred({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, |
93 |
sys.exc_info()[-1].tb_lineno)
|
94 |
App.mainWnd().addMessage.emit(MessageType.Error, message) |
95 |
return []
|
96 |
|
97 |
def request_ocr_gcloud(self, img): |
98 |
# send uncroped image
|
99 |
try:
|
100 |
if not self.test_connection(): |
101 |
return []
|
102 |
|
103 |
text_box = '/recognition/ocr'
|
104 |
|
105 |
_, bts = cv2.imencode('.png', img)
|
106 |
bts = bts.tostring() |
107 |
|
108 |
response = requests.post(self._url + text_box, data=bts)
|
109 |
|
110 |
return response.json()['text_box'] |
111 |
except Exception as ex: |
112 |
from App import App |
113 |
from AppDocData import MessageType |
114 |
message = 'error occurred({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, |
115 |
sys.exc_info()[-1].tb_lineno)
|
116 |
App.mainWnd().addMessage.emit(MessageType.Error, message) |
117 |
return []
|
118 |
|
119 |
def request_text_box_tile(self, img_infos): |
120 |
""" main code """
|
121 |
try:
|
122 |
if not self.test_connection(): |
123 |
return []
|
124 |
|
125 |
text_box = '/recognition/stream_text_box'
|
126 |
|
127 |
imgs = [info[4] for info in img_infos] |
128 |
str_imgs = [] |
129 |
for img in imgs: |
130 |
_, bts = cv2.imencode('.png', img)
|
131 |
# bts = bts.tostring()
|
132 |
bts = base64.b64encode(bts).decode() |
133 |
str_imgs.append(bts) |
134 |
|
135 |
response = requests.post(self._url + text_box, data=json.dumps(str_imgs))
|
136 |
box_list = response.json()['text_box_list']
|
137 |
|
138 |
# for debug
|
139 |
if len(img_infos) != len(box_list): |
140 |
print('check return values')
|
141 |
return
|
142 |
|
143 |
for index in range(len(img_infos)): |
144 |
img_infos[index].append(box_list[index]) |
145 |
|
146 |
return img_infos
|
147 |
except Exception as ex: |
148 |
from App import App |
149 |
from AppDocData import MessageType |
150 |
message = 'error occurred({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, |
151 |
sys.exc_info()[-1].tb_lineno)
|
152 |
App.mainWnd().addMessage.emit(MessageType.Error, message) |
153 |
return []
|
154 |
|
155 |
def request_upload_training_data(self, project_name, tiles, xmls) -> bool: |
156 |
""" main code """
|
157 |
try:
|
158 |
if not self.test_connection(): |
159 |
return []
|
160 |
|
161 |
upload_data = '/training/upload_training_data'
|
162 |
|
163 |
data = {'name': project_name, 'count': len(tiles) + len(xmls), 'tiles': [], 'xmls': []} |
164 |
|
165 |
for tile in tiles: |
166 |
with open(tile.encode('utf-8'), 'rb') as stream: |
167 |
_bytes = stream.read() |
168 |
numpyArray = np.asarray(bytearray(_bytes), dtype=np.uint8)
|
169 |
|
170 |
image = cv2.imdecode(numpyArray, cv2.IMREAD_UNCHANGED) |
171 |
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) |
172 |
|
173 |
_, bts = cv2.imencode('.png', image)
|
174 |
bts = base64.b64encode(bts).decode() |
175 |
data['tiles'].append((os.path.basename(tile) , bts))
|
176 |
|
177 |
for xml in xmls: |
178 |
with open(xml.encode('utf-8'), 'r') as stream: |
179 |
rects = stream.read() |
180 |
#bts = base64.b64encode(rects).decode()
|
181 |
data['xmls'].append((os.path.basename(xml), rects))
|
182 |
|
183 |
response = requests.post(self._url + upload_data, data=json.dumps(data))
|
184 |
count = response.json()['count']
|
185 |
|
186 |
return data['count'] == count |
187 |
except Exception as ex: |
188 |
from App import App |
189 |
from AppDocData import MessageType |
190 |
message = 'error occurred({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, |
191 |
sys.exc_info()[-1].tb_lineno)
|
192 |
App.mainWnd().addMessage.emit(MessageType.Error, message) |
193 |
|
194 |
return False |
195 |
|
196 |
def request_execute_training(self, name, classes, bigs): |
197 |
"""request symbol training on server"""
|
198 |
try:
|
199 |
if not self.test_connection(): |
200 |
return []
|
201 |
|
202 |
training_model = '/training/training_model'
|
203 |
|
204 |
data = {'name': name, 'classes': classes, 'bigs': bigs} |
205 |
|
206 |
response = requests.post(self._url + training_model, data=json.dumps(data))
|
207 |
count = response.json()['count']
|
208 |
|
209 |
return count == 1 |
210 |
except Exception as ex: |
211 |
from App import App |
212 |
from AppDocData import MessageType |
213 |
message = 'error occurred({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, |
214 |
sys.exc_info()[-1].tb_lineno)
|
215 |
App.mainWnd().addMessage.emit(MessageType.Error, message) |
216 |
|
217 |
return False |