Skip to main content

Real-time RTSP video stream processor with motion detection, object recognition, and analysis capabilities. Built with Python and OpenCV.

Project description

RTSP Stream Processor

Real-time RTSP video stream processor with motion detection, object recognition, and analysis capabilities. Built with Python and OpenCV.

Build Status PyPI version License Code Style: Black Coverage Status

Author

Tom Sapletta

Repository

https://github.com/mediainspect/rtsp.git

๐Ÿš€ Features

  • RTSP Stream Handling

    • Secure credential management
    • Automatic reconnection
    • Buffer management
    • Support for TCP/UDP protocols
  • Video Processing

    • Motion detection
    • Object recognition
    • Frame analysis
    • Real-time statistics
  • Monitoring

    • Performance metrics
    • Health checks
    • Processing statistics
    • Docker integration

๐Ÿ“‹ Prerequisites

  • Python 3.7+
  • FFmpeg
  • OpenCV dependencies
  • Docker (optional)

๐Ÿ› ๏ธ Makefile Usage

The Makefile provides convenient commands for common tasks. Run make help to see all available targets.

Project Management

  • make install โ€“ Install dependencies
  • make test โ€“ Run tests
  • make lint โ€“ Lint code
  • make run โ€“ Run the main application
  • make clean โ€“ Remove caches

Network Scanning

  • make scan-network โ€“ Scan the default network for common services
  • make scan-cameras โ€“ Scan for cameras and related services
  • make scan-camera IP=192.168.1.100 โ€“ Scan a specific camera IP
  • make scan-quick โ€“ Quick scan of common ports
  • make scan-full โ€“ Comprehensive scan
  • make scan-local โ€“ Scan common local network ranges

Printer Management

  • make scan-printers โ€“ List all available printers

Shell & Interactive Clients

  • make shell โ€“ Start a Python shell in the package context (now in scripts/)
  • make interactive โ€“ Start the interactive command-line client (now in scripts/)

Help

  • make help โ€“ Show all available targets

๐Ÿš Shell Client

Start an interactive Python shell with project context:

make shell

(Uses scripts/shell_client.py)

๐Ÿ–ฅ๏ธ Interactive CLI

Launch the interactive command-line interface:

make interactive

(Uses scripts/interactive_client.py)

๐Ÿ” Network Scanning & Printing

mediainspect includes powerful network scanning capabilities to discover devices like cameras and printers on your local network.

Scan for Network Devices

Scan your local network for various devices and services:

make scan-network

Discover Cameras

Find RTSP cameras on your network:

make scan-cameras

Discover Printers

List all available printers on your system:

make scan-printers

Print a Test Page

Send a test page to your default printer:

make print-test

Using the Network Scanner in Python

You can also use the network scanner directly in your Python code:

from mediainspect.scanner import NetworkScanner
import asyncio

async def scan_network():
    scanner = NetworkScanner()
    
    # Scan for all services
    services = await scanner.scan_network()
    
    # Or scan for specific service types
    cameras = await scanner.scan_network(service_types=['rtsp'])
    
    for service in services:
        print(f"{service.ip}:{service.port} - {service.service} ({service.banner})")

# Run the scan
asyncio.run(scan_network())

๐Ÿ–จ๏ธ Printing Support

mediainspect includes basic printing capabilities using the CUPS (Common Unix Printing System) interface.

๐Ÿ”ง Installation

Using pip

# Clone the repository
git clone https://github.com/mediainspect/rtsp.git
cd rtsp

# Create virtual environment
python -m venv venv
source venv/bin/activate  # Windows: .\venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

start

python main.py

Using Docker

# Build and run using docker-compose
docker-compose up -d

# View logs
docker-compose logs -f

โš™๏ธ Configuration

  1. Create environment file:
cp .env.template .env
  1. Configure your settings:
# RTSP Credentials
RTSP_USER=your_username
RTSP_PASSWORD=your_password

# RTSP Stream Configuration
RTSP_HOST=stream_host_ip
RTSP_PORT=554
RTSP_PATH=/stream

# Processing Configuration
MOTION_THRESHOLD=25.0
BLUR_SIZE=21

๐Ÿ’ป Usage

Basic Usage

from rtsp_client import RTSPClient

# Initialize client
client = RTSPClient()

# Connect to stream
if client.connect():
    try:
        while True:
            frame = client.read_frame()
            # Process frame here
    finally:
        client.disconnect()

With Custom Processing

from video_processor import VideoProcessor

processor = VideoProcessor(
    motion_threshold=25.0,
    blur_size=21,
    min_object_size=1000
)

def process_frame(frame):
    processed_frame, stats = processor.process(frame)
    return processed_frame

๐Ÿ“ฆ Python Package Information

Installation

pip install mediainspect-rtsp

Usage Example

from mediainspect_rtsp.video_processor_rtsp_class import VideoProcessor

processor = VideoProcessor(rtsp_url="rtsp://...", motion_threshold=25.0)
processor.run()

For more details, see the PyPI page and documentation.

๐Ÿ” Monitor and Debug

Health Checks

Access health metrics at:

Prometheus & Grafana

๐Ÿ› ๏ธ Development

Project Structure

rtsp-processor/
โ”œโ”€โ”€ src/
โ”‚   โ”œโ”€โ”€ rtsp_client.py     # RTSP handling
โ”‚   โ”œโ”€โ”€ video_processor.py # Video processing
โ”‚   โ””โ”€โ”€ config.py         # Configuration management
โ”œโ”€โ”€ tests/
โ”‚   โ””โ”€โ”€ test_*.py        # Test files
โ”œโ”€โ”€ docker/
โ”‚   โ”œโ”€โ”€ Dockerfile       # Container definition
โ”‚   โ””โ”€โ”€ docker-compose.yml
โ”œโ”€โ”€ .env.template        # Environment template
โ”œโ”€โ”€ scripts/
โ”‚   โ”œโ”€โ”€ shell_client.py  # Shell client
โ”‚   โ””โ”€โ”€ interactive_client.py  # Interactive client
โ””โ”€โ”€ README.md           # This file

๐Ÿงช Running Tests

This project uses pytest for testing. To run all tests:

make test

Or directly with pytest:

pytest

All modules in mediainspect_rtsp/ are covered by basic import tests in tests/. Extend these with functional tests as needed.

๐Ÿค Contributing

  1. Fork the repository
  2. Create feature branch (git checkout -b feature/amazing-feature)
  3. Commit changes (git commit -m 'Add amazing feature')
  4. Push to branch (git push origin feature/amazing-feature)
  5. Open Pull Request

๐Ÿ“ Versioning

We use SemVer for versioning. For available versions, see the CHANGELOG.md.

๐Ÿ”’ Security

Credential Handling

  • Credentials stored in .env file
  • Passwords never logged
  • URL encoding for special characters
  • Secure connection handling

Best Practices

  • Use environment variables
  • Regularly update dependencies
  • Follow security advisories
  • Implement proper error handling

โ— Common Issues

  1. OpenCV Import Error
# Install system dependencies
sudo ./install_opencv.sh
  1. RTSP Connection Failed
  • Verify credentials
  • Check network connectivity
  • Confirm stream availability
  1. Performance Issues
  • Adjust buffer size
  • Modify processing parameters
  • Check system resources

๐Ÿ“ˆ Performance Tuning

Memory Usage

# Configure buffer size
client = RTSPClient(buffer_size=1024*1024)

Processing Speed

# Adjust processing parameters
processor = VideoProcessor(
    skip_frames=2,
    downscale_factor=0.5
)

๐Ÿ“š Documentation

๐Ÿ“„ License

This project is licensed under the MIT License - see LICENSE file.

๐Ÿ™ Acknowledgments

  • OpenCV community
  • FFmpeg project
  • Docker community
  • All contributors

๐Ÿ“ž Support

For support, please:

  1. Check documentation
  2. Search existing issues
  3. Create new issue if needed

๐Ÿ”„ Changelog

See CHANGELOG.md for all changes.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mediainspect_rtsp-0.1.3.tar.gz (30.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mediainspect_rtsp-0.1.3-py3-none-any.whl (40.9 kB view details)

Uploaded Python 3

File details

Details for the file mediainspect_rtsp-0.1.3.tar.gz.

File metadata

  • Download URL: mediainspect_rtsp-0.1.3.tar.gz
  • Upload date:
  • Size: 30.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.11.12 Linux/6.14.6-300.fc42.x86_64

File hashes

Hashes for mediainspect_rtsp-0.1.3.tar.gz
Algorithm Hash digest
SHA256 4b81d3357a8f827cfbc142f7dbf683a743cf11ac4ffe54fe4d0acab3187136ff
MD5 e16ea647c6acbeeb6a5c35b881ee93b6
BLAKE2b-256 eaa2eed3791e57fc072ec6122d7da355a50572c674a2ca58d34b5165f0ec6577

See more details on using hashes here.

File details

Details for the file mediainspect_rtsp-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: mediainspect_rtsp-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 40.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.3 CPython/3.11.12 Linux/6.14.6-300.fc42.x86_64

File hashes

Hashes for mediainspect_rtsp-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e1dcce46d69db8d409fa0478077135a53da74b9235987fa22690838f512ec2bd
MD5 d8fedbd97c0354e3637ccd04d253d781
BLAKE2b-256 1a092adcb41061740becfdabc896585c94243c498fe50153e989c930cec74b35

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page