Python opencv videocapture无法使用python3打开文件,但可以使用python2

2024-09-28 03:21:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经尝试了很多方法,包括从源代码构建(包括opencv和ffmpeg以及编解码器),但似乎没有任何帮助。我有一个mp4文件,在python2.7环境下可以正常工作,但是在python3.5中无法加载。

我知道我安装了不同的版本,但是我不明白为什么opencv无法为python3.5环境加载。我检查了路径等,没发现什么问题。

Python 2.7.12:

Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.3.1'
>>> a=cv2.VideoCapture("Serenity.mp4")
>>> a.isOpened()
True

Python 3.5.2:

^{pr2}$

我已经完成了以下工作:

视频样本信息:

ffprobe version 3.4 Copyright (c) 2007-2017 the FFmpeg developers
  built with gcc 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.5) 20160609
  configuration: --enable-pic
  libavutil      55. 78.100 / 55. 78.100
  libavcodec     57.107.100 / 57.107.100
  libavformat    57. 83.100 / 57. 83.100
  libavdevice    57. 10.100 / 57. 10.100
  libavfilter     6.107.100 /  6.107.100
  libswscale      4.  8.100 /  4.  8.100
  libswresample   2.  9.100 /  2.  9.100
[mov,mp4,m4a,3gp,3g2,mj2 @ 0x3d57b00] stream 0, timescale not set
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'Serenity.mp4':
  Metadata:
    major_brand     : isom
    minor_version   : 1
    compatible_brands: isomavc1
    creation_time   : 2007-05-30T05:20:31.000000Z
    genre           : Trailer
    artist          : Universal Pictures
    title           : Serenity - HD DVD Trailer
    date            : 2005
  Duration: 00:02:19.71, start: 0.000000, bitrate: 4802 kb/s
    Stream #0:0(und): Video: h264 (High) (avc1 / 0x31637661), yuv420p, 1280x720, 4674 kb/s, 23.98 fps, 23.98 tbr, 24k tbn, 47.95 tbc (default)
    Metadata:
      creation_time   : 2007-05-30T05:20:31.000000Z
      handler_name    : GPAC ISO Video Handler
    Stream #0:1(und): Audio: aac (LC) (mp4a / 0x6134706D), 48000 Hz, stereo, fltp, 127 kb/s (default)
    Metadata:
      creation_time   : 2007-05-30T05:20:34.000000Z
      handler_name    : GPAC ISO Audio Handler
    Stream #0:2: Video: mjpeg, yuvj420p(pc, bt470bg/unknown/unknown), 240x240 [SAR 1:1 DAR 1:1], 90k tbr, 90k tbn, 90k tbc

编辑:我还尝试过从源代码处编译,根据注释中的建议(并交换.so文件): https://www.pyimagesearch.com/2016/10/24/ubuntu-16-04-how-to-install-opencv/


解决方法:经过多次尝试,我不得不清除我的lubuntu16.04.3系统并重新构建它。请参阅下面的详细答案。


Tags: 方法defaultstream视频kb源代码timeversion
1条回答
网友
1楼 · 发布于 2024-09-28 03:21:28

在新安装了lubuntu并添加了dev包之后,我成功地编译了opencv 3.3.1,然后打开了python3:

import cv2
cv2.__version__

报告的3.3.0是错误的

^{pr2}$

告诉我导入位置,所以我将其重命名为cv2.cpython-35m-x86_64-linux-gnu.so.旧. 然后使用

find /usr/local/lib/ -type f -name "cv2*.so"

我找到了我刚刚构建的一个,并将其移动到cv2中。文件位置,这个方法奏效了。在

FYI还使用以下一组命令编译了opencv:

#Remove any previous installations of x264</h3>
sudo apt-get remove x264 libx264-dev

#Install dependencies now

sudo apt-get install build-essential checkinstall cmake pkg-config yasm
sudo apt-get install git gfortran
sudo apt-get install libjpeg8-dev libjasper-dev libpng12-dev

# LUbuntu 16.04
sudo apt-get install libtiff5-dev

sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev
sudo apt-get install libxine2-dev libv4l-dev
sudo apt-get install libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev
sudo apt-get install qt5-default libgtk2.0-dev libtbb-dev
sudo apt-get install libatlas-base-dev
sudo apt-get install libfaac-dev libmp3lame-dev libtheora-dev
sudo apt-get install libvorbis-dev libxvidcore-dev
sudo apt-get install libopencore-amrnb-dev libopencore-amrwb-dev
sudo apt-get install x264 v4l-utils

# Optional dependencies
sudo apt-get install libprotobuf-dev protobuf-compiler
sudo apt-get install libgoogle-glog-dev libgflags-dev
sudo apt-get install libgphoto2-dev libeigen3-dev libhdf5-dev doxygen

# clone it
git clone https://github.com/opencv/opencv.git
cd opencv 
git checkout 3.3.1 
cd ..

git clone https://github.com/opencv/opencv_contrib.git
cd opencv_contrib
git checkout 3.3.1
cd ..

cd opencv
mkdir build
cd build

cmake -D CMAKE_BUILD_TYPE=RELEASE \
      -D CMAKE_INSTALL_PREFIX=/usr/local \
      -D INSTALL_C_EXAMPLES=ON \
      -D INSTALL_PYTHON_EXAMPLES=ON \
      -D WITH_TBB=ON \
      -D WITH_V4L=ON \
      -D WITH_QT=ON \
      -D WITH_OPENGL=ON \
      -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \
      -D BUILD_EXAMPLES=ON ..

# depending on your processor cores use -j2 for 2 cores, -j4 for 4
make -j2
sudo make install

相关问题 更多 >

    热门问题