A Python-based camera streaming and recording system that can connect to a camera, stream video in real-time, and automatically save video segments at regular intervals.
Easiest way to use all features: Launch the graphical control panel:
python camera_control_panel.pyThis opens a settings window where you can:
- Select which mode to run (streaming, detection, recording, etc.)
- Configure all settings (camera, model, confidence, output directory, etc.)
- Start/stop the camera with one click
- No need to remember command-line arguments!
- Connect to camera devices
- Real-time video streaming with frame counter
- Display camera properties (resolution, FPS)
- Continuous video recording from camera
- Automatic segmentation (saves video every minute by default)
- Timestamped filenames for easy organization
- Preview window with recording status
- Configurable segment duration and output directory
- Real-time person detection using YOLOv8
- Draws bounding boxes around detected persons
- Shows detection confidence scores
- Multiple model sizes for speed/accuracy tradeoff
- Configurable confidence threshold
- Continuously monitors camera for persons
- Automatically starts recording when person enters frame
- Stops recording when person leaves frame
- Saves video segments only when persons are detected
- Prevents flickering with configurable grace period
- Detects persons holding cups/mugs
- Only records when a person with a cup is detected
- Ignores persons without cups
- Uses spatial proximity to determine if person is holding cup
- Saves recordings to separate directory
-
Install Python 3.7 or higher
-
Install required dependencies:
pip install -r requirements.txtNote: The GUI control panel uses tkinter, which is included with Python on most systems. If you get an error about tkinter, install it:
- Ubuntu/Debian:
sudo apt-get install python3-tk - macOS: Usually pre-installed
- Windows: Usually pre-installed
The easiest way to use all features is through the graphical control panel:
python camera_control_panel.pyFeatures:
- Mode Selection: Choose from all available modes
- Basic Streaming
- Person Detection (view only)
- Person Recording
- Person with Cup Recording
- Settings Configuration: Adjust all parameters through GUI
- Camera index
- YOLO model size and confidence
- Output directory
- Recording duration and grace periods
- One-Click Control: Start/Stop buttons
- Real-time Status: See current status of camera operations
For advanced users or scripting, you can also use the command-line interface:
To stream video from your camera:
python camera_stream.pyOr specify a different camera index:
python camera_stream.py --camera 1Press 'q' to quit the stream.
To record video segments (saves every 60 seconds by default):
python camera_recorder.pyCommand-line options:
--camera N: Specify camera index (default: 0)--duration N: Segment duration in seconds (default: 60)--output DIR: Output directory for video files (default: recordings)--no-preview: Disable preview window (useful for headless operation)
Examples:
Record with 30-second segments:
python camera_recorder.py --duration 30Record to a custom directory:
python camera_recorder.py --output /path/to/videosRecord without preview window:
python camera_recorder.py --no-previewTo detect persons in the camera stream with bounding boxes:
python person_detector.pyCommand-line options:
--camera N: Specify camera index (default: 0)--model SIZE: YOLO model size -nano(fastest),small,medium,large,xlarge(most accurate, default: nano)--confidence FLOAT: Confidence threshold 0.0-1.0 (default: 0.25)
Examples:
Use a larger model for better accuracy:
python person_detector.py --model smallAdjust confidence threshold (higher = fewer false positives):
python person_detector.py --confidence 0.5Use different camera:
python person_detector.py --camera 1Note: On first run, the YOLO model will be automatically downloaded (this may take a moment).
Monitor camera and automatically record only when persons are detected:
python person_recorder.pyHow it works:
- Camera continuously monitors for persons
- Recording starts automatically when a person enters the frame
- Recording stops when no person is detected (after a short grace period)
- Each recording is saved as a separate video file with timestamp in filename
- Timestamp overlays: Every frame in recorded videos includes a timestamp overlay (top-right corner)
- Recording log: All recording events are logged to
recording_log.txtwith precise timestamps
Command-line options:
--camera N: Camera index (default: 0)--model SIZE: YOLO model size (default: nano)--confidence FLOAT: Confidence threshold 0.0-1.0 (default: 0.25)--output DIR: Output directory (default: person_recordings)--min-frames N: Minimum frames before stopping (prevents flickering, default: 30)--no-preview: Disable preview window
Examples:
Record with higher confidence threshold:
python person_recorder.py --confidence 0.5Use more accurate model:
python person_recorder.py --model smallCustom output directory:
python person_recorder.py --output /path/to/recordingsOutput:
-
Video files: Saved with timestamped filenames like:
person_20251208_143022.mp4- Recording when person enteredperson_20251208_143145.mp4- Next recording when person entered again
-
Timestamp overlays: Each recorded video frame displays:
- Current date and time (top-right corner)
- Recording duration (elapsed time since recording started)
-
Recording log:
recording_log.txtfile contains:- Timestamp of each recording start
- Timestamp of each recording stop
- Duration and frame count for each recording
- Session summary statistics
Monitor camera and record only when a person holding a cup is detected:
python person_cup_detector.pyHow it works:
- Continuously monitors for persons and cups
- Uses spatial proximity to detect if a person is holding a cup
- Only records when person + cup combination is detected
- Ignores persons without cups
- Saves recordings to separate directory (
person_cup_recordings/)
Command-line options:
--camera N: Camera index (default: 0)--model SIZE: YOLO model size (default: nano)--confidence FLOAT: Confidence threshold 0.0-1.0 (default: 0.25)--output DIR: Output directory (default: person_cup_recordings)--min-frames N: Minimum frames before stopping (default: 30)--no-preview: Disable preview window
Examples:
# Use more accurate model
python person_cup_detector.py --model small
# Adjust confidence threshold
python person_cup_detector.py --confidence 0.5
# Custom output directory
python person_cup_detector.py --output /path/to/cup_recordingsVisual indicators:
- Blue boxes: Detected persons
- Yellow boxes: Detected cups
- Green boxes: Person with cup (recording trigger)
- Green line: Connection between person and cup
Output:
- Video files saved as:
person_cup_YYYYMMDD_HHMMSS.mp4 - Timestamp overlays on each frame
- Recording log file:
recording_log.txt
Video segments are saved in the recordings directory (or your specified output directory) with filenames like:
segment_20240115_143022.mp4segment_20240115_143122.mp4- etc.
Each filename includes a timestamp indicating when the recording started.
- The system will automatically try alternative camera indices if the default camera (index 0) is not available
- Video files are saved in MP4 format using the 'mp4v' codec
- If the camera FPS cannot be detected, a default of 30 FPS is used
- Press 'q' in the preview window to stop recording gracefully
Camera not detected:
- Try different camera indices:
--camera 1or--camera 2 - Check that your camera is properly connected
- On Linux, you may need to check permissions:
ls -l /dev/video*
Permission errors:
- On Linux, you may need to add your user to the
videogroup:Then log out and log back in.sudo usermod -a -G video $USER
Video playback issues:
- If MP4 files don't play, you may need to install additional codecs
- Consider using a different codec by modifying the
fourccparameter incamera_recorder.py