This repository contains two Python scripts demonstrating the use of hybrid object detection models combining YOLOv5 and Faster R-CNN. These models are fine-tuned to detect vehicles but can be adapted for other object detection tasks.
-
initial.py
- Implements a pipeline where YOLOv5 is used for initial object detection, followed by Faster R-CNN for enhanced detection within the cropped regions identified by YOLO.
- Focuses on processing images from a custom dataset and visualizing the results.
-
main.py
- Demonstrates a more integrated hybrid approach combining YOLOv5 and Faster R-CNN.
- Includes:
- IoU-based merging of detections from both models.
- Application of Non-Maximum Suppression (NMS) to refine final detections.
- Visualization of results from YOLO, Faster R-CNN, and the hybrid model side by side.
- Python 3.7 or above
- Required libraries:
torchultralyticstorchvisionopencv-python
Install the required libraries using:
pip install torch ultralytics torchvision opencv-python-
Setup:
- Ensure you have the pretrained YOLOv5 model file (
yolov5su.pt) and a custom dataset of images. - Update the
dataset_dirpath ininitial.pyand theimage_pathinmain.pyto point to your dataset.
- Ensure you have the pretrained YOLOv5 model file (
-
Run Scripts:
- Execute
initial.pyfor a basic detection pipeline:python initial.py
- Execute
main.pyfor hybrid detection with IoU-based merging and NMS:python main.py
- Execute
-
Visualization:
- View the results in separate windows displaying outputs from YOLO, Faster R-CNN, and the hybrid model.
- YOLOv5:
- Fast, real-time object detection with bounding boxes and confidence scores.
- Faster R-CNN:
- Accurate region-based detection for refining YOLO results.
- Hybrid Approach:
- Combines the strengths of both models using IoU for merging and NMS for final detection refinement.
- Ensure the image files are properly loaded; paths should be valid.
- You may adjust the confidence threshold and IoU values in
main.pyfor optimal results.
- YOLOv5 by Ultralytics.
- Faster R-CNN from PyTorch's
torchvisionlibrary.
Feel free to modify the scripts to suit your use case!