You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用YOLOv8统计视频每帧边界框数量并输出CSV文件?

YOLOv8 视频帧轮虫计数导出CSV实现方案

核心代码实现(简洁高效版)

直接利用YOLOv8的stream模式遍历视频帧,统计每帧的轮虫检测框数量并导出CSV:

from ultralytics import YOLO
import pandas as pd

# 加载你的轮虫检测模型
model = YOLO("your_trained_model.pt")
# 存储每帧计数结果
count_results = []
frame_index = 0

# 遍历视频帧进行检测,conf为置信度阈值,按需调整
for result in model.predict(source="your_video.mp4", conf=0.5, stream=True):
    frame_index += 1
    # 统计当前帧的边界框数量
    worm_count = len(result.boxes)
    count_results.append({"帧号": frame_index, "边界框计数": worm_count})

# 转换为DataFrame并保存CSV
result_df = pd.DataFrame(count_results)
result_df.to_csv("轮虫帧计数结果.csv", index=False, encoding="utf-8-sig")

关键参数说明

  • 替换your_trained_model.pt为你训练好的模型文件路径,your_video.mp4为目标视频路径
  • conf=0.5:过滤置信度低于0.5的检测框,可根据模型实际精度调整(比如模型精度高可设0.7,避免误检)
  • encoding="utf-8-sig":确保CSV文件在Excel等软件中打开时不会出现中文乱码

手动帧处理版(更灵活)

如果需要对帧做额外预处理(比如裁剪、增强),可以手动读取视频帧:

from ultralytics import YOLO
import cv2
import pandas as pd

model = YOLO("your_trained_model.pt")
video_cap = cv2.VideoCapture("your_video.mp4")
count_results = []
frame_index = 0

while video_cap.isOpened():
    ret, frame = video_cap.read()
    if not ret:
        break
    
    frame_index += 1
    # 可在此添加帧预处理逻辑,比如frame = cv2.resize(frame, (640, 640))
    results = model(frame, conf=0.5)
    worm_count = len(results[0].boxes)
    count_results.append({"帧号": frame_index, "边界框计数": worm_count})

video_cap.release()
result_df = pd.DataFrame(count_results)
result_df.to_csv("轮虫帧计数结果.csv", index=False, encoding="utf-8-sig")

内容的提问来源于stack exchange,提问作者Colton Pelletier

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 21:13:17