yolov8训练模型、测试视频

yolov8先训练生成best.pt文件,用这个生成的模型进行视频的测试

因为本来用的代码生成的测试视频打不开,格式应该是损坏了,或者部分帧没有正常保存吧。

修改了一下代码,现状可以正常打开生成的视频了。

1、训练代码train.py

python 复制代码
import os

# os.environ["CUDA_VISIBLE_DEVICES"] = "3"  # 同样是选择第3块GPU

from ultralytics import YOLO

# Load a model
# model = YOLO("yolov8n.yaml")  # build a new model from YAML
# model = YOLO("yolov8n.pt")  # load a pretrained model (recommended for training)

# ffs = os.listdir("cfg1116/new_cfg")
# for ff in ffs:
model = YOLO(f"cfg1116/yolov8n.yaml")  # build from YAML and transfer weights
# Train the model
# results = model.train(data=r"/mnt/disk3/sunjiahui/CV-code/v8_all/data.yaml", epochs=5, imgsz=1280, workers=0, batch=2, device=[2])
results = model.train(
    data=r"/mnt/disk3/sunjiahui/CV-code/v8_all/data.yaml",
    epochs=500,
    imgsz=1280,
    workers=0,
    batch=2,
    device=[0],
    hsv_h=0.015,  # HSV色调变化
    hsv_s=0.7,    # HSV饱和度变化
    hsv_v=0.4,    # HSV亮度变化
    degrees=0.0,  # 旋转角度
    translate=0.1,  # 平移比例
    scale=0.5,    # 缩放比例
    shear=0.0,    # 剪切变换
    perspective=0.0,  # 透视变换
    flipud=0.0,   # 上下翻转概率
    fliplr=0.5,   # 左右翻转概率
    mosaic=1.0,   # Mosaic增强的概率
    mixup=0.0     # MixUp增强的概率
)
model.val(imgsz=[1280,1280])

2、测试代码:视频

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

os.environ["CUDA_VISIBLE_DEVICES"] = "2"  # 同样是选择第3块GPU

def process_video():
    # 初始化模型
    model = YOLO("runs/detect/train2/weights/best.pt")
    
    # 输入输出路径
    input_path = "/mnt/disk3/sunjiahui/CV-code/v8_all/XIONG_AN/shipin.mp4"
    output_path = "/mnt/disk3/sunjiahui/CV-code/v8_all/XIONG_AN/output_video15.mp4"
    
    # 尝试不同编解码器组合
    codec_options = ['mp4v', 'avc1', 'X264', 'MJPG']
    success = False
    
    for codec in codec_options:
        try:
            cap = cv2.VideoCapture(input_path)
            fps = int(cap.get(cv2.CAP_PROP_FPS)) or 30  # 处理fps为0的情况
            width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
            height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
            
            fourcc = cv2.VideoWriter_fourcc(*codec)
            out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
            print(f"尝试使用编解码器 {codec}...")
            
            while cap.isOpened():
                ret, frame = cap.read()
                if not ret:
                    break
                
                results = model.predict(frame, conf=0.15)
                annotated_frame = results[0].plot()
                
                # 确保帧格式正确
                if annotated_frame.shape[:2] != (height, width):
                    annotated_frame = cv2.resize(annotated_frame, (width, height))
                
                out.write(annotated_frame)
            
            success = True
            break
            
        except Exception as e:
            print(f"编解码器 {codec} 失败: {str(e)}")
            if os.path.exists(output_path):
                os.remove(output_path)
            continue
            
        finally:
            cap.release()
            out.release()
    
    if success:
        print(f"视频生成成功!保存路径:{os.path.abspath(output_path)}")
        print("如果仍无法播放,请尝试以下方案:")
        print("1. 使用 VLC 播放器(兼容性最佳)")
        print("2. 执行命令:ffmpeg -i output_video.mp4 -c:v libx264 final.mp4")
    else:
        print("所有编解码器尝试失败,改用图像序列方案...")
        save_as_image_sequence(model, input_path)

def save_as_image_sequence(model, input_path):
    """备用方案:保存为图片序列"""
    output_dir = "video_frames"
    os.makedirs(output_dir, exist_ok=True)
    
    cap = cv2.VideoCapture(input_path)
    frame_count = 0
    
    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break
        
        results = model.predict(frame)
        annotated_frame = results[0].plot()
        
        cv2.imwrite(f"{output_dir}/frame_{frame_count:04d}.jpg", annotated_frame)
        frame_count += 1
    
    cap.release()
    print(f"图像序列已保存至 {output_dir},可用以下命令合成视频:")
    print(f"ffmpeg -framerate 30 -i {output_dir}/frame_%04d.jpg -c:v libx264 output.mp4")

if __name__ == "__main__":
    process_video()
相关推荐
xcSpark19 分钟前
Python基础入门(二)
开发语言·人工智能·python
予早35 分钟前
Python 冷门魔术方法
开发语言·python
databook42 分钟前
多变量决策树:机器学习中的“多面手”
python·机器学习·scikit-learn
linux kernel1 小时前
Python基础语法2
开发语言·python
学点技术儿1 小时前
理解什么是叶子张量和非叶子张量?
机器学习
学点技术儿1 小时前
pytorch计算图的可视化
机器学习
互联网搬砖老肖1 小时前
python成功解决AttributeError: can‘t set attribute ‘lines‘
开发语言·python
奋斗者1号1 小时前
深入解析 sklearn 中的 LabelEncoder:功能、使用场景与注意事项
人工智能·python·sklearn
CrawlerCracker1 小时前
小程序逆向|六六找房|请求头Authorization
javascript·爬虫·python·小程序·网络爬虫·js
大霸王龙2 小时前
基于 Streamlit 的 PDF 编辑器
python·pdf·编辑器·streamlit