wu_xinjun
2022-03-07 360ba8a616cb525053f288ec5dc3fc9719734282
src/engine/devices.py
File was renamed from src/engine/CameraDevice.py
@@ -1,3 +1,4 @@
from lib2to3.pytree import convert
import cv2
import time
import threading
@@ -57,7 +58,7 @@
        """start capture thread"""
        if self._capture_thread is None:
            self._capture_thread = \
                CaptureThread(self.cameras,self.image_data_queue,interval=10)
                CaptureThread(self.cameras,self.image_data_queue,self.device_id,interval=10)
            self._capture_thread.setDaemon(True)
            self._capture_thread.start()
@@ -68,11 +69,18 @@
            self._capture_thread = None
class microwaveController(object):
    pass
class dustCleanerController(object):
    pass
class CaptureThread(threading.Thread):
    """
    camera capture thread
    """
    def __init__(self, cameras, msg_queue, interval, image_quality = 95):
    def __init__(self, cameras, msg_queue, interval, device_id,image_quality = 95):
        """init the thread"""
        threading.Thread.__init__(self)
        self.cameras = cameras
@@ -80,43 +88,45 @@
        self.interval = interval
        self.image_quality = image_quality
        self.is_stop = False
        self.device_id = device_id
    def stop(self):
        """stop the thread"""
        self.is_stop = True
    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:
                data1 = pb.ImagesMessage()
                for i in range(len(self.cameras)):
                    _, frame = self.cameras[i].read()
                    encode_param = [int(cv2.IMWRITE_JPEG_QUALITY), self.image_quality]
                    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数组
                        if i == 0:
                            data1.image1 = byte_array
                        elif i == 1:
                            data1.image2 = byte_array
                        elif i == 2:
                            data1.image3 = byte_array
                        elif i == 3:
                            data1.image4 = byte_array
                data1_str = data1.SerializeToString()
                        # convert the image to pb.DataField
                        imagedata = pb.DataField()
                        imagedata.data = byte_array
                        imagedata.length = len(byte_array)
                # create protobuf message
                req = pb.RequestCommand()
                req.sys_command = pb.SysCommand.FIVE_G_COMMAND
                req.sub_command = pb.SubCommand.FIVE_G_SEND_IMAGE_SUB_COMMAND
                req.data1.length = len(data1_str)
                req.data1.data = data1_str
                # add message to queue
                self.msg_queue.put(req)
                        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: