NEW Free Trial Class — Book now, start learning this week. Reserve Your Spot →
Book FREE Trial Talk to a Mentor
AI & ML

How to Teach Your Computer to Recognise Objects: OpenCV in 30 Minutes

How to Teach Your Computer to Recognise Objects: OpenCV in 30 Minutes

Computer vision — giving computers the ability to "see" and understand images — is one of the most in-demand skills in AI right now. It's behind face unlock on your phone, the cameras that detect pedestrians in self-driving cars, and the quality-control systems in factories.

The good news: you can build a working object recognition system in about 30 minutes using Python and OpenCV. Here's how.

What is OpenCV?

OpenCV (Open Source Computer Vision Library) is a free, open-source library written in C++ with Python bindings. It's been around since 1999 and is used by engineers at Google, Intel, and research labs worldwide. For us, it's the fastest way to start doing real computer vision without building everything from scratch.

Setup: Install Python & OpenCV

If you haven't already, install Python 3.x from python.org. Then open your terminal (Command Prompt on Windows) and run:

pip install opencv-python numpy

That's it for setup. OpenCV installs in under a minute.

Project 1: Detect Your Face in Real Time (10 mins)

OpenCV includes pre-trained "Haar Cascade" classifiers — algorithms that know what faces, eyes, and other objects look like. You just load the classifier and apply it to each frame of your webcam feed.

import cv2

# Load face detector
face_cascade = cv2.CascadeClassifier(
    cv2.data.haarcascades + 'haarcascade_frontalface_default.xml'
)
cap = cv2.VideoCapture(0)   # 0 = your webcam

while True:
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.1, 4)
    
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
        cv2.putText(frame, 'Face', (x, y-10),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0,255,0), 2)
    
    cv2.imshow('Face Detector', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

Run this script and point your webcam at your face. A green rectangle will appear around your face in real time. You just built a face detector.

Project 2: Detect Coloured Objects (15 mins)

Colour detection is used in robotics to make a robot follow a coloured line or pick up a specific coloured object. We convert the frame to HSV (Hue-Saturation-Value) colour space, which makes colour filtering much easier than RGB.

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while True:
    _, frame = cap.read()
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    
    # Define blue colour range in HSV
    lower_blue = np.array([100, 150, 50])
    upper_blue = np.array([130, 255, 255])
    
    mask = cv2.inRange(hsv, lower_blue, upper_blue)
    result = cv2.bitwise_and(frame, frame, mask=mask)
    
    cv2.imshow('Detected Blue', result)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

Hold a blue object in front of your webcam — only the blue parts will appear in the output window. Change the HSV values to detect red, green, or any other colour.

Going Further: YOLOv8 for Multi-Object Detection

Haar Cascades are fast but limited. For detecting multiple object types (people, cars, phones, bottles) with high accuracy, the industry standard is YOLO (You Only Look Once). The latest version, YOLOv8, can be run with just a few lines of Python:

pip install ultralytics

YOLOv8 can detect 80 different object classes in real time on a normal laptop CPU. At KnowledgePitch, we cover YOLO in our Developer and Engineer-level curriculum — building systems that detect objects and trigger physical responses through Arduino.

Real Projects You Can Build

All of these are real projects built by students at KnowledgePitch. The plant disease detector was built by a Grade 11 student and presented at the district science fair.

Turn Reading Into Building

Come to MakersPitch Lab and build what you just read about — in a real lab, with real components, guided by expert mentors.