Skip to main content

Overview

The CameraManager class provides a simple interface for capturing images from a USB camera using OpenCV. It handles camera initialization, frame buffering, and image saving. Source: arm_system/perception/vision/camera/main.py:5

Class Definition

CameraManager

class CameraManager:
    def __init__(self, camera_index: int = 0, width: int = 1280, height: int = 720)
Initializes camera with specified resolution.
camera_index
int
default:"0"
Camera device index (0 for default camera)
width
int
default:"1280"
Camera frame width in pixels
height
int
default:"720"
Camera frame height in pixels

Attributes

cap
cv2.VideoCapture
OpenCV video capture object

Methods

capture_image

def capture_image(self) -> str
Captures a single image from the camera and saves it to disk. Process:
  1. Grabs 5 frames to flush camera buffer
  2. Reads fresh frame
  3. Generates timestamped filename
  4. Creates output directory if needed
  5. Saves image as PNG
filename
str
Full path to saved image file, or None if capture failed
Output Directory: {script_directory}/objects_images/ Filename Format: YYYYMMDD-HHMMSS.png Source: arm_system/perception/vision/camera/main.py:11

Example

from arm_system.perception.vision.camera.main import CameraManager

# Initialize camera
camera = CameraManager(camera_index=0, width=1280, height=720)

# Capture image
image_path = camera.capture_image()

if image_path:
    print(f"Image saved to: {image_path}")
else:
    print("Failed to capture image")

Resource Management

The camera is automatically released when the CameraManager object is destroyed:
def __del__(self):
    self.cap.release()
Source: arm_system/perception/vision/camera/main.py:28

Integration

The CameraManager is used by the CommunicationManager during scan operations:
# In CommunicationManager.__init__
self.camera = CameraManager(camera_index=camera_index)

# In CommunicationManager._handle_object_detection
img_path = self.camera.capture_image()

Configuration

Supported Camera Indices

  • 0 - Default USB camera
  • 1, 2, etc. - Additional cameras (if connected)

Resolution Options

Common resolutions:
  • 1280x720 (720p HD) - Default
  • 1920x1080 (1080p Full HD)
  • 640x480 (VGA)

Error Handling

The capture_image() method returns None if:
  • Camera is not accessible
  • Frame read fails
  • No frame data available

Example Usage with Error Handling

from arm_system.perception.vision.camera.main import CameraManager
import time

camera = CameraManager()

# Capture multiple images with delay
for i in range(5):
    image_path = camera.capture_image()
    if image_path:
        print(f"Captured: {image_path}")
    else:
        print(f"Failed to capture image {i+1}")
    time.sleep(1)

Build docs developers (and LLMs) love