更新网站怎么弄,珠海手机网站开发,西安网站建设熊掌,网站建站知识yolov8先训练生成best.pt文件#xff0c;用这个生成的模型进行视频的测试
因为本来用的代码生成的测试视频打不开#xff0c;格式应该是损坏了#xff0c;或者部分帧没有正常保存吧。
修改了一下代码#xff0c;现状可以正常打开生成的视频了。
1、训练代码train.py
im…yolov8先训练生成best.pt文件用这个生成的模型进行视频的测试
因为本来用的代码生成的测试视频打不开格式应该是损坏了或者部分帧没有正常保存吧。
修改了一下代码现状可以正常打开生成的视频了。
1、训练代码train.py
import os# os.environ[CUDA_VISIBLE_DEVICES] 3 # 同样是选择第3块GPUfrom 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(fcfg1116/yolov8n.yaml) # build from YAML and transfer weights
# Train the model
# results model.train(datar/mnt/disk3/sunjiahui/CV-code/v8_all/data.yaml, epochs5, imgsz1280, workers0, batch2, device[2])
results model.train(datar/mnt/disk3/sunjiahui/CV-code/v8_all/data.yaml,epochs500,imgsz1280,workers0,batch2,device[0],hsv_h0.015, # HSV色调变化hsv_s0.7, # HSV饱和度变化hsv_v0.4, # HSV亮度变化degrees0.0, # 旋转角度translate0.1, # 平移比例scale0.5, # 缩放比例shear0.0, # 剪切变换perspective0.0, # 透视变换flipud0.0, # 上下翻转概率fliplr0.5, # 左右翻转概率mosaic1.0, # Mosaic增强的概率mixup0.0 # MixUp增强的概率
)
model.val(imgsz[1280,1280])
2、测试代码视频
from ultralytics import YOLO
import cv2
import osos.environ[CUDA_VISIBLE_DEVICES] 2 # 同样是选择第3块GPUdef process_video():# 初始化模型model YOLO(runs/detect/train2/weights/best.pt)# 输入输出路径input_path /mnt/disk3/sunjiahui/CV-code/v8_all/XIONG_AN/shipin.mp4output_path /mnt/disk3/sunjiahui/CV-code/v8_all/XIONG_AN/output_video15.mp4# 尝试不同编解码器组合codec_options [mp4v, avc1, X264, MJPG]success Falsefor 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:breakresults model.predict(frame, conf0.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 Truebreakexcept Exception as e:print(f编解码器 {codec} 失败: {str(e)})if os.path.exists(output_path):os.remove(output_path)continuefinally: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_framesos.makedirs(output_dir, exist_okTrue)cap cv2.VideoCapture(input_path)frame_count 0while cap.isOpened():ret, frame cap.read()if not ret:breakresults model.predict(frame)annotated_frame results[0].plot()cv2.imwrite(f{output_dir}/frame_{frame_count:04d}.jpg, annotated_frame)frame_count 1cap.release()print(f图像序列已保存至 {output_dir}可用以下命令合成视频)print(fffmpeg -framerate 30 -i {output_dir}/frame_%04d.jpg -c:v libx264 output.mp4)if __name__ __main__:process_video()