物品识别 树莓派4 YOLO v11

 让树莓派可以识别身边的一些物品

python3  -m venv --system-site-packages yolooo

source yolooo/bin/activate

树莓派换清华源,bookworm

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

pip install ultralytics
还会安装大量其他软件包,容易失败
如果安装失败(会显示一大片红色)
 

  • 官方PyPI源在2025年Q1出现过区域性访问故障
  • 推荐改用阿里云镜像:
    pip install ultralytics[export] -i https://mirrors.aliyun.com/pypi/simple/

如果有问题,再用这个装一下  

pip install ultralytics[export] -i https://pypi.tuna.tsinghua.edu.cn/simple

pip install --upgrade-strategy=only-if-needed ultralytics[export] -i https://mirrors.aliyun.com/pypi/simple/

 安装完毕,接着,打开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 按钮

0: 640x640 1 person, 1 bed, 1 book, 1756.0ms
Speed: 43.3ms preprocess, 1756.0ms inference, 6.0ms postprocess per image at shape (1, 3, 640, 640)

0: 640x640 1 person, 1 book, 1633.1ms
Speed: 30.1ms preprocess, 1633.1ms inference, 5.8ms postprocess per image at shape (1, 3, 640, 640)

0: 640x640 1 person, 1 bed, 1 laptop, 1647.8ms
Speed: 42.4ms preprocess, 1647.8ms inference, 6.3ms postprocess per image at shape (1, 3, 640, 640)

0: 640x640 1 person, 1 bed, 1 book, 1709.4ms
Speed: 35.9ms preprocess, 1709.4ms inference, 5.8ms postprocess per image at shape (1, 3, 640, 640)

0: 640x640 1 person, 1 bed, 1 laptop, 1 book, 1748.9ms
Speed: 23.9ms preprocess, 1748.9ms inference, 5.9ms postprocess per image at shape (1, 3, 640, 640)

0: 640x640 1 person, 1 bed, 1740.9ms
Speed: 26.0ms preprocess, 1740.9ms inference, 5.9ms postprocess per image at shape (1, 3, 640, 640)

0: 640x640 1 person, 1 bed, 174

 model = YOLO("yolov8n.pt")

换成model = YOLO("yolo11l.pt")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MC数据局

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值