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,
)| Parameter | Type | Description |
|---|---|---|
index | int | Machine tool index. |
name | str | Human-readable name for the tool. |
ip_address | str | IP address of the Raspberry Pi camera server. |
port | int | Port number for HTTP access. |
video_endpoint | str | Path for video streaming endpoint. |
still_endpoint | str | Path for still image capture endpoint. |
image_folder | str | Folder to store downloaded images. |
focus_height | float | Target height (Z) for focusing images. |
light | bool | Whether a ring light is attached. |
light_pin | int | GPIO pin number controlling the ring light (optional). |
Key Attributes
| Attribute | Type | Description |
|---|---|---|
still_url | str | Full URL for capturing a still image. |
video_url | str | Full URL for accessing the live video feed. |
focus_height | float | Target focus height relative to machine tool Z. |
light_pin | int | GPIO pin used to control illumination (if available). |
Important Methods
| Method | Returns | Description |
|---|---|---|
from_config(index, name, config_file, path) | Camera | Instantiate Camera object from a JSON config file. |
_capture_image(timeout=30) | bytes | Internal method to capture an image as binary data (bytestring). |
capture_image(location, light=False, light_intensity=0, timeout=30) | bytes | Move to location and capture an image, optionally turning on the light. |
video_feed() | None | Open the camera's video stream in the default web browser. |
decode_image(image_bin) | np.array | Decode 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.array | Internally 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) | None | Visualize the image with optional masking using matplotlib. |
Method Details
Capture Image
python
capture_image(location, light=False, light_intensity=0)- Move machine to specified
locationandfocus_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, andnumpylibraries.