Real-time wild-animal intrusion detection using a custom-trained YOLOv5 model, with hardware alert triggers over a serial link.
A computer-vision system that watches a camera feed (webcam, video file, or stream) and detects wild animals in real time so that nearby personnel can be warned before an intrusion becomes dangerous. It is built on the YOLOv5 detection pipeline with custom-trained weights (best.pt), and distinguishes wild animals from domestic ones. When a wild animal is detected, the system fires an alert signal to external hardware (e.g. a microcontroller driving sirens/deterrents) over a serial port.
detect.pyruns YOLOv5 inference on the input source (defaults: webcam0, custom weightsbest.pt, confidence threshold0.65).- Each frame is annotated with bounding boxes and class labels, and results can be saved as images/video, text labels, or CSV.
- When one of the trained wild-animal classes is detected — Tiger, Bison, Elephant, Leopard, Lion, Deer — a class-specific single-character code (
T,B,E,L,M,D) is written to a serial port (9600 baud, seeserial_test.py), where connected hardware can trigger the appropriate alert. - A separate image-classification stage lives in
classify/with its own trained weights (classify/best_classify.pt).
- Custom-trained YOLOv5 weights for six wild-animal classes (Tiger, Bison, Elephant, Leopard, Lion, Deer)
- Real-time inference on webcam, video files, RTSP/HTTP streams, image folders, or screenshots
- Serial-port alert triggers (pyserial) so a microcontroller can react per detected species
- Annotated output saved to
runs/detect/(video/images), with optional--save-txtand--save-csvexports - Full YOLOv5 toolchain included:
train.pyfor training on custom datasets,val.pyfor evaluation,export.pyfor ONNX/TensorRT and other export formats - Sample test videos (
test.mp4,v10.mp4) and dataset images inimages/
- Detection: YOLOv5 (PyTorch), Ultralytics utilities
- Vision: OpenCV
- Hardware link: pyserial (UART, 9600 baud)
- Models: custom-trained
best.pt(detection),classify/best_classify.pt(classification), plus stockyolov5s.pt
# Install the standard YOLOv5 dependencies (PyTorch, OpenCV, ultralytics, etc.)
# plus pyserial for the hardware alert link
pip install torch torchvision opencv-python ultralytics pyserial
# Run detection on the default webcam with the custom wildlife weights
python detect.py
# Or on a sample video
python detect.py --weights best.pt --source test.mp4
# Validate / retrain
python val.py --weights best.pt
python train.py --weights yolov5s.pt --data <your-dataset>.yamlNote:
detect.pyimports the serial sender at startup, which opens a serial port (configured asCOM4inserial_test.py). To run with the hardware alerts, connect your microcontroller and adjust the port name for your machine.
Built by Manu Rathan Setty