G-K0TMFLLLS9
0 like 0 dislike
12 views
ago in AI + Rasberry PI by

Line Crossing Counter with AI on Raspberry Pi

Hardware: Raspberry Pi 4 / 5
AI type: Computer Vision – Object Detection


Overview

This project shows how a Raspberry Pi can count entries and exits using AI.
Objects are detected by a lightweight AI model and counted when they cross a virtual line.
Everything runs locally, without cloud services or GPUs.


What you build

  • AI object detection on Raspberry Pi

  • Entry / exit counting using a virtual line

  • Fully offline processing


Required hardware

  • Raspberry Pi 4 or 5

  • Camera (Pi Camera or USB)

  • microSD card

  • Power supply


Software

  • Raspberry Pi OS

  • Python 3

  • OpenCV

  • Ultralytics YOLO


Installation

sudo apt update sudo apt upgrade sudo apt install python3-opencv python3-pip pip3 install ultralytics

Concept

A horizontal virtual line is placed in the video frame.
When an object’s center crosses the line, it is counted as entry or exit depending on direction.


Python code (copy-paste)

import cv2 from ultralytics import YOLO model = YOLO("yolov8n.pt") cap = cv2.VideoCapture(0) line_y = 240 prev_y = None entries = 0 exits = 0 while True: ret, frame = cap.read() if not ret: break results = model(frame, verbose=False) boxes = results[0].boxes if len(boxes) > 0: x1, y1, x2, y2 = boxes[0].xyxy[0] cy = int((y1 + y2) / 2) if prev_y is not None: if prev_y < line_y and cy >= line_y: entries += 1 elif prev_y > line_y and cy <= line_y: exits += 1 prev_y = cy cv2.line(frame, (0, line_y), (frame.shape[1], line_y), (255, 0, 0), 2) cv2.putText(frame, f"Entries: {entries}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0,255,0), 2) cv2.putText(frame, f"Exits: {exits}", (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0,0,255), 2) cv2.imshow("Line Counter", frame) if cv2.waitKey(1) & 0xFF == 27: break cap.release() cv2.destroyAllWindows()

Applications

  • People entry/exit counting

  • Store traffic analysis

  • Simple access monitoring


Limitations

  • Works best with one main object

  • Occlusions reduce accuracy


Conclusion

Raspberry Pi can perform practical AI analytics like entry and exit counting without expensive hardware.
This is a real, deployable AI use case on affordable devices.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.

30 questions

1 answer

3 comments

2 users

Welcome to Asky Q&A, where you can ask questions and receive answers from other members of the community.
Asky AI - Home
HeyPiggy Banner
...