基于Wio Terminal的智能库存计数系统开发指南
引言
在现代零售环境中,准确高效的库存管理至关重要。本文将介绍如何使用Wio Terminal开发一个基于计算机视觉的智能库存计数系统,这是Microsoft物联网初学者项目中的一个实用案例。
系统原理概述
该系统通过Wio Terminal的摄像头捕获货架图像,利用对象检测算法识别商品并绘制边界框,然后通过计算边界框的重叠率来准确统计商品数量。
关键技术实现
1. 边界框重叠检测算法
核心算法包括以下几个关键组件:
struct Point {
float x, y;
};
struct Rect {
Point topLeft, bottomRight;
};
float area(Rect rect) {
return abs(rect.bottomRight.x - rect.topLeft.x) *
abs(rect.bottomRight.y - rect.topLeft.y);
}
该结构定义了图像中的点和矩形区域,以及计算矩形面积的函数。
2. 重叠区域计算
float overlappingArea(Rect rect1, Rect rect2) {
float left = max(rect1.topLeft.x, rect2.topLeft.x);
float right = min(rect1.bottomRight.x, rect2.bottomRight.x);
float top = max(rect1.topLeft.y, rect2.topLeft.y);
float bottom = min(rect1.bottomRight.y, rect2.bottomRight.y);
if (right > left && bottom > top) {
return (right-left)*(bottom-top);
}
return 0.0f;
}
该函数计算两个矩形重叠区域的面积,若无重叠则返回0。
3. 边界框转换
Rect rectFromBoundingBox(JsonVariant prediction) {
JsonObject bounding_box = prediction["boundingBox"].as<JsonObject>();
float left = bounding_box["left"].as<float>();
float top = bounding_box["top"].as<float>();
float width = bounding_box["width"].as<float>();
float height = bounding_box["height"].as<float>();
Point topLeft = {left, top};
Point bottomRight = {left + width, top + height};
return {topLeft, bottomRight};
}
将JSON格式的预测结果转换为矩形结构,便于后续处理。
库存计数核心逻辑
std::vector<JsonVariant> passed_predictions;
for (int i = 0; i < predictions.size(); ++i) {
Rect prediction_1_rect = rectFromBoundingBox(predictions[i]);
float prediction_1_area = area(prediction_1_rect);
bool passed = true;
for (int j = i + 1; j < predictions.size(); ++j) {
Rect prediction_2_rect = rectFromBoundingBox(predictions[j]);
float prediction_2_area = area(prediction_2_rect);
float overlap = overlappingArea(prediction_1_rect, prediction_2_rect);
float smallest_area = min(prediction_1_area, prediction_2_area);
if (overlap > (overlap_threshold * smallest_area)) {
passed = false;
break;
}
}
if (passed) {
passed_predictions.push_back(predictions[i]);
}
}
该算法通过双重循环比较所有预测结果,当两个边界框的重叠面积超过阈值(默认20%)时,认为它们代表同一商品,只保留其中一个预测结果。
结果输出与优化
系统最终会输出每个有效检测到的商品信息及总数:
tomato paste: 35.84% {"left":0.395631,"top":0.215897,"width":0.180768,"height":0.359364}
Counted 4 stock items.
优化建议
- 动态调整重叠阈值:根据实际场景调整
overlap_threshold
值 - 多维度验证:结合商品标签和置信度进行更精确的过滤
- 异常处理:增加对无效预测结果的容错机制
实际应用扩展
本系统可进一步扩展为:
- 实时库存监控系统
- 自动补货预警系统
- 货架陈列分析工具
结语
通过本教程,我们实现了一个基于Wio Terminal的智能库存计数系统原型。该系统展示了物联网设备在零售场景中的实际应用价值,为初学者提供了从理论到实践的完整学习路径。读者可以根据实际需求进一步优化算法,或将其集成到更大的零售管理系统中。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考