A Python-based video servo control system for the Pixy2 camera, enabling automated pan/tilt tracking functionality with audio recording capabilities. This is a hobby project, and a work in progress. Don't use this code if you're not comfortable with c++ and swig, or if you expect it to work correctly on your machine out of the box. thanks!
Next Feature: face recognition + tracking.
Before building and running the video servo system, ensure you have the following dependencies installed:
brew install swig libusb portaudio ffmpeg timgsudo apt-get update
sudo apt-get install -y swig libusb-1.0-0-dev python3-dev portaudio19-dev ffmpeg timgAlternatively, you can use the provided installation script:
./install_prerequisites.shInstall the required Python packages using either:
# From the root directory
pip3 install -r requirements.txt
# Or from the python_demos directory
cd src/python_demos
pip3 install -r requirements.txt- Clone the repository:
git clone https://github.com/tamirelazar/video_servo.git
cd video_servo- Build the Python module:
cd src/python_demos
python3 setup.py build_ext --inplaceNote: The build process will create a _pixy.cpython-*.so file in the python_demos directory. This file is specific to your Python version and operating system.
-
If you get an error about missing
libusb-1.0:- On macOS: Make sure you've installed libusb with
brew install libusb - On Ubuntu/Debian: Install with
sudo apt-get install libusb-1.0-0-dev
- On macOS: Make sure you've installed libusb with
-
If you get an error about missing
Python.h:- On Ubuntu/Debian: Install python3-dev with
sudo apt-get install python3-dev
- On Ubuntu/Debian: Install python3-dev with
-
If you get an error about SWIG:
- Make sure SWIG is installed:
- macOS:
brew install swig - Ubuntu/Debian:
sudo apt-get install swig
- macOS:
- Make sure SWIG is installed:
-
Connect your Pixy2 camera to your computer via USB.
-
Run the video servo program:
cd src/python_demos
python3 video_servo.py-
Movement:
a/d: Pan left/rightw/s: Tilt up/downc: Center servos
-
Presets:
1: Smooth (slower, precise)2: Responsive (quick)3: Precise (balanced speed)4: Balanced (default)
-
Speed Control:
[: Decrease velocity scale]: Increase velocity scale
-
Video/Audio:
r: Toggle recording (video + audio)p: Toggle display mode (terminal/window)q: Quit
The system supports two display modes for the video preview:
- Terminal Display (default): Displays the video directly in the terminal using timg. This mode is useful for remote connections or when you want to keep everything in a single terminal window.
- Window Display: Shows the video in a separate OpenCV window.
Toggle between these modes using the p key.
The system now supports audio recording alongside video capture. When enabled in the config.json file, audio will be recorded from your selected microphone whenever video recording is active. The recordings are automatically combined using FFmpeg after recording stops.
To configure audio settings, modify the audio section in src/config.json:
"audio": {
"enabled": true, // Set to false to disable audio recording
"sample_rate": 44100, // Audio sample rate in Hz
"channels": 1, // Number of audio channels (1 for mono, 2 for stereo)
"format": "paInt16", // Audio format (paInt16, paInt24, paInt32, paFloat32)
"chunk_size": 1024, // Audio buffer chunk size
"device_index": null // Specify a device index or null for default
}On first run with audio enabled, you'll be prompted to select an audio input device from available microphones.
Recordings are saved to the src/output directory with the following naming convention:
- Video:
video_servo_YYYYMMDD_HHMMSS.mp4 - Audio:
audio_servo_YYYYMMDD_HHMMSS.wav - Combined:
video_servo_YYYYMMDD_HHMMSS_combined.mp4
.
├── src/
│ ├── arduino/ # Arduino library headers
│ ├── common/ # Common utility files
│ ├── libpixyusb2/ # Core USB communication library
│ ├── audio_recorder.py # Audio recording functionality
│ ├── config.json # Configuration settings
│ ├── video_servo_interface.py # Main interface program
│ └── python_demos/ # Python interface and examples
│ ├── setup.py # Build script for Python module
│ ├── pixy.i # SWIG interface file
│ ├── pixy_python_interface.cpp # Python/C++ interface
│ ├── requirements.txt # Python dependencies
│ └── video_servo.py # Main program
├── install_prerequisites.sh
└── README.md
The project uses SWIG to generate Python bindings for the C++ Pixy2 library. The main components are:
video_servo.py: Main program for video-based servo controllibpixyusb2: Core C++ library for USB communication with Pixy2- SWIG interface files for Python bindings
The build process involves several steps:
- SWIG generates Python wrapper code from the interface file (
pixy.i) - The C++ code is compiled into a shared library
- Python bindings are created to interface with the library
If you modify any of the C++ source files or SWIG interface, you'll need to rebuild the module:
cd src/python_demos
python3 setup.py clean --all
python3 setup.py build_ext --inplaceThis project is derived from the Pixy2 library. All original Pixy2 code retains its original license.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
- Timestamp-Based Synchronization: Each audio and video frame is precisely timestamped at capture time
- Real-time Synchronized Access: External applications can access perfectly synchronized audio and video streams
- Drift Monitoring: The system continuously monitors audio-video drift with visual indicators
- Enhanced FFmpeg Processing: Improved audio-video combination that uses timestamp metadata
- Configurable Parameters: Fine-tune synchronization behavior through config.json
- Adaptive Time Stretching: Automatically stretches the shorter media file (audio or video) to match the duration of the longer one, ensuring perfect synchronization
Configure synchronization in the config.json
