Zbotic Logo Zbotic Logo
  • Home
  • Shop
  • Sale
  • 3D Print Service
  • PCB Service
  • B2B
  • Blogs
  • Contact Us
0 0

View Wishlist Add all to cart

0 0
0 Shopping Cart
Shopping cart (0)
Subtotal: ₹0.00

View cartCheckout

  • Shop
  • About Us
  • Contact Us
  • Reseller
  • Blogs
020 69134444
1800 209 0998
[email protected]
Help Desk
Facebook Twitter Instagram Linkedin YouTube
Zbotic Logo Zbotic Logo
0 0

View Wishlist Add all to cart

0 0
0 Shopping Cart
Shopping cart (0)
Subtotal: ₹0.00

View cartCheckout

All departments
  • 3D Print Service
  • 3D Printer
  • Batteries & Chargers
  • Development Boards
  • Drone Parts
  • EBike parts
  • Sensor Modules
  • Electronic Components
  • Electronic Modules
  • IoT and Wireless
  • Mechanical Parts and Workbench Tools
  • Motors & Drivers & Pumps & Actuators
  • DIY and Robot Kits
  • Show more
  • Home
  • Shop
  • Sale
  • 3D Print Service
  • PCB Service
  • B2B
  • Blogs
  • Contact Us
Return to previous page
Home Camera & Vision Modules

Machine Vision with OpenCV: Raspberry Pi Object Detection Guide

Machine Vision with OpenCV: Raspberry Pi Object Detection Guide

April 1, 2026 /Posted by / 0

Running OpenCV on a Raspberry Pi for object detection opens up countless applications, from industrial quality inspection to smart doorbell cameras. With the Raspberry Pi 4 or 5 providing sufficient processing power and OpenCV’s optimised libraries, you can achieve real-time object detection at 10-30 fps depending on the model complexity. This guide covers everything from installation to deploying face detection, colour tracking, and TensorFlow Lite-based object detection on your Pi.

Table of Contents

  • Getting Started with OpenCV on Pi
  • Installation and Setup
  • Face Detection with Haar Cascades
  • Colour Object Tracking
  • TensorFlow Lite Object Detection
  • Performance Optimisation Tips
  • Frequently Asked Questions
  • Conclusion

Getting Started with OpenCV on Pi

OpenCV (Open Source Computer Vision Library) provides over 2500 algorithms for image processing, feature detection, and machine learning. On the Raspberry Pi, OpenCV combined with a camera module creates a portable, affordable computer vision platform that rivals systems costing 10x more.

🛒 Recommended: Arduino Uno R3 Development Board — Pair with Raspberry Pi for vision projects that need to control motors, LEDs, or other hardware based on detection results.

Installation and Setup

# Update system
sudo apt update && sudo apt upgrade -y

# Install OpenCV (pre-built package, much faster than compiling)
sudo apt install python3-opencv -y

# Install additional Python packages
pip3 install numpy imutils tflite-runtime

# Verify installation
python3 -c "import cv2; print(cv2.__version__)"
# Should print 4.x.x

# Test camera
python3 -c "
import cv2
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
print('Camera working:', ret, 'Shape:', frame.shape)
cap.release()
"

Face Detection with Haar Cascades

Haar Cascade classifiers are the fastest method for face detection on Raspberry Pi, achieving 15-25 fps at 640×480 resolution. OpenCV includes pre-trained cascades for faces, eyes, and smiles.

import cv2

face_cascade = cv2.CascadeClassifier(
    cv2.data.haarcascades + 'haarcascade_frontalface_default.xml'
)

cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)

while True:
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    
    faces = face_cascade.detectMultiScale(
        gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30)
    )
    
    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 Detection', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

Colour Object Tracking

HSV colour space-based tracking is extremely fast (30+ fps) and ideal for industrial sorting, robotics, and sports analysis. Convert the frame to HSV, apply a colour range mask, find contours, and track the largest contour.

import cv2
import numpy as np

# Track red objects (adjust HSV range for your target)
lower_red = np.array([0, 120, 70])
upper_red = np.array([10, 255, 255])

cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(hsv, lower_red, upper_red)
    
    # Clean up mask
    mask = cv2.erode(mask, None, iterations=2)
    mask = cv2.dilate(mask, None, iterations=2)
    
    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), radius = cv2.minEnclosingCircle(largest)
            cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 0), 2)
    
    cv2.imshow('Colour Tracking', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
🛒 Recommended: Arduino Mega 2560 R3 Board — For complex vision-controlled systems needing many I/O pins for servos, motors, and additional sensors.

TensorFlow Lite Object Detection

For detecting multiple object types (people, cars, animals), TensorFlow Lite with a pre-trained SSD MobileNet model provides the best balance of accuracy and speed on Raspberry Pi. Expect 5-10 fps on Pi 4 at 300×300 input resolution.

import cv2
import numpy as np
import tflite_runtime.interpreter as tflite

# Load TFLite model
interpreter = tflite.Interpreter(model_path='ssd_mobilenet_v2.tflite')
interpreter.allocate_tensors()

input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

# Load labels
with open('coco_labels.txt', 'r') as f:
    labels = [line.strip() for line in f.readlines()]

cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    input_data = cv2.resize(frame, (300, 300))
    input_data = np.expand_dims(input_data, axis=0).astype(np.uint8)
    
    interpreter.set_tensor(input_details[0]['index'], input_data)
    interpreter.invoke()
    
    boxes = interpreter.get_tensor(output_details[0]['index'])[0]
    classes = interpreter.get_tensor(output_details[1]['index'])[0]
    scores = interpreter.get_tensor(output_details[2]['index'])[0]
    
    h, w = frame.shape[:2]
    for i in range(len(scores)):
        if scores[i] > 0.5:
            ymin, xmin, ymax, xmax = boxes[i]
            x1, y1 = int(xmin * w), int(ymin * h)
            x2, y2 = int(xmax * w), int(ymax * h)
            label = labels[int(classes[i])]
            cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
            cv2.putText(frame, f'{label}: {scores[i]:.0%}',
                       (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,255,0), 2)
    
    cv2.imshow('Object Detection', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
🛒 Recommended: DHT11 Temperature and Humidity Sensor — Add environmental data logging to your vision system for correlating detections with conditions.

Performance Optimisation Tips

  • Reduce resolution: Process at 320×240 instead of 640×480; detection accuracy barely drops but speed doubles
  • Use threading: Separate camera capture and processing into different threads to eliminate frame buffering delays
  • Skip frames: Process every 2nd or 3rd frame for real-time display while maintaining smooth video
  • ROI processing: Define a region of interest and only run detection within that area
  • Use Pi 5: The Pi 5’s improved CPU provides 40-60% faster inference than Pi 4
🛒 Recommended: Waveshare ESP32-S3-Nano Development Board — For edge AI applications where a full Raspberry Pi is overkill, the ESP32-S3 can run basic TFLite models.

Frequently Asked Questions

Can Raspberry Pi handle real-time object detection?

Yes. With TFLite and SSD MobileNet, Raspberry Pi 4 achieves 8-12 fps and Pi 5 achieves 15-20 fps at 300×300 input. For faster inference, add a Coral USB Accelerator (TPU) which pushes performance to 30+ fps.

Which Pi model is best for OpenCV?

Raspberry Pi 5 (8GB) is the best choice for serious OpenCV work. The Pi 4 (4GB) is adequate for lighter tasks. Avoid Pi 3 and Zero for real-time video processing as they lack the necessary CPU and RAM.

Can I use a USB webcam instead of the Pi Camera?

Yes. Any V4L2-compatible USB camera works with OpenCV via cv2.VideoCapture(0). However, the CSI camera has lower latency and CPU overhead because it uses the Pi’s dedicated camera interface and hardware ISP.

How do I run OpenCV headless (without display)?

Replace cv2.imshow() with cv2.imencode() to save frames, or stream via Flask/FastAPI to a web browser. This is common for deployed systems where the Pi runs as a headless server.

Conclusion

OpenCV on Raspberry Pi is a powerful and affordable platform for machine vision projects. From simple face detection to advanced TensorFlow Lite object detection, the Pi handles a remarkable range of computer vision tasks. Start with the simpler Haar Cascade examples, progress to colour tracking, and then tackle deep learning-based detection as you gain confidence. Find Raspberry Pi boards, cameras, and accessories at Zbotic to start your vision project today.

Tags: AI, camera, object detection, OpenCV, Raspberry Pi
Share Post
  • Facebook
  • Linkedin
  • Whatsapp
Solar Battery Monitor with Ard...
blog solar battery monitor with arduino track your system performance 612643
blog smart india hackathon project ideas components and build guide 612647
Smart India Hackathon Project ...

Related posts

Svg%3E
Read more

Endoscope Camera Module: PCB Inspection and Industrial Use

April 1, 2026 0
An endoscope camera module is an invaluable tool for PCB inspection, industrial equipment maintenance, and quality control tasks where direct... Continue reading
Svg%3E
Read more

Number Plate Recognition System: ESP32-CAM ANPR Project India

April 1, 2026 0
Building a number plate recognition system with ESP32-CAM is an affordable approach to automatic number plate recognition (ANPR) for Indian... Continue reading
Svg%3E
Read more

Arducam vs Raspberry Pi Camera: Which Camera Module to Choose

April 1, 2026 0
Choosing between Arducam and Raspberry Pi camera modules is one of the first decisions for any vision project. Both connect... Continue reading
Svg%3E
Read more

360-Degree Camera Stitching Project with OpenCV and Pi

March 11, 2026 0
Creating a 360-degree camera using OpenCV image stitching with Raspberry Pi is an ambitious computer vision project that combines multiple... Continue reading
Svg%3E
Read more

Webcam Streaming with Flask and Raspberry Pi: Web Interface

March 11, 2026 0
Building a webcam streaming web interface with Flask and Raspberry Pi is one of the most practical home server projects.... Continue reading

Add comment Cancel reply

Your email address will not be published. Required fields are marked

Facebook Twitter Instagram Pinterest Linkedin Youtube

Get the latest deals and more.

Download on Google Play Download on the App Store

Call us: 020 69134444 / 1800 209 0998

Monday - Saturday 09:30 AM - 06:00 PM
For Technical Supports Email: [email protected]
For Sales / Enquiries Email: [email protected]

  • My Account

    • Cart

    • Wishlist

    • Checkout

    • My Orders

    • Track Order

    • My Account

  • Information

    • FAQs

    • Blogs

    • Career

    • About Us

    • Contact Us

    • Payment Options

  • Policies

    • Privacy Policy

    • Terms & Conditions

    • GST Input Tax Credit

    • Shipping Return Policy

    • E-Waste Collection Points

    • Our Sitemap

© Zbotic.in is registered trademark of Moxie Supply Pvt Ltd – All Rights Reserved
Login
Use Phone Number
Use Email Address
Not a member yet? Register Now
Reset Password
Use Phone Number
Use Email Address
Register
Already a member? Login Now