-
Notifications
You must be signed in to change notification settings - Fork 2
/
Service.py
103 lines (90 loc) · 3.31 KB
/
Service.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import socket
import threading
import json
import base64
import time
import multiprocessing
import cv2
import numpy as np
from Interface import meterReader
class meterReaderService:
"""TCP service"""
def __requestHandler(self, clientSocket, clientAddress):
"""
:param clientSocket: client socket
:param clientAddress: client address
:return:
"""
print('Accept new connection from %s:%s...' % clientAddress)
# collect buffer
receiveData = ""
bufferSize = 2048
while True:
buffer = clientSocket.recv(bufferSize)
# when client close socket, not the end of data
if not buffer:
break
receiveData += buffer.decode("utf-8")
# when
if len(buffer) != bufferSize and receiveData[-1] == '}':
break
# feed data to interface
data = json.loads(receiveData)
meterIDs = data["meterIDs"]
imageByte = data["image"].encode("ascii")
imageByte = base64.b64decode(imageByte)
imageArray = np.asarray(bytearray(imageByte), dtype = "uint8")
image = cv2.imdecode(imageArray, cv2.IMREAD_COLOR)
result = meterReader(image, meterIDs)
sendData = json.dumps(result).encode("utf-8")
clientSocket.send(sendData)
print("Result is sent to client!")
clientSocket.close()
print('Close new connection from %s:%s...' % clientAddress)
def startServer(self, port=9999, maxConnection=5):
"""
start service
:param port: port
:param maxConnection: max connection at a time
"""
self.serverSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.serverSocket.bind(("127.0.0.1", port))
self.serverSocket.listen(maxConnection)
print("Binding meter reader service to port %d." % port)
print("Waiting for connection...")
# listen
while True:
clientSocket, clientAddress = self.serverSocket.accept()
# create a new thread th handle connection
thread = threading.Thread(target = self.__requestHandler, args = (clientSocket, clientAddress))
thread.start()
def startClient(self, port = 9999):
"""
test service
:param port: port
"""
self.clientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.clientSocket.connect(("127.0.0.1", port))
# image to byte
image = open("image/2018-11-20-16-22-02.jpg", "rb")
imageByte = base64.b64encode(image.read())
data = {
"image": imageByte.decode("ascii"),
"meterIDs": [
"1_1"
]
}
sendData = json.dumps(data).encode("utf-8")
self.clientSocket.send(sendData)
print("data is sent to server!")
print(self.clientSocket.recv(1024).decode("utf-8"))
self.clientSocket.close()
if __name__ == "__main__":
test = meterReaderService()
# # serverProcess = multiprocessing.Process(target = test.startServer)
# # clientProcess = multiprocessing.Process(target=test.startClient)
# # serverProcess.start()
# # time.sleep(3)
# # clientProcess.start()
image = cv2.imread("image/bileiqi1.JPG")
print(meterReader(image, ["bileiqi1_1"]))