Skip to content

WebCamera (class name: Camera)

Description

A network-connected camera client for controlling and acquiring images from a Raspberry Pi Camera Server over HTTP.

This tool allows image capture, video stream viewing, and basic image processing directly from a web-based camera interface.

Constructor

python
Camera(
    index: int,
    name: str,
    ip_address: str,
    port: int,
    video_endpoint: str,
    still_endpoint: str,
    image_folder: str,
    focus_height: float,
    light: bool = False,
    light_pin: int = None,
)
ParameterTypeDescription
indexintMachine tool index.
namestrHuman-readable name for the tool.
ip_addressstrIP address of the Raspberry Pi camera server.
portintPort number for HTTP access.
video_endpointstrPath for video streaming endpoint.
still_endpointstrPath for still image capture endpoint.
image_folderstrFolder to store downloaded images.
focus_heightfloatTarget height (Z) for focusing images.
lightboolWhether a ring light is attached.
light_pinintGPIO pin number controlling the ring light (optional).

Key Attributes

AttributeTypeDescription
still_urlstrFull URL for capturing a still image.
video_urlstrFull URL for accessing the live video feed.
focus_heightfloatTarget focus height relative to machine tool Z.
light_pinintGPIO pin used to control illumination (if available).

Important Methods

MethodReturnsDescription
from_config(index, name, config_file, path)CameraInstantiate Camera object from a JSON config file.
_capture_image(timeout=30)bytesInternal method to capture an image as binary data (bytestring).
capture_image(location, light=False, light_intensity=0, timeout=30)bytesMove to location and capture an image, optionally turning on the light.
video_feed()NoneOpen the camera's video stream in the default web browser.
decode_image(image_bin)np.arrayDecode captured image bytes into an RGB array.
process_image(image_bin, radius=50)list[float]Apply circular mask and return the average RGB values.
_mask_image(image, radius=50)np.arrayInternally apply a circular mask to an image.
_get_rgb_avg(image)list[float]Calculate average RGB values from a masked image.
view_image(image_bin, masked=False, radius=50)NoneVisualize the image with optional masking using matplotlib.

Method Details

Capture Image

python
capture_image(location, light=False, light_intensity=0)
  • Move machine to specified location and focus_height.
  • Optionally turns on the ring light before taking picture.
  • Returns a raw bytestring representing the captured JPEG image.

Video Feed

python
video_feed()
  • Opens the video stream URL in the user's default browser.
  • Live view of the camera.

Decode Image

python
decode_image(image_bin)
  • Converts a JPEG bytestring into a NumPy array with RGB format.

Process Image

python
process_image(image_bin, radius=50)
  • Circular mask is applied around the image center.
  • Saves full and masked images locally for debugging.
  • Computes and returns average RGB inside masked region.

View Image

python
view_image(image_bin, masked=False, radius=50)
  • Display the captured image in a matplotlib window.
  • Useful for quick visual inspection without saving files.

Usage Example

python
webcam = Camera.from_config(index=3, name="WebCamera", config_file="webcam_config.json")
webcam._machine = my_machine  # set machine controller

# Open live video feed
webcam.video_feed()

# Capture still image at a well
image_bytes = webcam.capture_image(well_A1)

# View image
webcam.view_image(image_bytes, masked=True, radius=60)

# Process and extract average RGB
rgb_avg = webcam.process_image(image_bytes)
print(rgb_avg)

Important Notes

  • Unlike the PiCamera tool, this class talks over HTTP with a camera server — not direct hardware control.
  • Lighting control is optional and depends on how your Pi's GPIO pins are configured.
  • Can save masked vs. full images automatically for verification.
  • Requires external requests, cv2, matplotlib, and numpy libraries.

Pre-release documentation · Internal research use only · Not authorized for redistribution.