This project uses YOLOv8 and OpenCV to detect and count objects, specifically focusing on identifying people and then detecting faces within the detected persons. It is implemented in Google Colab and leverages both Ultralytics YOLO and Haar Cascades.
- Upload and analyze an image using
Google Colab. - Detect all objects using the YOLOv8 model.
- Highlight and count the number of persons detected.
- Perform face detection on each person using OpenCV's Haar cascade classifier.
- Save the annotated image with bounding boxes and labels.
- Display the final output image in the notebook.
- Python
- OpenCV
- Ultralytics YOLOv8 (
yolov8n.pt) - Google Colab
- Haar Cascade Classifier
To run this project in Google Colab:
pip install ultralytics opencv-pythonNo manual installation of cv2 is needed in Colab — it is preinstalled.
.
├── counting_objects_face_detection.ipynb
├── output_with_boxes.jpg
└── README.md
- Upload an image in Google Colab.
- YOLOv8 detects all objects in the image.
- Each detected object is labeled and counted.
- If a detected object is a person, it is passed to the Haar Cascade classifier.
- Faces are detected inside the person region.
- Output is saved as
output_with_boxes.jpgwith all detections highlighted.
model = YOLO("yolov8n.pt")
results = model(image)
detections = results[0]
total_objects = len(detections.boxes)
# Face detection on detected 'person' boxes
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')- Switch to deep learning-based face detection for better accuracy.
- Add support for real-time webcam detection.
- Integrate a simple web UI using Gradio or Streamlit.
- Count and visualize other specific objects (e.g., vehicles, animals).
Deepak Misal 🔗 LinkedIn Profile
Samarth Bhutnal 🔗 LinkedIn Profile
Let me know if you'd like a version with Colab badges or want to auto-generate the sample image from your code output.
