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.
- 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.txtfile 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.
- Python 3.10 or newer
- A webcam connected to your computer
-
Setup .env
Create
.envfile at root folder following to the.env.sampleformat (Getting google api key: https://aistudio.google.com/app/apikey)GOOGLE_API_KEY=
-
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
-
Install the required packages: Create a
requirements.txtfile in the same directory with the content below, and then run:# requirements.txt ultralytics opencv-python PyAutoGUI sounddevice transformers torch scipy google-generativeai python-dotenvInstall the packages using pip:
pip install -r requirements.txt
The
ultralyticspackage will automatically install PyTorch and other necessary dependencies.
-
Run the script from your terminal:
python main.py
-
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.txtwill be created in the project directory, containing all possible classes the model can detect.
-
To stop the program, make sure the display window is active and press the 'q' key.
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")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.