物品识别 树莓派 5 YOLO v5 v8 v10 11 计算机视觉

0. 要实现的效果


让树莓派可以识别身边的一些物品,比如电脑,鼠标,键盘,杯子,行李箱,双肩包,床,椅子等


请添加图片描述

请添加图片描述


1. 硬件设备


请添加图片描述


2. 前置条件

  1. 给树莓派烧录好操作系统,下面我们会用现在最新的(2024年12月) bookworm
  2. VNC 连接或者用一根 HDMI 或者用官方的 raspberrypi connect

我写过一篇关于给树莓派烧录操作系统的 blog blog.csdn.net/u013633921/article/details/121433186

也有一篇 VNC 的 blog blog.csdn.net/u013633921/article/details/129677105


3. 开始!


更新一下,下面 4 个截图都好理解,不懂问问 AI

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述


下面这条命令将安装 OpenCV 以及运行 YOLO 所需的基础设施

pip install ultralytics[export]

还会安装大量其他软件包,容易失败
如果安装失败(会显示一大片红色)
只需重新执行,已经安装过的不会再安装
我是一次过的,哈哈哈哈哈哈~(过程大概有 2 个小时 🤔)

安装后,重启树莓派
Pi 5 有物理按键,连续按两次会关机。等等再按一次,就会启动。


4. Thonny

切换到常规模式。
在这里插入图片描述
关闭 Thonny 再打开 Thonny。

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

用 Thonny 创建个文件 yolo.py

import cv2
from picamera2 import Picamera2
from ultralytics import YOLO

# Set up the camera with Picam
picam2 = Picamera2()
picam2.preview_configuration.main.size = (1280, 1280)
picam2.preview_configuration.main.format = "RGB888"
picam2.preview_configuration.align()
picam2.configure("preview")
picam2.start()

# Load YOLOv8
model = YOLO("yolov8n.pt")

while True:
    # Capture a frame from the camera
    frame = picam2.capture_array()
    
    # Run YOLO model on the captured frame and store the results
    results = model(frame)
    
    # Output the visual detection data, we will draw this on our camera preview window
    annotated_frame = results[0].plot()
    
    # Get inference time
    inference_time = results[0].speed['inference']
    fps = 1000 / inference_time  # Convert to milliseconds
    text = f'FPS: {fps:.1f}'

    # Define font and position
    font = cv2.FONT_HERSHEY_SIMPLEX
    text_size = cv2.getTextSize(text, font, 1, 2)[0]
    text_x = annotated_frame.shape[1] - text_size[0] - 10  # 10 pixels from the right
    text_y = text_size[1] + 10  # 10 pixels from the top

    # Draw the text on the annotated frame
    cv2.putText(annotated_frame, text, (text_x, text_y), font, 1, (255, 255, 255), 2, cv2.LINE_AA)

    # Display the resulting frame
    cv2.imshow("Camera", annotated_frame)

    # Exit the program if q is pressed
    if cv2.waitKey(1) == ord("q"):
        break

# Close all windows
cv2.destroyAllWindows()

点一下绿色 Run 按钮(三角),等一小会,

在这里插入图片描述
然后你就能看到这样的运行效果了。按 q 可以关闭。

请添加图片描述

可以改改第 7 行,第 14 行,再运行看看

在这里插入图片描述


喜欢或对你有帮助,点个赞吧,自己先点个嘿嘿。
有错误或者疑问还请评论指出。
我的个人网站 点击访问 hongweizhu.com

END

推荐一下我写的的 App 熊猫小账本

熊猫小账本 一个简洁的记账 App,用于记录日常消费开支收入,使用 iCloud 保存同步数据。

  • 支持备注,自定义时间偶尔忘记记账也没关系。
  • 搜索历史记账,支持分类、金额、备注。
  • 启动时需要面容/指纹验证,保护个人隐私。
  • 支持自定义分类功能,自由添加修改分类。
  • 统计图表,支出收入一目了然。
  • 每天提醒记账,不会有其他推送。
  • 桌面锁屏小组件等。

点击了解更多详情 👀

### 基于树莓派的视觉识别开发指南 #### 1. 环境搭建 为了在树莓派上实现高效的视觉识别功能,可以采用Yocto项目来定制操作系统环境。这一步骤能够确保系统轻量级运行并支持AI框架的需求[^1]。 安装必要的依赖库以及工具链之后,需配置好OpenCV和TensorFlow Lite等机器学习框架的支持。这些框架提供了丰富的API接口用于图像处理与模型推理操作。 ```bash sudo apt-get update && sudo apt-get upgrade -y sudo apt-get install python3-opencv libatlas-base-dev pip3 install tflite-runtime ``` #### 2. 模型准备 选择适合边缘设备使用的预训练模型非常重要。SSD-MobileNet因其低延迟特性成为理想的选择之一。该网络结构不仅保持较高的精度,在资源受限条件下依然表现良好。 下载官方提供的浮点版本或者整数量化后的.tflite文件到本地目录下: ```python import urllib.request url = 'https://tfhub.dev/tensorflow/lite-model/ssd_mobilenet_v2/1/metadata' urllib.request.urlretrieve(url, "/path/to/model.tflite") ``` #### 3. 实时目标检测应用编写 利用Python脚本加载上述提到的TFLite模型,并结合摄像头输入流完成每一帧的画面分析工作流程如下所示: ```python import cv2 from PIL import Image import numpy as np import tensorflow.lite as tflite interpreter = tflite.Interpreter(model_path="/path/to/model.tflite") interpreter.allocate_tensors() cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() img = cv2.resize(frame,(300,300)) input_data = np.expand_dims(img,axis=0).astype('float32') interpreter.set_tensor(interpreter.get_input_details()[0]['index'],input_data) interpreter.invoke() output_details = interpreter.get_output_details() boxes = interpreter.get_tensor(output_details[0]['index']) classes = interpreter.get_tensor(output_details[1]['index']) scores = interpreter.get_tensor(output_details[2]['index']) # Draw bounding box and label on image... cv2.destroyAllWindows() ``` 以上代码片段展示了基本的目标检测逻辑。 #### 4. 性能调优建议 针对具体应用场景可能还需要进一步调整参数设置比如改变分辨率大小、降低刷新率等方式提升整体效率;另外也可以尝试不同类型的压缩算法减少计算负担从而提高响应速度。 ---
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值