import cv2
import logging
import os
# log information settings
logging.basicConfig(level=logging.INFO,
format='%(asctime)s - %(levelname)s: %(message)s')
def save_image(num, image):
"""Save the data.
Args:
num: serial number
image: image resource
Returns:
None
"""
# 指定输出路径
image_path = './labels/{}.jpg'.format(str(num))
cv2.imwrite(image_path, image)
count = 0 # count the number of pictures
# 输入视频路径
folder_path = f'./video'
# 遍历文件夹下的所有文件
for root, dirs, files in os.walk(folder_path):
for file in files:
# 打印文件路径
file_path = os.path.join(root, file)
vc = cv2.VideoCapture(file_path) # import video files
print(file_path)
# determine whether to open normally
if vc.isOpened():
ret, frame = vc.read()
else:
ret = False
frame_interval = 30 # video frame count interval frequency
frame_interval_count = 0
# loop read video frame
while ret:
ret, frame = vc.read()
# store operation every time f frame
if frame_interval_count % frame_interval == 0:
save_image(count, frame)
logging.info("num:" + str(count) + ", frame: " +
str(frame_interval_count))
count += 1
frame_interval_count += 1
cv2.waitKey(5)
vc.release()
Python-opencv-将指定路径全部视频切帧处理
最新推荐文章于 2024-07-10 09:55:17 发布