Manually counting stock is tedious, error-prone, and eats into time you could spend actually running your business. With a Raspberry Pi, a camera, and a bit of object detection AI, you can build a Smart Shelf Inventory Counter that watches your shelves, counts products automatically, and updates your stock levels in real time.

This project is perfect for small shop owners, warehouse tinkerers, and makers who want to streamline operations without paying for expensive enterprise software.

What You’ll Need

Here’s your shopping list:

  • Raspberry Pi 4 (4GB or 8GB) — the 8GB model handles AI workloads more comfortably
  • Raspberry Pi Camera Module 3 or a good USB webcam (1080p minimum)
  • MicroSD card (32GB+) with Raspberry Pi OS (64-bit)
  • Official Raspberry Pi power supply
  • Camera mount or bracket to fix the camera above or in front of the shelf
  • LED light strip for consistent lighting
  • Wi-Fi or Ethernet connection
  • (Optional) Google Coral USB Accelerator — massively speeds up inference
  • (Optional) 7″ touchscreen for a local dashboard

You’ll also want a free Google Sheets account (or any database) to store your inventory data.

Step 1: Prepare the Raspberry Pi

Flash Raspberry Pi OS (64-bit) using Raspberry Pi Imager, boot up, and update the system:

bash
sudo apt update && sudo apt full-upgrade -y

Enable the camera via sudo raspi-config → Interface Options → Camera, then reboot.

Step 2: Install the AI Libraries

We’ll use TensorFlow Lite for lightweight object detection that runs smoothly on the Pi.

bash
sudo apt install python3-pip python3-opencv -y
pip3 install tflite-runtime numpy pillow gspread oauth2client requests

Test the camera:

bash
libcamera-still -o shelf.jpg

Step 3: Choose or Train Your Object Detection Model

You have two paths:

  1. Pre-trained model — Start with MobileNet SSD or EfficientDet-Lite, which can detect common items (bottles, boxes, cans) out of the box. Great for prototyping.
  2. Custom model — For your actual products, train a model using Google Teachable Machine, Roboflow, or TensorFlow Object Detection API. Snap 30–50 photos per product from different angles, label them, train, and export as .tflite.

Place the model file and labels in a project folder like ~/shelf-counter/.

Step 4: Position Your Camera

Mount the camera directly above or facing the shelf so every product is clearly visible. Consistency is key — lock it in place so the view never changes. Add an LED strip to eliminate shadows and flickering, which throw off detection accuracy.

Step 5: Write the Detection Script

Create shelf_counter.py that captures an image, runs detection, and counts each product class:

python
import tflite_runtime.interpreter as tflite
import numpy as np
from PIL import Image
import subprocess
from collections import Counter

interpreter = tflite.Interpreter(model_path="shelf_model.tflite")
interpreter.allocate_tensors()
labels = open("labels.txt").read().splitlines()

def capture():
    subprocess.run(["libcamera-still", "-o", "shelf.jpg", "-n"])
    return Image.open("shelf.jpg").resize((320, 320))

def detect(img):
    input_data = np.expand_dims(np.array(img, dtype=np.uint8), axis=0)
    interpreter.set_tensor(interpreter.get_input_details()[0]['index'], input_data)
    interpreter.invoke()
    classes = interpreter.get_tensor(interpreter.get_output_details()[1]['index'])[0]
    scores = interpreter.get_tensor(interpreter.get_output_details()[2]['index'])[0]
    detected = [labels[int(c)] for c, s in zip(classes, scores) if s > 0.5]
    return Counter(detected)

Step 6: Sync Counts to Google Sheets

Create a Google Cloud service account, enable the Sheets API, and share your spreadsheet with the service email. Then push counts live:

python
import gspread
gc = gspread.service_account(filename="creds.json")
sheet = gc.open("Inventory").sheet1

def update_sheet(counts):
    for i, (product, qty) in enumerate(counts.items(), start=2):
        sheet.update(f"A{i}", product)
        sheet.update(f"B{i}", qty)

Now your spreadsheet updates automatically every time the script runs — no manual counting, ever.

Step 7: Automate and Alert

Schedule the script to run every 10 minutes with cron:

bash
crontab -e
# Add:
*/10 * * * * /usr/bin/python3 /home/pi/shelf-counter/shelf_counter.py

Add low-stock alerts with a simple Telegram bot — whenever a product count drops below a threshold, send yourself a message so you know it’s time to restock.

Tips for Reliable Results

  • Lock the camera angle — any shift ruins consistency
  • Use even, diffused lighting to avoid glare on packaging
  • Retrain periodically as you add new products
  • Add a Coral USB Accelerator if detection feels sluggish
  • Keep shelves tidy — overlapping items confuse the model

Wrapping Up

You’ve just built a real-time, AI-powered inventory system for a fraction of what commercial solutions cost. Expand it with multiple cameras for larger shops, build a web dashboard, or integrate it with your POS system for end-to-end automation.

Small business, big brain. 🧠📦


Enjoyed this build? Follow @raspitips on Instagram for more Raspberry Pi projects, tips, and tutorials.

Leave A Comment

Related Posts