python调用opencv从视频中解析图片保存到本地

直接上代码,注意其他人博客中的代码和这边的可能不一样,贴一下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)

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值