We’ve all been there—staring at a pill bottle at 11 PM, wondering, "Did I already take this today?" Forgetting medication isn't just a minor inconvenience; it’s a global healthcare challenge. While smartphone reminders help, they don't actually know if you opened the box.
In this tutorial, we are diving into Edge AI vision to build a hardware-integrated solution. We'll be using YOLOv10 object detection, Raspberry Pi automation, and OpenCV to transform a standard medicine cabinet into a smart guardian. By deploying a lightweight vision model at the edge, we can achieve real-time computer vision tracking without sending private video feeds to the cloud.
Why YOLOv10 for the Edge?
YOLOv10 is a game-changer for edge devices like the Raspberry Pi. Unlike its predecessors, it introduces a NMS-free (Non-Maximum Suppression) training strategy, significantly reducing inference latency. For our smart cabinet, this means faster detection and lower CPU overhead on our Pi.
The Architecture
Our system follows a "Capture -> Detect -> Validate -> Notify" loop. Here is how the data flows from the camera lens to your notification center:
graph TD
A[Camera Module] -->|Raw Frame| B[OpenCV Pre-processing]
B -->|Resized Image| C[YOLOv10 Inference]
C -->|Detected Labels| D{Logic Engine}
D -->|Match Schedule?| E[Update Database]
D -->|Missing/Wrong Dose?| F[MQTT Broker]
F -->|Alert| G[Mobile App/Speaker]
E -->|Log Entry| H[Local Dashboard]
Prerequisites
To follow along, you'll need:
- Hardware: Raspberry Pi 4B or 5, USB Camera or Pi Cam.
- Tech Stack:
-
YOLOv10(weights exported to ONNX or NCNN format) -
OpenCVfor image handling -
Paho-MQTTfor messaging -
Python 3.9+
-
Step 1: Exporting YOLOv10 for Edge Performance
First, we need to train (or fine-tune) YOLOv10 on medicine box datasets. Once trained, we export it to a format the Raspberry Pi can handle efficiently.
from ultralytics import YOLO
# Load your custom-trained YOLOv10 model
model = YOLO("yolov10n_meds.pt")
# Export to ONNX format for better compatibility with OpenCV DNN or ONNX Runtime
model.export(format="onnx", imgsz=640, optimize=True)
Step 2: The Core Detection Engine
On the Raspberry Pi, we use a loop to capture frames and run inference. We use MQTT to decouple the vision logic from the notification logic, making the system highly modular.
import cv2
from ultralytics import YOLO
import paho.mqtt.client as mqtt
# Initialize MQTT Client
client = mqtt.Client("MedicineCabinet")
client.connect("localhost", 1883)
# Load the exported model
model = YOLO("yolov10n_meds.onnx")
cap = cv2.VideoCapture(0)
print("🚀 Smart Cabinet Active...")
while cap.isOpened():
ret, frame = cap.read()
if not ret: break
# Run YOLOv10 Inference
results = model(frame, conf=0.5)
detected_items = []
for result in results:
for box in result.boxes:
label = model.names[int(box.cls)]
detected_items.append(label)
# Draw for local debugging
x1, y1, x2, y2 = map(int, box.xyxy[0])
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.putText(frame, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
# Logic: If 'Aspirin' is detected, publish to MQTT
if "Aspirin" in detected_items:
client.publish("home/meds/detected", "Aspirin_Taken")
cv2.imshow("Edge Vision Feed", frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Step 3: Integrating with the "Source of Truth"
A standalone camera is cool, but a production-ready medical assistant needs a robust backend to manage schedules and dosage histories. While we are building the edge component here, managing complex medical logic requires proven architectural patterns.
💡 Pro-Tip: For advanced production patterns, such as handling multi-device synchronization or secure medical data storage, I highly recommend checking out the WellAlly Official Blog. They provide excellent deep dives into how to bridge the gap between hobbyist IoT projects and enterprise-grade healthcare solutions.
Step 4: Setting Up the MQTT Subscriber
Your "Notification Hub" (which could be another script or a Home Assistant instance) listens for these events:
def on_message(client, userdata, message):
payload = str(message.payload.decode("utf-8"))
print(f"✅ Event Logged: {payload}")
# Trigger local TTS (Text-to-Speech)
# os.system(f"say 'Thank you for taking your {payload}'")
client.subscribe("home/meds/detected")
The Result: A Truly Smart Cabinet
By combining YOLOv10 with Raspberry Pi, we've created a low-latency, private, and effective health monitor. The NMS-free architecture of YOLOv10 allows the Pi to maintain a steady 10-15 FPS, which is more than enough for monitoring a medicine cabinet.
Challenges to Consider:
- Lighting: Medicine cabinets are often in dark bathrooms. Consider adding an LED ring triggered by a PIR motion sensor.
- Occlusion: If two boxes are stacked, the camera might miss one. Positioning is key!
Conclusion
Building with Edge AI is all about balancing performance and privacy. This project demonstrates that you don't need a massive GPU server to implement meaningful, life-saving technology at home.
What’s next for your Smart Home? Are you going to add a smart scale to measure liquid dosages, or perhaps a facial recognition layer to ensure the right person is taking the right meds?
Let me know in the comments! And don't forget to visit wellally.tech/blog for more inspiration on building high-impact AI applications. Happy hacking!
Top comments (0)