import os
import cv2
path = r'H:\datasets\Positive\video'
img_path0 = r'H:\datasets\Positive\img'
# os.chdir('视频文件根目录') # 例如f:/video
# v_path = 'ghz.mp4'
list_video = []
for i in os.listdir(path):
list_video.append(os.path.join(path, i))
for m in list_video:
cap = cv2.VideoCapture(m)
frame_count = cap.get(cv2.CAP_PROP_FRAME_COUNT)
if frame_count!= 125:
img_path = os.path.join(img_path0, m.split("\\")[4])
if (os.path.exists(img_path) == False):
os.makedirs(img_path)
for i in range(int(frame_count)):
_, img = cap.read()
img = cv2.cvtColor(img, cv2.cv2.COLOR_BGR2GRAY) # cv2.COLOR_RGB2HSV cv2.COLOR_BGR2GRAY
if i== 125:
continue
cv2.imwrite(img_path+'/image{}.jpg'.format(i), img)
视频取帧python
于 2022-05-31 15:17:40 首次发布