hytos / DTI_PID / DTI_PID / AppWebService.py @ 26cdd6c1
이력 | 보기 | 이력해설 | 다운로드 (4.5 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 |
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, img): |
52 |
# send uncroped image
|
53 |
try:
|
54 |
if not self.test_connection(): |
55 |
return []
|
56 | |
57 |
symbol_box = '/recognition/symbol_box'
|
58 | |
59 |
_, bts = cv2.imencode('.png', img)
|
60 |
bts = bts.tostring() |
61 | |
62 |
response = requests.post(self._url + symbol_box, data=bts)
|
63 | |
64 |
return response.json()['symbol_box'] # |
65 |
except Exception as ex: |
66 |
from App import App |
67 |
from AppDocData import MessageType |
68 |
message = 'error occurred({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, |
69 |
sys.exc_info()[-1].tb_lineno)
|
70 |
App.mainWnd().addMessage.emit(MessageType.Error, message) |
71 |
return []
|
72 | |
73 |
def request_text_box(self, img): |
74 |
# send uncroped image, test code
|
75 |
try:
|
76 |
if not self.test_connection(): |
77 |
return []
|
78 | |
79 |
text_box = '/recognition/text_box'
|
80 | |
81 |
_, bts = cv2.imencode('.png', img)
|
82 |
bts = bts.tostring() |
83 | |
84 |
response = requests.post(self._url + text_box, data=bts)
|
85 | |
86 |
return response.json()['text_box'] |
87 |
except Exception as ex: |
88 |
from App import App |
89 |
from AppDocData import MessageType |
90 |
message = 'error occurred({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, |
91 |
sys.exc_info()[-1].tb_lineno)
|
92 |
App.mainWnd().addMessage.emit(MessageType.Error, message) |
93 |
return []
|
94 | |
95 |
def request_text_box_tile(self, img_infos): |
96 |
""" main code """
|
97 |
try:
|
98 |
if not self.test_connection(): |
99 |
return []
|
100 | |
101 |
text_box = '/recognition/stream_text_box'
|
102 | |
103 |
imgs = [info[4] for info in img_infos] |
104 |
str_imgs = [] |
105 |
for img in imgs: |
106 |
_, bts = cv2.imencode('.png', img)
|
107 |
# bts = bts.tostring()
|
108 |
bts = base64.b64encode(bts).decode() |
109 |
str_imgs.append(bts) |
110 | |
111 |
response = requests.post(self._url + text_box, data=json.dumps(str_imgs))
|
112 |
box_list = response.json()['text_box_list']
|
113 | |
114 |
# for debug
|
115 |
if len(img_infos) != len(box_list): |
116 |
print('check return values')
|
117 |
return
|
118 | |
119 |
for index in range(len(img_infos)): |
120 |
img_infos[index].append(box_list[index]) |
121 | |
122 |
return img_infos
|
123 |
except Exception as ex: |
124 |
from App import App |
125 |
from AppDocData import MessageType |
126 |
message = 'error occurred({}) in {}:{}'.format(ex, sys.exc_info()[-1].tb_frame.f_code.co_filename, |
127 |
sys.exc_info()[-1].tb_lineno)
|
128 |
App.mainWnd().addMessage.emit(MessageType.Error, message) |
129 |
return []
|
130 | |
131 |
'''
|
132 |
def request(self, url):
|
133 |
response = urllib.request.urlopen(url)
|
134 |
byte_data = response.read()
|
135 |
text_data = byte_data.decode('utf-8')
|
136 |
return text_data
|
137 |
'''
|