Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

Real-Time Webcam Object Detection with YOLOv12

This project is a part of SAIG K-Engineering 2025 Lab Tour project.

This project demonstrates how to use a pre-trained YOLOv12 model to perform real-time object detection on a live webcam feed. The script captures video from the default webcam, runs inference using the ultralytics library, draws bounding boxes and labels on detected objects, and displays the output in a full-screen window.

Features

  • Real-Time Detection: Utilizes the efficient YOLOv12 model for smooth, real-time object detection.
  • Webcam Integration: Captures video directly from your computer's webcam using OpenCV.
  • Model Flexibility: Easily switch between different YOLOv12 models (e.g., yolov12n.pt, yolov12s.pt, or a custom-trained model).
  • Class List Export: Automatically generates a class_names.txt file listing all detectable classes and their corresponding IDs from the loaded model.
  • Full-Screen Display: The output video window is resized to fit the entire screen for an immersive view.
  • Customizable Detections: The code can be easily modified to detect only a specific set of target classes.

Prerequisites

  • Python 3.10 or newer
  • A webcam connected to your computer

Installation

  1. Setup .env

    Create .env file at root folder following to the .env.sample format (Getting google api key: https://aistudio.google.com/app/apikey)

    GOOGLE_API_KEY=
  2. Create a virtual environment (recommended): It's good practice to create a virtual environment to keep project dependencies isolated.

    # For Windows
    python -m venv venv
    venv\Scripts\activate
    
    # For macOS/Linux
    python3 -m venv venv
    source venv/bin/activate
  3. Install the required packages: Create a requirements.txt file in the same directory with the content below, and then run:

    # requirements.txt
    ultralytics
    opencv-python
    PyAutoGUI
    sounddevice
    transformers
    torch
    scipy
    google-generativeai
    python-dotenv
    

    Install the packages using pip:

    pip install -r requirements.txt

    The ultralytics package will automatically install PyTorch and other necessary dependencies.

Usage

  1. Run the script from your terminal:

    python main.py
  2. How it works:

    • The script will initialize your default webcam. You may need to grant permission the first time.
    • A window titled "YOLOv12 Webcam Detection" will open, displaying the webcam feed with detected objects highlighted by green bounding boxes.
    • Each box will be labeled with the class name and the confidence score (e.g., person 0.95).
    • A file named class_names.txt will be created in the project directory, containing all possible classes the model can detect.
  3. To stop the program, make sure the display window is active and press the 'q' key.

Customization

Changing the YOLO Model

You can use any other YOLOv12 model by changing this line in the script. The first time you specify a new model, ultralytics will download it automatically.

# From
model = YOLO("yolov12n.pt")

# To (for the small model)
model = YOLO("yolov12s.pt")

# Or to a custom-trained model
model = YOLO("path/to/your/custom_model.pt")

Filtering Detected Classes

The current script is set up to detect all classes available in the model. If you want to detect only specific classes (e.g., 'person', 'car', 'bottle'), you can modify the code.

Find these lines in the script:

# Set the classes you want to detect (e.g., person=0, car=2, dog=16 for COCO)
target_class_names = ['person', 'car', 'bottle']

# ...

# target_class_ids = [i for i, name in all_class_names.items() if name in target_class_names]
target_class_ids = [i for i, name in all_class_names.items()]

To enable filtering, uncomment the first target_class_ids line and comment out or delete the second one:

# ...

target_class_ids = [i for i, name in all_class_names.items() if name in target_class_names]
# target_class_ids = [i for i, name in all_class_names.items()]

Now, the script will only draw bounding boxes for the classes defined in the target_class_names list.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages