Ejemplo n.º 1
0
 def _initialise_stream(self, camera_config):
     cam_number = camera_config.camera_number.value()
     width = camera_config.width.value()
     height = camera_config.height.value()
     stream = CaptureManager(Camera(cam_number, width, height))
     stream.create_capture()
     return stream
    def _test_camera(self):
        # Check that values are integers
        try:
            camera = Camera(int(self.txt_number.text()),
                            int(self.txt_width.text()),
                            int(self.txt_height.text()))
        except ValueError:
            QMessageBox.critical(
                self, "Camera Error",
                "Camera number, width, and height must be integers")
            return

        # Check that we can connect to the camera
        stream = CaptureManager(camera)
        stream.create_capture()

        # Display a preview feed from the camera
        breaking_frame = False
        while True:
            # Capture the next frame from the camera
            frame = stream.get_frame()
            if frame is None:
                QMessageBox.critical(self, "Camera Error",
                                     "Cannot find specified camera")
                return

            if frame is None:
                breaking_frame = True
                break
            elif cv2.waitKey(1) not in (255, -1):
                break

            small = cv2.resize(frame, (0, 0), fx=0.5, fy=0.5)
            cv2.imshow('Camera Preview (Press any key to exit)', small)

            # Check resolution is acceptable
            set_width = int(stream.get_width())
            set_height = int(stream.get_height())
            if set_width != camera.get_camera_width(
            ) or set_height != camera.get_camera_height():
                QMessageBox.warning(
                    self, "Camera Error",
                    "Could not set the camera to the specified resolution: {}x{}.\nThe camera defaulted "
                    "to {}x{}.".format(camera.get_camera_width(),
                                       camera.get_camera_height(), set_width,
                                       set_height))
                self.txt_width.setText(str(set_width))
                self.txt_height.setText(str(set_height))
                return

        stream.release_resources()
        cv2.destroyAllWindows()

        # Opening the camera controls window stops the camera from working; reopen this window
        if breaking_frame:
            self._test_camera()
 def _test_camera_settings(self):
     # Check that we can connect to the camera
     stream = CaptureManager(self._camera_config)
     stream.create_capture()
     stream.read_frame()
     read_ok = stream.is_read_ok()
     stream.release_resources()
     if not read_ok:
         # Capture the next frame from the camera
         raise ValueError
    def _display_camera_preview(self):
        stream = CaptureManager(self._camera_config)
        stream.create_capture()
        while True:
            stream.read_frame()
            if stream.is_read_ok():
                res = stream.get_frame().get_frame()
                if res is not None:
                    small = res
                    cv2.imshow('Camera Preview', small)
                    cv2.waitKey(50)
            if cv2.getWindowProperty('Camera Preview',
                                     cv2.WND_PROP_VISIBLE) == 0:
                break

        cv2.destroyAllWindows()
        stream.release_resources()
class StreamManager:
    
    def __init__(self, camera_config, cam_position):
        self.camera_config = camera_config
        self.camera_position = cam_position
        self.stream = None
        self._scanner = None 
        
    def initialise_stream(self, ):
        self.stream = CaptureManager(self.camera_config)
        
    def create_capture(self):
        self.stream.create_capture()
        
    def release_capture(self):
        self.stream.release_resources()
    
    def create_scanner(self,config):
        if self.camera_position == CameraPosition.SIDE:
            plate_type = "None"
            barcode_sizes = DataMatrix.DEFAULT_SIDE_SIZES
        else:
            plate_type = config.plate_type.value()
            barcode_sizes = [config.top_barcode_size.value()]

        if plate_type == "None":
            self._scanner = OpenScanner(barcode_sizes)
        else:
            self._scanner = GeometryScanner(plate_type, barcode_sizes)

    def process_frame(self,frame):
        if frame is None:
            return ScanResult(0)
            
        return self._scanner.scan_next_frame(frame)
            
    def get_frame(self):
        self.stream.read_frame()
        if self.stream.is_read_ok():
            frame = self.stream.get_frame()
            return frame
        return None
 def _open_camera_controls(self):
     camera_num = int(self.txt_number.text())
     CaptureManager.open_camera_controls(camera_num)
 def initialise_stream(self, ):
     self.stream = CaptureManager(self.camera_config)