Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor stream to use framegrab #47

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ venv.bak/
# Rope project settings
.ropeproject

# VSCode project settings
.vscode

# mkdocs documentation
/site

Expand Down
35 changes: 25 additions & 10 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
FROM python:3.11-slim-bookworm
# Stage 1: Build environment
FROM python:3.11-slim-bookworm AS builder

# Install dependencies
RUN apt-get update && apt-get install -y \
gcc \
libgl1 \
libglib2.0-0 \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
# Install dependencies and clean up
RUN apt-get update && \
apt-get install -y gcc && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

# Use uv to install python dependencies.
WORKDIR /app
Expand All @@ -20,7 +19,23 @@ RUN --mount=from=ghcr.io/astral-sh/uv,source=/uv,target=/bin/uv \
# Add source code
COPY . /app/

# Install the project
# Stage 2: Final image
FROM python:3.11-slim-bookworm

# Install OpenGL and GLib libraries
RUN apt-get update && \
apt-get install -y libgl1 libglib2.0-0 && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*

# Copy only the necessary files from the builder stage
WORKDIR /app
COPY --from=builder /app/.venv /app/.venv
COPY --from=builder /app/src /app/src
COPY --from=builder /app/pyproject.toml /app/pyproject.toml
COPY --from=builder /app/uv.lock /app/uv.lock

# Install the project in the final stage
RUN --mount=from=ghcr.io/astral-sh/uv,source=/uv,target=/bin/uv \
uv sync --no-dev --frozen --no-editable --no-cache

Expand All @@ -29,4 +44,4 @@ ENV VIRTUAL_ENV=/app/.venv
ENV PATH="/app/.venv/bin:$PATH"

# Run the application
ENTRYPOINT ["python", "-m", "stream"]
ENTRYPOINT ["python", "-m", "stream"]
23 changes: 22 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
.PHONY: build install install-dev test
.PHONY: help build install install-dev test

help:
@echo "Available commands:"
@echo " help - Show this help message"
@echo " build - Build Docker image"
@echo " install - Install package"
@echo " install-dev - Install package with dev dependencies"
@echo " install-uv - Install package using uv"
@echo " install-dev-uv - Install package with dev dependencies using uv"
@echo " test - Run tests"
@echo " test-uv - Run tests using uv"
@echo " relock - Update lockfile using uv"

build:
docker build -t stream:local .
Expand All @@ -9,8 +21,17 @@ install:
install-dev:
pip install -e .[dev]

install-uv:
uv sync

install-dev-uv:
uv sync --all-extras

test:
pytest

test-uv:
uv run pytest

relock:
uv lock
26 changes: 15 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,14 @@ usage: python -m stream -t TOKEN -d DETECTOR [options]

Groundlight Stream Processor

A command-line tool that captures frames from a video source and sends them to a Groundlight detector for analysis.
Captures frames from video sources and sends them to Groundlight detectors for analysis.

Supports a variety of input sources including:
- Video devices (webcams)
- Video files (mp4, etc)
Supported input sources:
- USB cameras and webcams
- RTSP streams
- YouTube videos
- Image directories
- Image URLs
- YouTube Live streams
- HLS streams
- Video files (mp4, mov, mjpeg)

options:
-h, --help show this help message and exit
Expand All @@ -57,12 +56,16 @@ options:
-f FPS, --fps FPS Frames per second to capture (0 for max rate). Defaults to 1 FPS.
-v, --verbose Enable debug logging.
-m, --motion Enables motion detection, which is disabled by default.
-r THRESHOLD, --threshold THRESHOLD
Motion detection threshold (% pixels changed). Defaults to 1%.
-r MOTION_PIXEL_THRESHOLD, --motion_pixel_threshold MOTION_PIXEL_THRESHOLD
Motion detection pixel threshold (% pixels changed). Defaults to 1%.
-b MOTION_VAL_THRESHOLD, --motion_val_threshold MOTION_VAL_THRESHOLD
Motion detection value threshold (degree of change). Defaults to 20.
-p POSTMOTION, --postmotion POSTMOTION
Seconds to capture after motion detected. Defaults to 1 second.
-i MAXINTERVAL, --maxinterval MAXINTERVAL
Max seconds between frames even without motion. Defaults to 1000 seconds.
-k, --keep-connection-open
Keep connection open for low-latency frame grabbing (uses more CPU and network bandwidth). Defaults to false.
-w RESIZE_WIDTH, --width RESIZE_WIDTH
Resize width in pixels.
-y RESIZE_HEIGHT, --height RESIZE_HEIGHT
Expand Down Expand Up @@ -109,10 +112,11 @@ docker run groundlight/stream \
-t api_29imEXAMPLE \
-d det_2MiD5Elu8bza7sil9l7KPpr694a \
-s "${YOUTUBE_URL}" \
-f 1
-k \
-f 5
```

Replace `YOUTUBE_URL` with the url of the YouTube video you are interested in.
Replace `YOUTUBE_URL` with the url of the YouTube video you are interested in. The `-k` parameter is used to keep the connection open for low-latency frame grabbing. This uses more CPU and network bandwidth but can provide faster frame rates.

### Connecting an RTSP Stream

Expand Down
10 changes: 5 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[project]
name = "stream"
version = "0.5.2"
version = "0.6.0"
description = "Groundlight Stream Processor - Container for analyzing video using RTSP etc"
readme = "README.md"
requires-python = ">=3.10"
requires-python = ">=3.11"
dependencies = [
"framegrab>=0.7.0",
"groundlight>=0.18.4",
"framegrab>=0.9.0",
"groundlight>=0.21.0",
"jsonformatter>=0.3.2",
"numpy<2.0.0",
"opencv-python-headless>=4.10.0.84",
Expand All @@ -16,7 +16,7 @@ dependencies = [

[project.optional-dependencies]
dev = [
"pytest>=7.0",
"pytest>=8.0",
"ruff==0.7.2",
]

Expand Down
Loading
Loading