使用 ultralytics 摄像头yolo推理

使用 ultralytics 摄像头yolo推理

官方网站
https://docs.ultralytics.com/

github
https://github.com/ultralytics/ultralytics

搭建环境

bash 复制代码
# Install the ultralytics package using conda
conda install -c conda-forge ultralytics

linux下摄像头推理

python 复制代码
import cv2
from ultralytics import YOLO
import time

# 加载轻量级模型,并指定较小的图像尺寸以提高速度
model = YOLO('yolov8n.pt')  # 或者根据实际情况选择其他轻量模型
IMG_SIZE = 320  # 调整输入图像尺寸

cap = cv2.VideoCapture(0)

prev_time = 0
curr_time = 0
fps = 0

while True:
    ret, frame = cap.read()
    if not ret:
        print("未能成功获取视频帧,退出...")
        break
    
    # 缩放图像以减小推理负担
    frame = cv2.resize(frame, (IMG_SIZE, IMG_SIZE))
    
    curr_time = time.time()
    fps = 1 / (curr_time - prev_time)
    prev_time = curr_time
    
    results = model(frame)
    
    annotated_frame = results[0].plot()
    cv2.putText(annotated_frame, f"FPS: {fps:.2f}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
    
    cv2.imshow('YOLO Real-Time Detection with FPS', annotated_frame)
    
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
相关推荐
bryant_meng12 分钟前
【Point Cloud】Point Cloud Fundamentals(1)
计算机视觉·点云·点云配准·点云分割·点云检测·点云特征提取
江畔柳前堤14 小时前
信号的本质——从模拟波形到AI中的数据张量
人工智能·深度学习·机器学习·计算机视觉·数据挖掘·语音识别
江畔柳前堤16 小时前
系统的本质——从LTI系统到神经网络
人工智能·深度学习·神经网络·机器学习·计算机视觉·数据挖掘·语音识别
sali-tec20 小时前
C# 基于OpenCv的视觉工作流-章98-频域滤波
图像处理·人工智能·opencv·计算机视觉
吃好睡好便好1 天前
MATLAB中图像的线性变换
开发语言·图像处理·学习·计算机视觉·matlab
落苜蓿蓝1 天前
多张覆盖视场的平面棋盘图片; 、对每张图片用 OpenCV 的 findChessboardCorners 检测内角点并用 corn ...
数码相机·opencv·平面
GetcharZp1 天前
让照片开口说话:LivePortrait 本地部署玩法详解
人工智能·计算机视觉
梦想三三1 天前
YOLOv5 口罩目标检测实战(一):项目整体介绍与数据准备
人工智能·yolo·目标检测
cout<<jester1 天前
人脸情绪识别模型
人工智能·深度学习·计算机视觉
明如正午1 天前
【python】Python + OpenCV 实现视频关键帧提取与智能去重
python·opencv·音视频