YOLO应用

yolov5 安装运行

# https://blog.csdn.net/qq_45701791/article/details/113992622 (参考连接)
conda create -n yolo python=3.8 # 创建虚拟环境
conda activate yolo # 激活虚拟环境
git clone https://github.com/ultralytics/yolov5.git # 下载代码
cd yolov5
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple # 配置环境
python detect.py --weights yolov5s.pt --img 640 --conf 0.25 --source data/images/# 测试
python train.py --img 640 --batch 50 --epochs 100 --data ../yolo_A/A.yaml --weights yolov5s.pt --nosave --cache # 训练

  yolov8 安装运行

# git clone https://github.com/ultralytics/ultralytics.git
# git clone https://gitcode.com/ultralytics/ultralytics.git
pip install ultralytics
yolo predict model=yolov8n.pt source='https://ultralytics.com/images/bus.jpg'
# yolo predict model=yolov8n.pt source=0
yolo task=detect mode=train model=yolov8n.pt data=../data.yaml epochs=100 batch=32 # 模型训练
yolo task=detect mode=predict model=../best.pt  source=0 # 模型实验

yolov8 python测试脚本(输入为视频流和图片)

import cv2
from ultralytics import YOLO

# 加载 YOLOv8 模型
# model = YOLO("ultralytics/weights/yolov8s.engine")
model = YOLO("ultralytics/weights/yolov8s.pt")

# 获取摄像头内容,参数 0 表示使用默认的摄像头
cap = cv2.VideoCapture(6)

# Loop through the video frames
while cap.isOpened():
    # Read a frame from the video
    success, frame = cap.read()
    if success:
        # Run YOLOv8 inference on the frame
        results = model(frame)
        # Visualize the results on the frame
        annotated_frame = results[0].plot()
        # Display the annotated frame
        cv2.imshow("YOLOv8 Inference", annotated_frame)
        # Break the loop if 'q' is pressed
        if cv2.waitKey(1) & 0xFF == ord("q"):
            break
    else:
        # Break the loop if the end of the video is reached
        break

# Release the video capture object and close the display window
cap.release()
cv2.destroyAllWindows()
import cv2
from ultralytics import YOLO

# 加载 YOLOv8 模型
# model = YOLO("ultralytics/weights/yolov8s.engine")
# model = YOLO("ultralytics/weights/yolov8s.pt")
model = YOLO("ultralytics/weights/yolov8n-seg.pt")

frame_path = "images/0000.png"
results = model(frame_path,conf=0.3,classes=39)
for result in results:
    boxes = result.boxes
    print(boxes)
    masks = result.masks.data
    print(masks)
    
annotated_frame = results[0].plot()
# cv2.imshow("YOLOv8 Inference", annotated_frame)
cv2.imwrite("0000.png", annotated_frame)

  • 16
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值