直接上代码,注意其他人博客中的代码和这边的可能不一样,贴一下opencv和python的版本:
opencv版本:
opencv-python (4.0.0.21)
python版本:
Python 3.5.2rc1 (v3.5.2rc1:68feec6488b2+, Jun 12 2016, 08:56:24) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>
import cv2
import os
import shutil
#
def getFrame(videoPath, svPath,N):
cap = cv2.VideoCapture(videoPath)
numFrame = 0
while True:
if cap.grab():
flag, frame = cap.retrieve()
# cv2.imshow('video', frame)
numFrame += 1
if numFrame%N==0:
print("第",numFrame,"帧")
newPath = svPath+ str(numFrame) + ".jpg"
cv2.imencode('.jpg', frame)[1].tofile(newPath)
if cap.grab()==False:
break
savePicturePath="H:\车辆目标检测项目\数据集\YOLO测试视频\carsPicture\\"
for (root,dirs,files) in os.walk("H:\车辆目标检测项目\数据集\YOLO测试视频\\videos"):
n=1
for file in files:
if n<57:
n+=1
continue
n+=1
print(n)
videoPath=root+"\\"+file
getFrame(videoPath, savePicturePath,100)