把文件夹下的多个视频处理成图片

目标检测常用数据处理脚本,把videos文件夹下的多个视频处理成图片(每隔15帧取一帧图,保持长宽比缩放为640*480大小),命名成视频名+帧数.jpg的形式保存在images文件夹下,并输出图片名称到list.txt中。

import cv2
import numpy as np
from pathlib import Path


videos_path=Path(r"./videos")
output_dir=Path(r"./images")
fps=15
w, h = 640, 480


def keepWHR_Resize(image_matrix, output_width, output_height):
    '''
    Resize image keeping ratio and using white background.
    image_matrix:narray 'RGB'
    '''
    new_image = np.zeros((output_height, output_width, 3), dtype=image_matrix.dtype)
    if len(image_matrix.shape)<3:
        image_matrix=cv2.cvtColor(image_matrix,cv2.COLOR_GRAY2BGR)
    if image_matrix.shape[-1]==4:
        image_matrix = cv2.cvtColor(image_matrix,cv2.COLOR_RGBA2BGR)
    origin_height,origin_width  = image_matrix.shape[:2]
    ratio_w = output_width / origin_width
    ratio_h = output_height / origin_height
    if ratio_w < ratio_h:
        resize_width = output_width
        resize_height = round(ratio_w * origin_height)
    else:
        resize_width = round(ratio_h * origin_width)
        resize_height = output_height
    image_resize = cv2.resize(image_matrix,(resize_width,resize_height))
    new_image[0:resize_height,0:resize_width,:]=image_resize
    new_image=np.ascontiguousarray(new_image)
    return new_image


cnt=0
for v_p in videos_path.glob("./*"):
    print(v_p)
    vid_cap = cv2.VideoCapture(str(v_p))
    video_name = v_p.stem
    count=0
    predix=video_name+"_%d.jpg"
    with open(output_dir.joinpath("list.txt"),"a") as f:
        while (vid_cap.isOpened()):
            _, img = vid_cap.read()
            if _:
                if count%fps==0:
                    img =keepWHR_Resize(img, w, h)                   
                    cv2.imwrite(str(output_dir/(predix%cnt)),img)
                    f.write(predix%cnt+"\n")
                    cnt+=1
                count+=1
            else:
                break
    vid_cap.release()

你可能感兴趣的:(deep,learning,#,object,detection,计算机视觉,python,目标检测)