A high-performance Go service that captures video frames from a camera, detects humans in real-time, and sends frames to a backend server using gRPC for efficient communication.
- High-Performance Video Capture: Uses Go bindings for OpenCV to efficiently capture video frames.
- Real-time Human Detection: Implements lightweight object detection using YOLOv4-tiny model.
- gRPC Frame Transmission: Efficiently streams frames with detected humans to a backend server using gRPC.
- Modular Design: Components are designed to be easily replaceable and extensible.
- Containerized Deployment: Docker and Docker Compose configuration for easy deployment.
- Optimized for Performance: Designed for low latency, high speed, and efficient memory usage.
- Go 1.22 or higher
- OpenCV 4.x
- Docker and Docker Compose (for containerized deployment)
- Protocol Buffer Compiler (protoc)
# Install OpenCV
brew install opencv
# Install Go
brew install go
# Install Protocol Buffer Compiler
brew install protobuf- Clone the repository:
git clone https://github.com/adverie/videoservice.git
cd videoservice- Install Go dependencies:
go mod tidy- Generate gRPC code:
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2
protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative proto/gyandoot/gyandoot.proto- Download YOLOv4-tiny model files:
# Create the configs directory if it doesn't exist
mkdir -p configs/detector
# Download YOLOv4-tiny weights
wget -O configs/detector/yolov4-tiny.weights https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4-tiny.weights
# Download YOLOv4-tiny configuration
wget -O configs/detector/yolov4-tiny.cfg https://raw.githubusercontent.com/AlexeyAB/darknet/master/cfg/yolov4-tiny.cfg
# Download COCO class names (or use the provided one)
wget -O configs/detector/coco.names https://raw.githubusercontent.com/AlexeyAB/darknet/master/data/coco.names-
Ensure you have a gRPC backend server running and accessible.
-
Set the
GYANDOOT_ADDRenvironment variable to the address of your gRPC server (e.g.,localhost:50051). -
In a separate terminal, start the video processing service:
GYANDOOT_ADDR=localhost:50051 go run cmd/videoservice/main.go... To be added.
You can adjust camera settings in cmd/videoservice/main.go or by setting environment variables:
CAMERA_ID: ID of the camera to use (default: 0)CAMERA_WIDTH: Width of the camera frames (default: camera default)CAMERA_HEIGHT: Height of the camera frames (default: camera default)CAMERA_FPS: Frames per second (default: camera default)
You can adjust detection settings by modifying the configuration in pkg/detector/detector.go or by setting environment variables:
CONFIDENCE_THRESHOLD: Confidence threshold for human detection (default: 0.7)MODEL_PATH: Path to the model weights file (default: configs/detector/yolov4-tiny.weights)CONFIG_PATH: Path to the model configuration file (default: configs/detector/yolov4-tiny.cfg)CLASS_NAMES_PATH: Path to the class names file (default: configs/detector/coco.names)
GYANDOOT_ADDR: Address of the gRPC backend server (default: localhost:50051)DEVICE_ID: Unique identifier for the video source (default: webcam-0)SEND_INTERVAL_SECONDS: Minimum interval in seconds between sending frames when a human is detected (default: 5)
The project is structured as follows:
cmd/videoservice: Main entry point for the video processing servicepkg/camera: Camera interface and implementationpkg/detector: Human detection modulepkg/sender: Module for sending frames to the backend via gRPCproto/gyandoot: Protocol buffer definitions for gRPC communicationconfigs: Configuration files for the servicesdocker: Docker-related files
- The service is designed to be lightweight and efficient.
- For optimal performance, consider the following:
- Adjust frame resolution and frame rate according to your hardware capabilities
- Use a more powerful machine for processing multiple video streams
- Adjust JPEG compression quality for network transmission (configured in
pkg/sender/grpc_sender.go)
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.