天天看點

ubuntu系統 - python中用cv2.VieoCapture()讀取視訊失敗,咋整?如果用盡方法仍然無法解決,不妨換個别的庫吧

近日在Python環境中用cv2.VideoCapture()讀取視訊時,cv2.VideoCapture.open()始終傳回False。去百度一查,遇到該情況的一大堆。然後檢視opencv的官方文檔,發現如下内容:

以下内容的出處:https://pypi.python.org/pypi/opencv-python
Q: Why I can’t open video files on GNU/Linux distribution X or on macOS?
A: OpenCV video I/O depends heavily on FFmpeg. Manylinux and macOS OpenCV binaries are not compiled against it. The purpose of these packages is to provide as easy as possible installation experience for OpenCV Python bindings and they should work directly out-of-the-box. Adding FFmpeg as an additional dependency without a “universal” FFmpeg build (e.g. LGPL licensed build like in the Windows wheels) the goal is considerably harder to achieve. This might change in the future.
           

大緻原因是沒有編解碼器,或者編解碼器安裝的不正确。那咋解決呢?在stackoverflow有一篇文章基本上總結的比較全:

// https://stackoverflow.com/questions/31040746/cant-open-video-using-opencv
           

基本思路就是:

1. git下載下傳ffmpeg源碼,編譯(具體編譯過程文章裡很詳細)

2. gitc下載下傳opencv源碼,編譯(具體編譯過程文章裡很詳細)。最最最重要的一點,cmake選項一定要加python支援:

cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_QT=OFF -D WITH_V4L=ON -D CMAKE_SHARED_LINKER_FLAGS=-Wl,-Bsymbolic ..
           

當然其他的選項,看自己情況加,比如opencv的contrib子產品等。可以用cmake-gui,能直覺的看到那些功能勾上了,那些沒勾上。

編譯opencv大家都知道,需要半個小時左右,可以去喝杯咖啡再來。

完成後,本帖題目的問題就搞定了。但各種奇怪的事情總會發生,如何還不行,咋整!?

----分割線----

在Python中,可以讀視訊的庫,除了opencv外,還有一個——imageio,這個庫的具體介紹見:

https://github.com/imageio/imageio
           

這個庫的核心函數如下:

imread() and imwrite() - for single images
mimread() and mimwrite() - for image series (animations)
volread() and volwrite() - for volumetric image data
get_reader() and get_writer() - for more control (e.g. streaming)
           

可以看到它就是用來讀圖像、視訊檔案、camera等的。

imageio依賴一下環境:

Python 3.x, 2.7 or 2.6
Numpy
Pillow
           

所有,如果想自己編譯源碼,要提前安裝所依賴的庫;如果用pip,就一條指令就夠了:

sudo pip install imageio
           

----分割線----

實際上,很多人都還是習慣用opencv。在Python中,opencv的圖像是一個np.array。我們用imageio讀取的圖像資料要想轉換成np.array,還需要一個庫,就是skimage:

這個庫的官網:

http://www.scikit-image.org/
           

安裝很簡單:

sudo pip install scikit-image
           

----分割線----

好了接下來是一個執行個體:

import sys
import os
import random
import math
import numpy as np
import linecache
import string
import imageio
import skimage

if __name__ == "__main__":
    print '--STA--'
    strPath = '/home/raintai/local_visual_algorithm/samples/5cc4bc32-64ad-40e4-93e8-b2ab88aa684f.init.mp4'
    if (os.path.exists(strPath) == False):
        print 'file not exist'
    # use imageio to read video file
    curVideo = imageio.get_reader(strPath)
    for i, img in enumerate(curVideo):
        opencvImg = skimage.img_as_ubyte(img, True)
        # 下邊就回到熟悉的opencv套路上了
		print opencvImg.shape
    print '--END--'
           
import sys
import os
import random
import math
import numpy as np
import linecache
import string
import imageio
import skimage
import cv2

if __name__ == "__main__":
    print '--STA--'
    strPath = '/home/raintai/local_visual_algorithm/samples/5cc4bc32-64ad-40e4-93e8-b2ab88aa684f.init.mp4'
    if (os.path.exists(strPath) == False):
        print 'file not exist'
    # use imageio to read video file
    videoReader = imageio.get_reader(strPath)
    FrameNum = videoReader.get_length()
    print 'frame num = ', FrameNum
    for i in range(0, FrameNum):
        img1 = videoReader.get_data(i)
        grayImg = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
        print '%5d'%i, grayImg.shape
    print '--END--'