欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

opencv讀取視頻并保存圖像的方法

 更新時(shí)間:2021年06月04日 10:33:33   作者:zhouxuechao  
實(shí)習(xí)項(xiàng)目要做安全帽目標(biāo)檢測(cè),拿到了公司給的一些視頻數(shù)據(jù),使用Opencv讀取視頻并每隔1s存儲(chǔ)一副圖像,本文就詳細(xì)的介紹一下使用,感興趣的可以了解一下

問(wèn)題重述

​ 實(shí)習(xí)項(xiàng)目要做安全帽目標(biāo)檢測(cè),拿到了公司給的一些視頻數(shù)據(jù),使用Opencv讀取視頻并每隔 1 s 1s 1s存儲(chǔ)一副圖像,下面是一些視頻數(shù)據(jù)

image-20210325225623015

實(shí)現(xiàn)步驟 添加依賴(lài)庫(kù)

import cv2
import os

定義視頻路徑和圖像存儲(chǔ)路徑

video_path = './未戴安全帽視頻01/'
image_path = './images/'

讀取視頻文件

video_files = [i for i in os.listdir(video_path) if i.split('.')[-1] in ['mp4']]
len(video_files)

獲取視頻幀

# video_file:'./未戴安全帽視頻01/中建四局-東圍墻5_001_2021-03-22-18-04-28_2021-03-22-18-04-33.mp4', 
# pic_dir:'中建四局-東圍墻5_001_2021-03-22-18-04-28_2021-03-22-18-04-33'
def get_image(video_file, pic_dir):
    if not os.path.exists(pic_dir):
        os.makedirs(pic_dir)
    
    # cv2讀取視頻文件
    vc = cv2.VideoCapture(video_file)
    index = 0
    # 判斷載入的視頻是否可以打開(kāi)
    rval = vc.isOpened()
    while rval:  # 循環(huán)讀取視頻幀
        index = index + 1
        
        rval, frame = vc.read()
        # 每十幀保存一張圖片
        if index * 10 % 1 == 0:
            if rval:
                # cv2.imshow("capture", frame)
                save_file = pic_dir + str(index).zfill(5) + '.png'
                cv2.imwrite(save_file, frame)  # 存儲(chǔ)為圖像,保存名為文件夾名
                cv2.waitKey(1)
            else:
                break
        vc.release()
    print("已保存%d" %(index - 1) + "張圖片")
        
# video_file = './未戴安全帽視頻01/01.mp4'
# pic_path = '01/'
# get_image(video_file, image_path + pic_path)

遍歷視頻文件

for file in video_files:
    video_file = video_path + file
    pic_path = image_path + file.replace('.mp4', '/')
    get_image(video_file, pic_path)

已保存1張圖片
已保存1張圖片
已保存1張圖片
已保存1張圖片
已保存1張圖片
已保存1張圖片
已保存1張圖片
已保存1張圖片
已保存1張圖片
已保存1張圖片
已保存1張圖片
已保存1張圖片
已保存1張圖片
已保存1張圖片
已保存1張圖片
已保存1張圖片
已保存1張圖片
已保存1張圖片

完整代碼

import cv2
import os


def save_img():
    video_path = r'F:\test\3.10'
    videos = os.listdir(video_path)
    for video_name in videos:
        file_name = video_name.split('.')[0]
        folder_name = video_path +'_'+ file_name
        os.makedirs(folder_name, exist_ok=True)
        print(video_path + '/' + video_name)
        vc = cv2.VideoCapture(video_path + '/' + video_name)
        # 讀入視頻文件
        c = 0
        rval = vc.isOpened()

        while rval:  # 循環(huán)讀取視頻幀
            c = c + 1

            rval, frame = vc.read()
            if c%10 ==0:
                pic_path = folder_name + '/'
                if rval:
                    cv2.imwrite(pic_path + str(c) + '.png', frame)  # 存儲(chǔ)為圖像,保存名為文件夾名
                    cv2.waitKey(1)
                else:
                    break
        vc.release()
        print('save_success')
        print(folder_name)


save_img()

存在問(wèn)題

讀取路徑問(wèn)題

問(wèn)題:讀取視頻結(jié)果顯示沒(méi)有打開(kāi)視頻,檢查發(fā)現(xiàn)視頻路徑錯(cuò)誤,導(dǎo)致沒(méi)有正確打開(kāi)

解決:可以在讀取之前檢查路徑,即判斷要保存的文件夾是否存在,不存在就創(chuàng)建該文件夾。代碼如下:

if not os.path.exists(path):
    os.makedirs(path)

中文路徑問(wèn)題

問(wèn)題:cv2.imwrite()保存圖像路徑不能存在中文字符,否則無(wú)法保存,并且沒(méi)有任何提示!?。?/p>

解決:改為英文路徑即可。

最終結(jié)果

image-20210325230120160

00001

到此這篇關(guān)于opencv讀取視頻并保存圖像的方法的文章就介紹到這了,更多相關(guān)opencv讀取視頻內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python將ip地址轉(zhuǎn)換成整數(shù)的方法

    python將ip地址轉(zhuǎn)換成整數(shù)的方法

    這篇文章主要介紹了python將ip地址轉(zhuǎn)換成整數(shù)的方法,涉及Python針對(duì)IP地址的轉(zhuǎn)換技巧,需要的朋友可以參考下
    2015-03-03
  • python實(shí)現(xiàn)m3u8格式轉(zhuǎn)換為mp4視頻格式

    python實(shí)現(xiàn)m3u8格式轉(zhuǎn)換為mp4視頻格式

    這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)m3u8格式轉(zhuǎn)換為mp4視頻格式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-02-02
  • Python Pandas對(duì)缺失值的處理方法

    Python Pandas對(duì)缺失值的處理方法

    這篇文章主要給大家介紹了關(guān)于Python Pandas對(duì)缺失值的處理方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Python Pandas具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • 最新評(píng)論