def meterReaderAPI(): try: data = request.get_data().decode("utf-8") data = json.loads(data) imageID = data["imageID"] path = data["path"] # print(imageID) meterIDs = getMeterIDs(imageID) # imageByte = data["image"].encode("ascii") # imageByte = base64.b64decode(imageByte) # imageArray = np.asarray(bytearray(imageByte), dtype="uint8") # image = cv2.imdecode(imageArray, cv2.IMREAD_COLOR) # recognitionData = None # if path[-4:] != ".jpg": recognitionData = cv2.VideoCapture(path) else: recognitionData = cv2.imread(path) # print(path, np.shape(recognitionData)) except: return json.dumps({"error": "json format error!"}) else: result = meterReader(recognitionData, meterIDs) sendData = json.dumps(result).encode("utf-8") return sendData
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 testVideo(): video_path = "info/20190128/IMAGES/video_" for file in os.listdir(video_path): if file.startswith(".DS"): continue video = cv2.VideoCapture(os.path.join(video_path, file)) result = meterReader(video, [file[:-4] + "_1"]) print(file, result) print("codecov done")
def codecov(): images = os.listdir("image") config = os.listdir("config") for im in images: image = cv2.imread("image/" + im) print(im) for i in range(1, 6): cfg = im.split(".jpg")[0] + "_" + str(i) print(cfg) if cfg + ".json" in config: receive2 = meterReader(image, [cfg]) print("codecov done")
def testVideo(): #video_path = "info/20190128/IMAGES/video_" video_path = "info/20191207/image/video" for file in os.listdir(video_path): if file.startswith(".DS"): continue video = cv2.VideoCapture(os.path.join(video_path, file)) #todo 读取视频 start = time.clock() result = meterReader(video, [file[:-5] + "1_1"]) # todo 调用meterReader 接口 end = time.clock() print(file, result) print(end - start) print("codecov done")
def testReadyStatus(): imgPath = "info/20190416/IMAGES/image" configPath = "info/20190416/config" images = os.listdir(imgPath) config = os.listdir(configPath) for im in images: filename, extention = os.path.splitext(im.lower()) if extention == '.jpg' or extention == '.png': image = cv2.imread(imgPath + "/" + im) for i in range(1, 6): cfg = filename + "_" + str(i) if cfg + ".json" in config: receive2 = meterReader(image, [cfg]) print(cfg, receive2)
def codecov(imgPath): images = os.listdir(imgPath) config = os.listdir(configPath) for im in images: image = cv2.imread(imgPath + "/" + im) print(im) pos = im.split(".")[0].split("-") # cfg = im.split(".")[0]+"_1" for i in range(1, 6): cfg = pos[0] + "-" + pos[1] + "_" + str(i) if cfg + ".json" in config: receive2 = meterReader(image, [cfg]) print(cfg, receive2) print("codecov done")
def meterReaderAPI(): data = request.get_data().decode("utf-8") data = json.loads(data) imageID = data["imageID"] meterIDs = getMeterIDs(imageID) 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") return sendData
def codecov(): # imgPath = "info/20190128/IMAGES/Pic_0225" imgPath = "info/image" images = os.listdir(imgPath) config = os.listdir(configPath) for im in images: image = cv2.imread(imgPath + "/" + im) print(im) pos = im.split(".")[0].split("-") for i in range(1, 6): cfg = pos[0] + "-" + pos[1] + "_" + str(i) if cfg + ".json" in config: receive2 = meterReader(image, [cfg]) print(cfg, receive2) print("codecov done")
def codecov(imgPath): images = os.listdir(imgPath) config = os.listdir(configPath) config = [s.split(".")[0][:-2] for s in config] newconfig = [] for item in config: if item not in newconfig: newconfig.append(item) for im in images: image = cv2.imread(imgPath + "/" + im) # todo 读取图片 print(im) pos = im.split(".")[0].split("-") # cfg = im.split(".")[0]+"_1" for i in range(1, 6): cfg = pos[0] + "-" + pos[1] + "_" + str(i) if cfg in newconfig: receive2 = meterReader(image, [cfg]) # todo 调用meterReader 接口 print(cfg, receive2) # cv2.imshow("ds",image) # cv2.waitKey() print("codecov done")
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"]))
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/SF6_1.JPG") print(meterReader(image, ["SF6_1"]))