在这篇文章中,我们将详细阐述如何构建一个由Raspberry Pi 5和AI摄像头支持的智能冰箱库存管理系统。该系统通过安装在冰箱侧面的超声波传感器,自动判断何时启动产品检测以及何时发送库存更新。借助定制的YOLOv8n模型(通过Roboflow导出为IMX500兼容格式),摄像头能够识别关键产品——包括奶酪、可乐、鸡蛋、番茄酱、牛奶、pittas和schnitzels——并根据它们在画面中的位置进行分类。生成的"进"(在冰箱内)和"出"(需补货)清单,通过基于SocketIO的Flask服务器实时传输到React仪表盘,帮助用户随时掌握库存状态和补货需求。
"井然有序的冰箱,是健康生活的第一步。"——匿名
项目简介: AI冰箱库存管理的创新愿景
想象一下,当你打开冰箱时,一个AI系统能即时告知可用物品和缺失清单。我们的项目利用传感器触发器实现了自动库存追踪:当超声波传感器检测到冰箱门距离小于50厘米时,AI摄像头立即开始分析产品放置;或者,使用备选的PIR运动传感器,摄像头在感应到动作时启动检测,并在静止5秒后发送更新。这种嵌入式AI与全栈开发的完美结合,使库存管理变得轻松高效。
硬件配置: 关键组件与传感器安装详解
我们的设置融合了传感器和先进AI技术:
Raspberry Pi 5:作为核心处理单元。
AI摄像头模块:搭载定制训练的YOLOv8n模型(导出为IMX500格式),用于检测奶酪、可乐、鸡蛋、番茄酱、牛奶、pittas和schnitzels等产品。
超声波传感器:安装在冰箱侧面,测量门距。当距离小于50厘米时,系统激活产品检测。
PIR运动传感器(备选):检测到运动时触发识别,并在5秒无动作后发送更新。
Flask服务器(集成SocketIO):处理实时数据传输。
React仪表盘:显示两个清单——当前在冰箱中的物品和已移除需补货的物品。
连接示意图如下,展示传感器与摄像头的布线:
超声波传感器安装指南: 将传感器置于冰箱侧面,正对门体。传感器持续监控距离:当门关闭(距离<50厘米)时,系统开始捕捉画面进行分析;当门开启(距离>50厘米)时,触发更新传输。
PIR运动传感器安装指南(备选):
安装传感器以检测冰箱附近动作。
动作检测时,摄像头启动产品识别;5秒无动作后,发送库存更新。 详细布线请参考Fritzing示意图。
AI模型: 定制YOLOv8产品识别技术实现
系统核心是搭载定制YOLOv8n网络的AI摄像头,该模型通过Roboflow优化并导出为IMX500兼容格式,能识别目标产品,并根据画面位置区分"进"(冰箱内)和"出"(移除或缺失)物品。
产品识别逻辑代码示例:
import cv2
import numpy as np
# 加载定制产品检测模型(YOLOv8n导出为IMX500格式)
model = load_model('path_to_imx500_model')
def detect_products(frame):
# 预处理画面供模型使用
processed_frame = preprocess_frame(frame)
# 运行推理
detections = model(processed_frame)
# 解析检测结果并分类为'进'和'出'
in_products = []
out_products = []
for det in detections:
label = det['label']
x, y, w, h = det['bbox']
# 假设画面垂直分割:左半为"进",右半为"出"
if x + w / 2 < frame.shape[1] // 2:
in_products.append(label)
else:
out_products.append(label)
return in_products, out_products
# 辅助函数preprocess_frame()和load_model()在其他部分实现
传感器逻辑与实时通信实现方案
超声波传感器工作流程: 超声波传感器测量门距,当距离小于50厘米时,系统启动画面捕捉和产品检测;门移开后(距离>50厘米),累积的"进"和"出"列表发送至服务器。
import time
import socketio
# 初始化SocketIO客户端
sio = socketio.Client()
sio.connect('http://your-ngrok-url')
THRESHOLD = 50 # 距离阈值(单位:厘米)
def read_ultrasonic_sensor():
# 模拟传感器读数;替换为实际逻辑
return get_distance()
def sensor_loop():
detecting = False
in_list = []
out_list = []
while True:
distance = read_ultrasonic_sensor()
if distance < THRESHOLD and not detecting:
print("检测到冰箱门!启动库存检查...")
detecting = True
# 捕捉画面进行产品检测(模拟摄像头捕捉)
frame = capture_frame() # 替换为实际摄像头逻辑
in_list, out_list = detect_products(frame)
elif distance >= THRESHOLD and detecting:
print("冰箱门关闭。向服务器发送库存数据。")
sio.emit('inventory_update', {'in': in_list, 'out': out_list})
detecting = False
in_list, out_list = [], []
time.sleep(0.5) # 调整传感器轮询间隔
# 在Raspberry Pi上运行sensor_loop()以持续监控门状态
PIR运动传感器工作流程(备选): 对于PIR传感器环境,摄像头在动作检测时激活,并在5秒静止后发送更新。
def pir_sensor_loop():
detecting = False
last_motion_time = time.time()
in_list = []
out_list = []
while True:
motion_detected = read_pir_sensor() # 替换为实际传感器逻辑
if motion_detected:
last_motion_time = time.time()
if not detecting:
print("检测到动作!启动产品识别...")
detecting = True
frame = capture_frame() # 使用AI摄像头捕捉画面
in_list, out_list = detect_products(frame)
elif detecting and (time.time() - last_motion_time) > 5:
print("5秒无动作。传输库存更新。")
sio.emit('inventory_update', {'in': in_list, 'out': out_list})
detecting = False
in_list, out_list = [], []
time.sleep(0.5)
Flask+SocketIO服务器实现: Flask服务器接收实时库存更新,并广播至所有连接的客户端,确保React仪表盘显示最新数据。
from flask import Flask
from flask_socketio import SocketIO
app = Flask(__name__)
socketio = SocketIO(app, cors_allowed_origins="*")
@socketio.on('inventory_update')
def handle_inventory_update(data):
print("收到库存更新:", data)
# 向仪表盘客户端广播更新
socketio.emit('dashboard_update', data)
if __name__ == '__main__':
socketio.run(app, host='0.0.0.0', port=5000)
前端展示: React实时仪表盘开发指南
数据管道从Raspberry Pi经Flask服务器(通过ngrok公开)流向React仪表盘。仪表盘显示两个列表:冰箱中的产品和需补货的移除产品。
React仪表盘代码示例:
import React, { useEffect, useState } from "react";
import io from "socket.io-client";
const socket = io("http://your-ngrok-url");
const FridgeInventory = () => {
const [inventory, setInventory] = useState({ in: [], out: [] });
useEffect(() => {
socket.on("dashboard_update", (data) => {
setInventory(data);
});
return () => {
socket.off("dashboard_update");
};
}, []);
return (
<div>
<h2>冰箱库存</h2>
<div>
<h3>在冰箱中</h3>
<ul>
{inventory.in.map((item, index) => (
<li key={index}>{item}</li>
))}
</ul>
</div>
<div>
<h3>需补货物品</h3>
<ul>
{inventory.out.map((item, index) => (
<li key={index}>{item}</li>
))}
</ul>
</div>
</div>
);
};
export default FridgeInventory;
系统部署与常见问题解决策略
部署此类边缘AI解决方案需应对以下挑战:
传感器校准:微调超声波传感器阈值(50厘米),确保PIR传感器(如使用)位置准确。
网络稳定性:通过ngrok运行Flask服务器需可靠网络连接,监控延迟和带宽。
模型优化:采用轻量级框架,确保定制产品识别模型针对实时推理优化。
日志记录与监控:在传感器循环和服务器中实施详细日志,以便快速诊断问题。
总结: 智能冰箱管理的未来展望
本项目整合嵌入式系统、深度学习和全栈开发,创建了智能冰箱库存管理方案。借助Raspberry Pi 5、定制YOLOv8n模型AI摄像头和传感器触发检测,用户能自动跟踪冰箱内容及补货需求——全部实时更新。准备好自动化厨房了吗?克隆我们的仓库,设置Raspberry Pi,开启高效冰箱管理新时代。祝您构建愉快,享受智能厨房生活!如有疑问或反馈,请访问GitHub仓库提交问题。
参考资料: 扩展学习资源
Raspberry Pi 5文档:https://www.raspberrypi.com/documentation/
Flask-SocketIO指南:https://flask-socketio.readthedocs.io/en/latest/
React文档:https://react.dev/
YOLOv8和Roboflow文档:https://roboflow.com/
IMX500传感器概述:https://www.sony-semicon.co.jp/products_en/IS/sensor2/products/IMX500_501.html