Practice and reinforce the concepts from Lesson 17
Total Time: 90-120 minutes
Activity Type: :computer: Coding & Implementation
Objective: Install the YOLO package and verify installation
Steps to complete:
py -m pip install -r requirements.txt
python3 -m pip install -r requirements.txt
:bulb: Installation Success If successful, you should see a version number printed in your terminal (may vary depending on install date). If you encounter errors, make sure you have Python 3.7+ installed and pip is up to date.
Objective: Explore what objects YOLO can detect
Steps to complete:
yolo_classes.py
from ultralytics import YOLO
# Load the trained model
model = YOLO('yolov8n.pt')
# Get class names
class_names = model.names
# Print class names
print(class_names)
# Save class names to file
with open('class_names.txt', 'w') as f:
for index in class_names:
f.write(f'{index}: {class_names[index]}\n')
class_names.txt
file to see all 80 object classes:information_source: About YOLO Classes YOLOv8 can identify 80 different object classes including people, animals, vehicles, and everyday objects. Learn more about the dataset at the YOLOv8 documentation.
Objective: Implement object detection on a static image
Steps to complete:
outing.jpg
in your project folderimage_detection.py
from ultralytics import YOLO
import cv2
import math
# Load model
model = YOLO('yolov8n.pt')
class_name = model.names
# Load image
img = cv2.imread('outing.jpg')
# Detect objects
results = model(img)
# Draw detections
for r in results:
for box in r.boxes:
# Bounding box
x1, y1, x2, y2 = box.xyxy[0]
cv2.rectangle(img, (int(x1), int(y1)), (int(x2), int(y2)), (255, 0, 255), 3)
# Confidence and class name
conf = math.ceil((box.conf[0]*100))/100
cls = int(box.cls[0])
# Label
label = f'{class_name[cls]}{conf}'
cv2.putText(img, label, (int(x1), int(y1)-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
# Display image
cv2.imshow('Image', img)
cv2.waitKey(0)
:bulb: Common Issues
- No detections? Try using an image with clearly visible objects like people, cars, or animals
- Low confidence scores? This is normal for small or partially visible objects
- Error loading image? Check that
outing.jpg
is in the same folder as your Python file
Objective: Apply YOLO detection to video files
Steps to complete:
video.mp4
in your project foldervideo_detection.py
from ultralytics import YOLO
import cv2
import math
# Load model
model = YOLO('yolov8n.pt')
class_name = model.names
# Load video
video = cv2.VideoCapture('video.mp4')
while True:
ret, frame = video.read()
if not ret:
break
# Detect objects
results = model(frame)
# Draw detections
for r in results:
for box in r.boxes:
x1, y1, x2, y2 = box.xyxy[0]
cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (255, 0, 255), 2)
conf = math.ceil((box.conf[0]*100))/100
cls = int(box.cls[0])
label = f'{class_name[cls]}{conf}'
cv2.putText(frame, label, (int(x1), int(y1)-10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
cv2.imshow('Video', frame)
if cv2.waitKey(1) == ord('q'):
break
video.release()
cv2.destroyAllWindows()
:bulb: Performance Tips
- For faster processing, use smaller video files or lower resolution
- If the video runs slowly, try using
yolov8s.pt
instead ofyolov8n.pt
- The first run may be slower as the model loads
Objective: Create a real-time object detection system using your webcam
Steps to complete:
webcam_detection.py
from ultralytics import YOLO
import cv2
import math
# Load model
model = YOLO('yolov8n.pt')
class_name = model.names
# Initialize webcam
cap = cv2.VideoCapture(0)
while True:
ret, frame = cap.read()
# Detect objects
results = model(frame)
# Draw detections
for r in results:
for box in r.boxes:
x1, y1, x2, y2 = box.xyxy[0]
cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (255, 0, 255), 2)
conf = math.ceil((box.conf[0]*100))/100
cls = int(box.cls[0])
label = f'{class_name[cls]}{conf}'
cv2.putText(frame, label, (int(x1), int(y1)-10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 2)
cv2.imshow('Webcam', frame)
if cv2.waitKey(1) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
:warning: Privacy Notice This application uses your webcam. Make sure you're in a suitable environment before running this code. The detection runs locally on your computer - no data is sent anywhere.
Objective: Create a specialized counter for specific object types
Challenge Requirements:
Steps to complete:
advanced_image.jpg
advanced_counter.py
with the following code:# Advanced-1.py
from ultralytics import YOLO
import cv2
model = YOLO('yolov8n.pt')
img = cv2.imread('advanced_image.jpg')
# Class IDs for person and cat
person_id = list(model.names.keys())[list(model.names.values()).index('person')]
cat_id = list(model.names.keys())[list(model.names.values()).index('cat')]
results = model(img)
person_count = 0
cat_count = 0
for r in results:
for box in r.boxes:
cls = int(box.cls[0])
if cls == person_id:
person_count += 1
elif cls == cat_id:
cat_count += 1
print(f"People: {person_count}, Cats: {cat_count}")
:bulb: Extension Ideas
- Add more object types to count (dogs, cars, etc.)
- Display the counts on the image itself
- Create a GUI interface for selecting which objects to count
- Save detection results to a CSV file
Installation Errors
pip install --upgrade pip
Model Download Issues
Detection Not Working
Performance Problems
yolov8n.pt
(nano) is fastest:warning: Important: Submit Your Work! Submission Deadline: End of class
What to submit:
- All your Python files (.py)
- Screenshot of your detection results
- Any modified code for the advanced challenge
Submit through this form:
:emoji: Exercise Submission Form
:information_source: Need Help? If you're stuck:
- Review the error messages carefully
- Check the troubleshooting guide above
- Ask your instructor or classmates
- Consult the YOLO documentation