【Arduino UNO Q】图像检测
本文介绍了 Arduino UNO Q 开发板结合 Arduino App Lab 实现网页图像检测和目标识别的项目设计,包括软件安装、工程创建、流程图、工程代码、效果演示等。
项目介绍
准备工作:硬件连接、Arduino App Lab 安装、软件包更新等;
工程加载:目标检测例程、工程代码、流程图等;
调试运行:编译上传、运行程序、加载图片测试、效果演示等。
硬件连接
连接显示屏(或 SSH 远程登录、数据线 ADB 登录);
连接鼠标键盘;
WiFi 连接无线网;
使用 PD 电源供电;


工程创建
下载、安装并运行 Arduino App Lab 软件;
识别到开发板,点击连接设备;
进入 Examples 标签页;
打开 Detect objects on images 示例工程;


流程图

工程代码
展开左侧文件列表,打开 python/main.py 文件,代码如下
from arduino.app_utils import *
from arduino.app_bricks.web_ui import WebUI
from arduino.app_bricks.object_detection import ObjectDetection
from PIL import Image
import io
import base64
import time
object_detection = ObjectDetection()
def on_detect_objects(client_id, data):
"""Callback function to handle object detection requests."""
try:
image_data = data.get('image')
confidence = data.get('confidence', 0.5)
if not image_data:
ui.send_message('detection_error', {'error': 'No image data'})
return
image_bytes = base64.b64decode(image_data)
pil_image = Image.open(io.BytesIO(image_bytes))
start_time = time.time() * 1000
results = object_detection.detect(pil_image, confidence=confidence)
diff = time.time() * 1000 - start_time
if results is None:
ui.send_message('detection_error', {'error': 'No results returned'})
return
img_with_boxes = object_detection.draw_bounding_boxes(pil_image, results)
if img_with_boxes is not None:
img_buffer = io.BytesIO()
img_with_boxes.save(img_buffer, format="PNG")
img_buffer.seek(0)
b64_result = base64.b64encode(img_buffer.getvalue()).decode("utf-8")
else:
# If drawing fails, send back the original image
img_buffer = io.BytesIO()
pil_image.save(img_buffer, format="PNG")
img_buffer.seek(0)
b64_result = base64.b64encode(img_buffer.getvalue()).decode("utf-8")
response = {
'success': True,
'result_image': b64_result,
'detection_count': len(results.get("detection", [])) if results else 0,
'processing_time': f"{diff:.2f} ms"
}
ui.send_message('detection_result', response)
except Exception as e:
ui.send_message('detection_error', {'error': str(e)})
ui = WebUI()
ui.on_message('detect_objects', on_detect_objects)
App.run()保存代码。
编译运行
点击右上角复制示例工程;
点击右上角 RUN 按钮,自动编译、上传并运行程序;


待程序编译并上传完成,自动打开目标网页;


点击 Upload 按钮,上传目标识别图片;
移动滑块,调整识别置信度,置信度阈值越高,识别越精确;
点击 Run / Run Again 按钮,实现图像识别;


识别结果由彩色外框标注,标签显示识别物体名称及置信度;
更多测试结果如下


可以看出,识别结果较为准确,推理速度较快;


总结
本文介绍了 Arduino UNO Q 开发板结合 Arduino App Lab 实现网页图像检测和目标识别的项目设计,包括软件安装、工程创建、流程图、工程代码、效果演示等,为相关产品在边缘 AI 领域的快速开发和应用设计提供了参考。
我要赚赏金
