AI / Raspberry Pi • FREE PROJECT

AI Bot

AI Bot circuit diagram
Advanced AI / Raspberry Pi ⏱ 2 Hours

Use a Raspberry Pi and a camera to detect and track a coloured object in real time with Python and OpenCV.

What You Will Learn

  • Setting up a Raspberry Pi camera with Python
  • Basics of computer vision with OpenCV
  • Detecting objects by colour (HSV thresholding)
  • Drawing bounding boxes and tracking a target in real time

Components Required

Raspberry Pi (3/4/5) Pi Camera Module or USB Webcam MicroSD Card Monitor / Remote Desktop
BUILD IT STEP BY STEP

Follow along and build with confidence.

Every step includes a visual reference and a plain-language explanation, so you always know what to do next.

STEP 01

Set Up the Raspberry Pi

🔧

Flash Raspberry Pi OS, connect the camera module (or a USB webcam), and confirm it works with a quick test capture before writing any AI code.

STEP 02

Install OpenCV

📦

Open a terminal on the Pi and install the OpenCV Python library, which handles image capture and processing for us.

bash
pip install opencv-python numpy
STEP 03

Write the Detection Code

💻

This script captures live video, converts each frame to HSV colour space, filters for a chosen colour range, and draws a box around the largest matching object.

python
import cv2
import numpy as np

cap = cv2.VideoCapture(0)

# Example range: tune these for your object's colour (here: orange)
lower_color = np.array([5, 150, 150])
upper_color = np.array([15, 255, 255])

while True:
    ret, frame = cap.read()
    if not ret:
        break

    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(hsv, lower_color, upper_color)

    contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    if contours:
        largest = max(contours, key=cv2.contourArea)
        if cv2.contourArea(largest) > 500:
            x, y, w, h = cv2.boundingRect(largest)
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
            cv2.putText(frame, "Object", (x, y - 10),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)

    cv2.imshow("AI Object Detection", frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
STEP 04

Test Your Project

✅

Run the script, hold up an object matching the tuned colour range in front of the camera, and confirm a green bounding box tracks it as it moves.

STUCK ON SOMETHING?

Troubleshooting & common fixes.

Camera window doesn't open.

Confirm the camera is enabled (raspi-config → Interface Options → Camera) and that no other program is currently using it.

No object is detected.

Your object's colour range likely differs from the example — use an HSV colour picker tool to find the right lower/upper values for your object and lighting.

Detection is laggy or slow.

Lower the camera resolution, or run on a Raspberry Pi 4/5 for better performance — the Pi 3 can struggle with larger frame sizes.

WANT GUIDED, HANDS-ON HELP?

Bring this project to life with a mentor.

Loved building this for free? Our instructor-led workshops go deeper with kits, live guidance and more advanced builds.

Plan a Workshop →
KEEP BUILDING

More free workshops.

💡

LED Blinking Using Arduino UNO

Learn how to control an LED using Arduino UNO by making it turn ON and OFF automatically. This is the basic concept behind indicator lights, warning lights, and signal systems used in real-world electronic devices.

View Free Project →
📏

Motion Detection Project

Use a PIR sensor to detect human movement and automatically switch ON an LED. This demonstrates the technology used in security systems, automatic lights, smart homes, and occupancy detection.

View Free Project →
🛤️

Automatic Street Light

Build a smart street light that automatically turns ON when it gets dark and OFF during daylight using an LDR sensor. This concept is commonly used in street lights, garden lights, parking areas, and energy-saving lighting systems.

View Free Project →