This project implements a simple object detection system using the pre-trained YOLOv5 model. It processes images from a small built-in dataset (e.g., scenes with people, vehicles, or animals), detects objects (from 80 COCO classes like "person", "car", "sports ball"), draws bounding boxes with class labels and confidence scores, counts detected objects by class, and displays/saves both the original and annotated images. The goal is to showcase end-to-end object detection, visualization, and basic tracking (via counting) without custom training.
- Language: Python 3.7+
- Deep Learning Framework: PyTorch (with YOLOv5 from Ultralytics)
- Computer Vision: OpenCV (for image loading, drawing, and display)
- Data Handling: NumPy (array operations), Requests (image downloads)
- Utilities: Collections (for object counting), Pathlib (file paths)
- Model: Pre-trained YOLOv5s (small variant; detects 80 classes from COCO dataset)
No additional training or large datasets required—uses public sample images.
-
Install Dependencies:
- Install PyTorch:
pip install torch torchvision torchaudio(for CPU). For GPU (e.g., CUDA 11.8):pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118(adjust for your CUDA version). - Install other packages:
pip install opencv-python requests numpy. - Optionally, clone YOLOv5 repo for local loading:
git clone https://github.com/ultralytics/yolov5 && cd yolov5 && pip install -r requirements.txt.
- Install PyTorch:
-
Save the Code:
- Copy the provided Python script into a file, e.g.,
object_detection.py.
- Copy the provided Python script into a file, e.g.,
-
Run the Script:
- Execute:
python object_detection.py. - It will download a selected sample image (change
selected_indexin code to pick from the dataset), run detection, print counts and details, saveoriginal_image.jpganddetected_image.jpg, and display images (if GUI is available; skips in headless environments like servers).
- Execute:
-
Outputs:
- Console: Selected image info, total detections, per-class counts, and bounding box details.
- Files: Original and detected images with annotations (bounding boxes, labels, overall count).
- GUI: Windows showing original and detected images (press any key to close).# ImageDetection