| | |
| | | # sys.path.insert(0, src_path) |
| | | |
| | | from ..protos import aerial_pb2 as pb |
| | | from .DustCleaner import DustCleaner |
| | | from .MessageThreads import send_logs_tool, send_images_tool |
| | | |
| | | |
| | | ######################################### CAMERAS ############################################ |
| | | class cameraController(object): |
| | | |
| | | def __init__(self, |
| | |
| | | |
| | | def _generate_msg(self,logs:List[str],images:List): |
| | | |
| | | # constract a images message |
| | | imagesMsg = pb.ImagesMessage() |
| | | imagesMsg.source = self.device_id |
| | | curr_time = datetime.now().strftime('%Y-%m-%d-%H-%M-%S') |
| | | imagesMsg.timestamp = curr_time |
| | | log_rk = self.routing_key+".logs.test0" |
| | | image_rk = self.routing_key+".images.test0" |
| | | send_logs_tool(logs=logs, |
| | | logs_queue=self.logs_queue, |
| | | source=self.device_id, |
| | | routing_key=self.routing_key+".logs.test0") |
| | | |
| | | for log, img in zip(logs,images): |
| | | |
| | | # encoding the images to the bytes |
| | | # encode_params = [int(cv2.IMWRITE_JPEG_QUALITY), self.image_quality] |
| | | # image_encoded = cv2.imencode(".jpg",single_img,encode_params) |
| | | if img is not None: |
| | | success,image_encoded = cv2.imencode(".jpg",img) |
| | | if success: |
| | | image_bytes = image_encoded.tobytes() |
| | | image_length = len(image_bytes) |
| | | # add the encoded image to msg |
| | | image_item = imagesMsg.Image.add() |
| | | image_item.length = image_length |
| | | image_item.data = image_bytes |
| | | |
| | | if log is not None: |
| | | # constract a logs message |
| | | logsMsg = pb.LogsMessage() |
| | | logsMsg.source = self.device_id |
| | | logsMsg.timestamp = f"{time.asctime()}" |
| | | logsMsg.context = log |
| | | # convert the proto object to string |
| | | logsMsg_bytes = logsMsg.SerializeToString() |
| | | # add logsmsg to queue |
| | | self.logs_queue.put([log_rk,logsMsg_bytes]) |
| | | |
| | | |
| | | # converting the pb image msg to bytes |
| | | imageMsg_bytes = imagesMsg.SerializeToString() |
| | | # add the imageMsg to the queue |
| | | self.images_queue.put([image_rk,imageMsg_bytes]) |
| | | send_images_tool(images=images, |
| | | images_queue=self.images_queue, |
| | | source=self.device_id, |
| | | routing_key=self.routing_key+".images.test0") |
| | | |
| | | |
| | | def _capture_image(self,mode:str = "capture"): |
| | |
| | | self.routing_key = self.init_routing_key |
| | | |
| | | |
| | | |
| | | ######################################## MICROWAVE ########################################### |
| | | class microwaveController(object): |
| | | pass |
| | | |
| | | class dustCleanerController(object): |
| | | |
| | | pass |
| | | ####################################### DUST CLEANER ######################################### |
| | | class dustCleanerController(DustCleaner): |
| | | def __init__(self, com, bps, timeout, logs_queue, device_id,routing_key): |
| | | super().__init__(com, bps, timeout) |
| | | |
| | | |
| | | class CaptureThread(threading.Thread): |
| | | """ |
| | | camera capture thread |
| | | """ |
| | | def __init__(self, cameras, msg_queue, interval, device_id,image_quality = 95): |
| | | """init the thread""" |
| | | threading.Thread.__init__(self) |
| | | self.cameras = cameras |
| | | self.msg_queue = msg_queue |
| | | self.interval = interval |
| | | self.image_quality = image_quality |
| | | self.is_stop = False |
| | | self.logs_queue = logs_queue |
| | | self.device_id = device_id |
| | | self.routing_key = routing_key |
| | | self._check_cleaner() |
| | | |
| | | def stop(self): |
| | | """stop the thread""" |
| | | self.is_stop = True |
| | | |
| | | # TODO根据新的数据结构修改队消息队列。 |
| | | def run(self): |
| | | print("CaptureThread started") |
| | | encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), self.image_quality] |
| | | self.next_time = time.time() + self.interval |
| | | while not self.is_stop: |
| | | this_time = time.time() |
| | | if this_time > self.next_time: |
| | | for i in range(len(self.cameras)): |
| | | _, frame = self.cameras[i].read() |
| | | capture_time = time.time() |
| | | _, encimg = cv2.imencode('.jpg', frame, encode_param) |
| | | if frame is not None: |
| | | np_array = np.asarray(encimg) # frame 转换为numpy数组 |
| | | byte_array = np_array.tobytes() # numpy数组转换为byte数组 |
| | | # convert the image to pb.DataField |
| | | imagedata = pb.DataField() |
| | | imagedata.data = byte_array |
| | | imagedata.length = len(byte_array) |
| | | def _check_cleaner(self): |
| | | |
| | | data_str = imagedata.SerializeToString() |
| | | success = self.Open() |
| | | |
| | | # create protobuf message |
| | | data_msg = pb.CapturedData() |
| | | data_msg.generate_time = capture_time |
| | | data_msg.generate_device = self.device_id |
| | | data_msg.camera_port = i |
| | | data_msg.image_data = data_str |
| | | log = "Unknown log." |
| | | if success: |
| | | log = f"The port [{self.port}] refered to the dust cleaner is open." |
| | | else: |
| | | log = f"The port [{self.port}] refered to the dust cleaner can not open." |
| | | |
| | | # display |
| | | print(f"{time.asctime()} | {log}") |
| | | |
| | | send_logs_tool(logs=[log], |
| | | logs_queue=self.logs_queue, |
| | | source = self.device_id, |
| | | routing_key=self.routing_key+".logs.test0") |
| | | |
| | | |
| | | |
| | | # add message to queue |
| | | self.msg_queue.put(data_msg) |
| | | # class CaptureThread(threading.Thread): |
| | | # """ |
| | | # camera capture thread |
| | | # """ |
| | | # def __init__(self, cameras, msg_queue, interval, device_id,image_quality = 95): |
| | | # """init the thread""" |
| | | # threading.Thread.__init__(self) |
| | | # self.cameras = cameras |
| | | # self.msg_queue = msg_queue |
| | | # self.interval = interval |
| | | # self.image_quality = image_quality |
| | | # self.is_stop = False |
| | | # self.device_id = device_id |
| | | |
| | | self.next_time += self.interval |
| | | time.sleep(0.1) |
| | | for camera in self.cameras: |
| | | camera.release() |
| | | print("CaptureThread stopped") |
| | | # def stop(self): |
| | | # """stop the thread""" |
| | | # self.is_stop = True |
| | | |
| | | # # TODO根据新的数据结构修改队消息队列。 |
| | | # def run(self): |
| | | # print("CaptureThread started") |
| | | # encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), self.image_quality] |
| | | # self.next_time = time.time() + self.interval |
| | | # while not self.is_stop: |
| | | # this_time = time.time() |
| | | # if this_time > self.next_time: |
| | | # for i in range(len(self.cameras)): |
| | | # _, frame = self.cameras[i].read() |
| | | # capture_time = time.time() |
| | | # _, encimg = cv2.imencode('.jpg', frame, encode_param) |
| | | # if frame is not None: |
| | | # np_array = np.asarray(encimg) # frame 转换为numpy数组 |
| | | # byte_array = np_array.tobytes() # numpy数组转换为byte数组 |
| | | # # convert the image to pb.DataField |
| | | # imagedata = pb.DataField() |
| | | # imagedata.data = byte_array |
| | | # imagedata.length = len(byte_array) |
| | | |
| | | # data_str = imagedata.SerializeToString() |
| | | |
| | | # # create protobuf message |
| | | # data_msg = pb.CapturedData() |
| | | # data_msg.generate_time = capture_time |
| | | # data_msg.generate_device = self.device_id |
| | | # data_msg.camera_port = i |
| | | # data_msg.image_data = data_str |
| | | |
| | | |
| | | |
| | | # # add message to queue |
| | | # self.msg_queue.put(data_msg) |
| | | |
| | | # self.next_time += self.interval |
| | | # time.sleep(0.1) |
| | | # for camera in self.cameras: |
| | | # camera.release() |
| | | # print("CaptureThread stopped") |