opencv-python将图片序列转成视频
在跟踪结果以图片形式保存后,有时为了演示会希望将图片序列转换成视频,甚至作成动图插入到博客中,所以这里记录一下自己用的脚本,方便后续取用,本文是利用yolov8 + deepsort算法实现目标的分割与跟踪
img2video.py
import os
import cv2
# image path
im_dir = './imgs'
# output video path
save_video_dir = './mp4'
if not os.path.exists(save_video_dir):
os.makedirs(save_video_dir)
# set saved fps
fps = 10
# get frames list
frames = sorted(os.listdir(im_dir))
# w,h of image
img = cv2.imread(os.path.join(im_dir, frames[0]))
img_size = (img.shape[1], img.shape[0])
# get seq name
seq_name = os.path.dirname(im_dir).split('/')[-1]
# splice video_dir
video_dir = os.path.join(save_video_dir, seq_name + 'save.mp4')
#fourcc = cv2.VideoWriter_fourcc('M', 'J', 'P', 'G')
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
# also can write like:fourcc = cv2.VideoWriter_fourcc(*'MJPG')
# if want to write .mp4 file, use 'MP4V'
videowriter = cv2.VideoWriter(video_dir, fourcc, fps, img_size)
for frame in frames:
f_path = os.path.join(im_dir, frame)
image = cv2.imread(f_path)
videowriter.write(image)
print(frame + " has been written!")
videowriter.release()
yolov8 + deepsort算法效果如下,需要源码或讨论的找我,回头有空再写博客
test2
yolov8 + deepsort算法