Add face detection to camera capture

- capture.py: detect faces with haar cascades (cv2.CascadeClassifier),
  draw green rectangles on frames before streaming
- capture/haarcascades/: bundled haar cascade XML
- README.md: document face detection feature and opencv dependency
This commit is contained in:
mikkeli
2026-08-06 10:24:49 +09:00
parent d4eebf79f6
commit 0f423fccd6
4 changed files with 33433 additions and 29 deletions
+14 -12
View File
@@ -6,14 +6,15 @@ Raspberry Pi 5.
## Quick start
```bash
# Ensure the v4l2 Python bindings are installed
sudo apt install -y python3-v4l2
# Install dependencies
sudo apt install -y python3-opencv python3-v4l2
# Run directly
python3 -m camera.app --host 0.0.0.0 --port 8080
```
Open http://localhost:8080 in a browser.
Open http://localhost:8080 in a browser. Faces visible in the camera view are marked
with green rectangles.
## Systemd service
@@ -22,8 +23,8 @@ The app runs natively on the Pi via a systemd unit.
### Install
```bash
# Install the v4l2 Python bindings
sudo apt install -y python3-v4l2
# Install dependencies
sudo apt install -y python3-opencv python3-v4l2
# Copy the service unit
sudo cp camera-webui.service /etc/systemd/system/
@@ -51,7 +52,7 @@ sudo systemctl daemon-reload
```
┌──────────────┐ ┌──────────────┐ ┌──────────┐
│ V4L2 mmap │───▶│ capture.py │───▶│ MJPEG │
│ (V4L2 API) │ │ (frame loop)│ │ server │
│ (V4L2 API) │ │ + face det. │ │ server │
└──────────────┘ └──────────────┘ │ :8080 │
└──────────┘
@@ -61,11 +62,11 @@ sudo systemctl daemon-reload
└───────────┘
```
- **`capture.py`** opens `/dev/video0` via V4L2 mmap buffers, dequeues frames in a background
thread, and stores them in a shared variable.
- **`capture.py`** opens `/dev/video0` via V4L2 mmap buffers, dequeues MJPEG frames in a
background thread, runs face detection (haar cascades, downscaled for speed), draws
green rectangles, and stores the processed frame.
- **`mjpeg.py`** serves the MJPEG stream (`multipart/x-mixed-replace`) via Python's
built-in `http.server` with threading. Each client gets its own thread, and only new frames
are sent.
built-in `http.server` with threading. Only new frames are sent.
- **`app.py`** is the entry point: argument parsing, signal handling, and server launch.
## Hardware
@@ -79,5 +80,6 @@ sudo systemctl daemon-reload
## Dependencies
- `python3-v4l2` — V4L2 Python bindings (system package, `apt install python3-v4l2`)
- No pip packages needed — the server uses Python's standard library only
- `python3-opencv` — OpenCV for face detection (haar cascades, JPEG encode/decode)
- `python3-v4l2` — V4L2 Python bindings for camera access
- No pip packages needed