Real-time object detection through webcam using YOLOv5 (You Only Look Once). This implementation is optimized to run on MacBook Air M3 using CPU-only inference, while still achieving real-time performance.
- Real-time object detection using your webcam
- CPU-optimized for Apple Silicon (M3 chip)
- Detection of 80 different object classes with confidence scores
- Automatic webcam access management with macOS permission handling
- Average inference time of ~40-50ms on MacBook Air M3 (20+ FPS)
This project uses YOLOv5, a state-of-the-art real-time object detection system. Key implementation details:
- Model: YOLOv5s (small variant) with 7.2M parameters
- Inference: CPU-only inference optimized for Apple Silicon
- Resolution: Input frames are processed at 640x640 resolution
- Performance: ~40-50ms inference time (~20-25 FPS) on MacBook Air M3
- Framework: PyTorch backend with OpenCV for camera handling
- Camera Access: Multi-index camera detection with macOS permission handling
- Detection Threshold: 0.25 confidence threshold for object detection
- Python 3.6+
- macOS (tested on macOS Sonoma with M3 chip)
- External webcam or built-in camera
- Required Python packages:
- torch
- torchvision
- opencv-python
- numpy
- Pillow
- Clone this repository:
git clone https://github.com/nudro/yolo-webcam.git
cd yolo-webcam- Run the installation script which will automatically install all dependencies:
./run_yolo_webcam.shRun the script with:
./run_yolo_webcam.shThis will:
- Check if Python 3 is installed
- Guide you through camera permission setup (if needed)
- Download the YOLOv5s model if needed
- Start the webcam detection
While the detection window is open:
- Press 'q' to quit the application
On first run, you may need to grant camera access permission:
- Open System Preferences → Security & Privacy → Privacy → Camera
- Ensure Terminal (or your Python application) is checked in the list
- You may need to click the lock icon in the bottom left to make changes
- Restart the application after granting permission
- Camera Initialization: The system attempts to connect to available cameras (index 0-2)
- Frame Processing: Captured frames are resized to 640x640 and normalized
- Inference: The YOLOv5s model processes frames with PyTorch on CPU
- Non-Maximum Suppression: Overlapping detections are filtered
- Visualization: Detected objects are annotated with bounding boxes and labels
The implementation includes several optimizations for MacBook Air M3:
- Efficient tensor operations optimized for Apple Silicon
- Minimal preprocessing to reduce CPU overhead
- Direct use of YOLOv5's detect.py script which is highly optimized
You can modify the detection parameters by editing the arguments in webcam_detection.py:
- Change
--confvalue to adjust detection threshold - Change
--img-sizeto adjust processing resolution - Filter specific classes by adding the
--classesparameter
This project uses YOLOv5 by Ultralytics.
