Engineering · Raspberry Pi · Project 31 of 47 · Camera and vision · Intermediate
Count objects with the camera
Every time the red ball crosses the middle line of the picture, the count goes up by one, and an LED bar shows the count. The same idea counts parcels, people or cars.

Privacy and safety. A camera records people. Tell everyone who lives in or visits the place, keep the pictures private, and delete old ones. Switch the Pi off before you plug in or remove the camera ribbon cable.
Parts in this circuit
| Part | Type |
|---|---|
| Pi Camera | Pi Camera Module 3 |
| Count display | LED bar |
Wiring, pin by pin
Red = 5 V, orange = 3.3 V, black = GND, other colours = signals. “Pin” is the position on the 40-pin header; the GPIO number is what the code uses. Power the Pi from a 5.1 V supply and wire with the power off.
| # | Colour | From (part and leg) | To (header pin) |
|---|---|---|---|
| 1 | Coloured | Count display - Signal | Pin 11 (GPIO17) |
| 2 | Coloured | Count display - Signal | Pin 13 (GPIO27) |
| 3 | Coloured | Count display - Signal | Pin 15 (GPIO22) |
| 4 | Coloured | Count display - Signal | Pin 16 (GPIO23) |
| 5 | Coloured | Count display - Signal | Pin 18 (GPIO24) |
| 6 | Black | Count display - GND | Pin 6 (GND) |
Python code (gpiozero)
from picamera2 import Picamera2
from gpiozero import LEDBarGraph
from time import sleep
cam = Picamera2()
cam.configure(cam.create_preview_configuration())
cam.start()
sleep(1)
bar = LEDBarGraph(17, 27, 22, 23, 24)
count = 0
was_left = None # where the ball was last time
while True:
frame = cam.capture_array()
spot = frame.find_color("red")
if spot is None:
was_left = None # nothing to follow
else:
left = spot[0] < frame.width / 2
if was_left is True and not left:
count += 1 # it crossed the middle line, left to right
bar.value = min(count, 5) / 5
print("Count:", count)
was_left = left
sleep(0.1)
How the code works
- was_left remembers on which side of the middle line the ball was in the last picture.
- A count happens only when it changes from left to right. A ball that stays on one side counts once at most.
- Setting was_left to None when nothing is seen stops false counts when a new ball appears.
- bar.value = count / 5 lights that fraction of the LEDs.
You will learn
- Remembering the previous state
- Detecting a crossing, not just a position
- Showing a number on an LED bar
Try this
- Press Walk left to right three times and watch the bar.
- Press Walk right to left. Does the count change? Change the code to count both ways.
- Remove the ball, put it in again on the right side, and see why nothing is counted.
On a real Pi
- Real counting lines are placed where objects cannot stop or bunch up. Use a high shutter speed for fast belts.
- If objects touch each other the camera sees one blob. Counting needs a good light and a clear background.
- For a simple belt, a break-beam sensor is cheaper and more reliable. Use the camera when you must also check colour or size.
Parts to buy
The links open a search on each store, so you see what is in stock today. Check the model, voltage and price before you order.
| # | Item | Qty | Buy / compare |
|---|---|---|---|
| 1 | Raspberry Pi 4 Model B about 4000 to 9000 INR | 1 | Robu.in Amazon.in ThinkRobotics |
| 2 | Official 15 W USB-C power supply (5.1 V, 3 A) Do not use an old phone charger | 1 | Robu.in Amazon.in ThinkRobotics |
| 3 | microSD card 32 GB, class 10 / A1 (with Raspberry Pi OS) And a card reader to write it | 1 | Robu.in Amazon.in ThinkRobotics |
| 4 | 330 ohm resistor, 1/4 W One per LED | 5 | Robu.in Amazon.in ThinkRobotics |
| 5 | LED bar graph module (5 LEDs) | 1 | Robu.in Amazon.in ThinkRobotics |
| 6 | Raspberry Pi Camera Module 3 (12 MP, autofocus) Or a Camera Module 2 / HQ camera | 1 | Robu.in Amazon.in ThinkRobotics |
| 7 | CSI camera ribbon cable, 15-pin, 30 cm Check the port size of your board | 1 | Robu.in Amazon.in ThinkRobotics |
| 8 | Camera mount or small tripod | 1 | Robu.in Amazon.in ThinkRobotics |
| 9 | Dupont jumper wires, male-to-female, 20 cm (pack of 40) | 1 | Robu.in Amazon.in ThinkRobotics |
| 10 | Breadboard 830 points For resistors and shared GND / 5 V | 1 | Robu.in Amazon.in ThinkRobotics |
This is a learning tool. The simulator checks the program logic, not the electrical design. Never put more than 3.3 V on a GPIO pin.