Ejemplo n.º 1
0
    def disable_cameras(self,
                        wait_for_disable: bool = False,
                        timeout: float = 5,
                        retry: bool = True) -> None:
        """Disables camera by setting dac output low."""
        self.logger.debug("Disabling cameras")

        # Check if using usb mux
        if not self.usb_mux_enabled:
            self.logger.debug("Cameras always enabled, not using mux")
            return

        # Turn off usb mux channel
        try:
            channel = self.usb_mux_channel
            self.dac5578.set_low(channel=channel, retry=retry)
        except DAC5578DriverError as e:
            raise exceptions.DisableCameraError(logger=self.logger) from e

        # Check if waiting for disable
        if not wait_for_disable:
            return

        # Wait for camera to be disabled
        self.logger.debug("Waiting for cameras to become disabled")

        # Check if simulated
        if self.simulated:
            self.logger.debug("Simulated camera disable complete")
            return

        # Initialize timing variables
        start_time = time.time()

        # Loop forever
        while True:

            # Look for camera
            try:
                camera_paths = usb.get_camera_paths(self.vendor_id,
                                                    self.product_id)

                # Check if all cameras are disables
                if camera_paths == []:
                    self.logger.debug("All cameras disabled")
                    return

            except Exception as e:
                raise exceptions.DisableCameraError(logger=self.logger) from e

            # Check for timeout
            if time.time() - start_time > timeout:
                message = "timed out"
                raise exceptions.DisableCameraError(message=message,
                                                    logger=self.logger)

            # Update every 100ms
            time.sleep(0.1)
Ejemplo n.º 2
0
    def capture_images(self) -> None:
        """Captures an image from each active camera."""
        self.logger.debug("Capturing images")

        # Get real or simulated camera paths
        if not self.simulate:
            camera_paths = usb.get_camera_paths(self.vendor_id,
                                                self.product_id)
        else:
            camera_paths = []
            for i in range(self.num_cameras):
                camera_paths.append("simulate_path")

        # Check correct number of camera paths
        num_detected = len(camera_paths)
        if num_detected != self.num_cameras:
            message = "Incorrect number of cameras detected, expected {}, detected {}".format(
                self.num_cameras, num_detected)
            message += ". Proceeding with capture anyway"
            self.logger.warning(message)

        # Capture an image from each active camera
        for index, camera_path in enumerate(camera_paths):

            # Get timestring in ISO8601 format
            timestring = datetime.datetime.utcnow().strftime(
                "%Y-%m-%d-T%H:%M:%SZ")

            # Get filename for individual camera or camera instance in set
            if self.num_cameras == 1:
                filename = "{}_{}.png".format(timestring, self.name)
            else:
                filename = "{}_{}.{}.png".format(timestring, self.name,
                                                 index + 1)

            # Create image path
            capture_image_path = self.capture_dir + filename
            final_image_path = self.directory + filename

            # Capture image
            self.capture_image_pygame(camera_path, capture_image_path)
            shutil.move(capture_image_path, final_image_path)