This directory contains example Python scripts that demonstrate how to use PyNode workflows programmatically without the UI.
A basic example that captures frames from your webcam and displays them in an OpenCV window.
Features:
- Camera capture at 30 FPS
- Real-time display using OpenCV
- Configurable resolution (default: 640x480)
Usage:
python examples/camera_workflow.pyControls:
- Press
qin the display window to quit - Press
Ctrl+Cin terminal to quit
An advanced example that captures camera frames, performs real-time YOLO object detection, and displays the annotated results.
Features:
- Camera capture at 30 FPS
- YOLOv8 object detection (uses yolov8n.pt - nano model)
- Real-time bounding boxes and labels
- FPS counter
- Detection count display
- Configurable confidence threshold
Usage:
python examples/camera_yolo_workflow.pyRequirements:
- OpenCV (opencv-python)
- Ultralytics YOLO (ultralytics)
- First run will download the YOLOv8n model (~6MB)
Performance Notes:
- Default configuration uses CPU
- For GPU acceleration, change
'device': 'cpu'to'device': 'cuda'in the code - YOLOv8n is the fastest model but less accurate
- For better accuracy, try yolov8s.pt, yolov8m.pt, or yolov8l.pt
Controls:
- Press
qin the display window to quit - Press
Ctrl+Cin terminal to quit
Both examples include a custom DisplayNode that demonstrates how to create your own nodes. You can extend this pattern to create any custom processing nodes:
class MyCustomNode(BaseNode):
display_name = 'My Node'
icon = '⚡'
category = 'function'
input_count = 1
output_count = 1
def on_input(self, msg: Dict[str, Any], input_index: int = 0):
# Process incoming message
payload = msg.get(MessageKeys.PAYLOAD)
# Do your processing
result = self.process(payload)
# Send output
output_msg = self.create_message(payload=result)
self.send(output_msg)All examples follow this pattern:
- Import modules - Import the workflow engine and node types
- Register node types - Register all available node types with the engine
- Create nodes - Instantiate nodes with configuration
- Connect nodes - Wire nodes together to create the workflow
- Start workflow - Begin execution
- Run loop - Keep the script running
- Stop workflow - Clean shutdown
- The first YOLO inference may take a few seconds to load the model
- Adjust the camera FPS based on your processing pipeline speed
- Use JPEG encoding (
encode_jpeg: True) for better performance - Monitor the FPS counter to optimize your pipeline
- For production use, add error handling and logging
Camera not found:
- Check your
camera_index(try 0, 1, 2) - Make sure no other application is using the camera
- On Linux, check camera permissions
Low FPS:
- Reduce camera resolution
- Use a faster YOLO model (yolov8n.pt)
- Enable GPU if available (
device: 'cuda') - Reduce camera FPS to match processing speed
YOLO errors:
- Install ultralytics:
pip install ultralytics - Model will auto-download on first run
- Check internet connection for model download