Real-Time Object Detection Using YOLOv8 and Python
Object detection is one of the most powerful capabilities in modern computer vision. With recent advances in deep learning, it is now possible to detect objects in images and video streams in real time. One of the most popular frameworks for this task is Ultralytics YOLOv8, which provides fast and accurate detection with very simple implementation in Python.
In this blog post, we will build a real-time object detection system using a webcam with Python and YOLOv8.
What is YOLO?
YOLO stands for You Only Look Once, a deep learning algorithm designed for real-time object detection. Unlike traditional detection systems that process an image multiple times, YOLO predicts bounding boxes and class probabilities in a single pass through the neural network, making it extremely fast.
YOLO models are widely used in applications such as:
Autonomous robots
Surveillance systems
Smart traffic monitoring
Industrial automation
AI-powered cameras
Prerequisites
Before running the code, install the required Python libraries.
pip install ultralytics opencv-python
Libraries used:
Ultralytics – for running YOLOv8 models
OpenCV – for handling webcam video and displaying results
YOLOv8 Model
YOLOv8 provides several pretrained models:
For real-time detection, we use yolov8n.pt, the lightweight nano model.
Python Implementation
Below is the complete Python code for real-time object detection using a webcam.
CODE;
from ultralytics import YOLO
import cv2
# Load YOLO model
model = YOLO(r"C:\Users\ransu\Documents\python\yolov8n.pt")
# Open webcam
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
if not ret:
break
# Run YOLO detection
results = model(frame)
# Draw detection results
annotated_frame = results[0].plot()
# Show output window
cv2.imshow("YOLO Object Detection", annotated_frame)
# Press 'q' to exit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release resources
cap.release()
cv2.destroyAllWindows()
Code Explanation
1. Import Libraries
from ultralytics import YOLO
import cv2
Ultralytics YOLO loads the trained object detection model.
OpenCV handles camera input and video display.
2. Load the YOLO Model
model = YOLO("yolov8n.pt")
This loads the pretrained YOLOv8 nano model which can detect 80 common object classes such as:
person
car
dog
chair
bottle
laptop
3. Start Webcam Capture
cap = cv2.VideoCapture(0)
0 represents the default webcam connected to the computer.
4. Run Object Detection
results = model(frame)
Each video frame is passed to the YOLO model, which detects objects and returns predictions.
5. Draw Bounding Boxes
annotated_frame = results[0].plot()
This automatically draws:
bounding boxes
object labels
confidence scores
on the detected objects.
6. Display Results
cv2.imshow("YOLO Object Detection", annotated_frame)
The processed frame is displayed in a window showing real-time detections.
7. Exit the Program
if cv2.waitKey(1) & 0xFF == ord('q'):
Press Q on the keyboard to close the program.
Example Output
When the program runs, the webcam feed will appear with bounding boxes around detected objects such as:
people
phones
bottles
laptops
Each object will display a label and confidence score.
Applications of This Project
This simple implementation can be extended to build many real-world systems:
Robot vision systems
Security monitoring
Smart retail analytics
Traffic object detection
Autonomous navigation
For robotics engineers, combining YOLO with motion control can allow robots to detect and interact with objects in their environment.
Conclusion
In this tutorial, we implemented a real-time object detection system using YOLOv8 and Python. With just a few lines of code, we were able to run a powerful deep learning model capable of detecting multiple objects in live video.
YOLO’s speed and simplicity make it an excellent tool for developers, researchers, and robotics engineers working on computer vision projects.
Comments
Post a Comment