2

I've been having issues getting OpenCV 2.4.9 to read (and show) a video file using python. I know it's not a problem with finding the file because I was able to get cv2.imread() to read an image from the same directory. It's definitely a problem with cv2.VideoCapture(). Here's the code and output I've been getting:

import cv2
import numpy as np

    testvid = cv2.VideoCapture('testvid.avi')

while(1):
    ret, frame = testvid.read()
    print ret

    cv2.imshow('frame',frame)
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break

testvid.release()
cv2.destroyAllWindows()

Output:

False

error: C:\pisi\tmp\opencv-2.4.9-5\work\opencv-2.4.9\modules\highgui\src\window.cpp:261: error: (-215) size.width>0 && size.height>0 in function cv::imshow 
OpenCV Error: Assertion failed (size.width>0 && size.height>0) in cv::imshow, file C:\pisi\tmp\opencv-2.4.9-5\work\opencv-2.4.9\modules\highgui\src\window.cpp, line 261

I know there's been several people who have had the same problem. This thread and this tread both address the issue by saying that you have to rename the opencv_ffmpeg.dll file to fit your version and copy it to the main python directory or directory in your path (or something to that effect).

However, I'm using Python 2.7.13 through Enthought (by recommendation from my professor), and the paths and way libraries and packages are set up seem completely different from how they are in the other threads. Enthought uses a package manager that automatically downloaded and setup OpenCV for me. There is no Python directory. Everything is through Enthought. After some digging, I found where all of the OpenCV .dlls are located, including opencv_ffmpeg249_64.dll:

C:\Users\USERNAME\AppData\Local\Enthought\Canopy\edm\pkgs\03\9d58e152b8f411718220c30639b8448585cb7d9a82de662c7e34b35d9ec1f6

So what do I do with this? Where do I copy the .dll? Is the .dll even the problem here? I've tried so many things, including copying it to all sorts of places and renaming the .dll, and nothing has worked. Help!

Spaceguy44
  • 21
  • 1

3 Answers3

1

In order to solve your problem I'd suggest you to restart from scratch and execute the following steps:

1 - Install a fresh standard version of Python 3 on your machine

2 - Download the get-pip.py file from https://bootstrap.pypa.io/get-pip.py

3 - Open a terminal and cd to the folder where you downloaded the get-pip.py file

4 - Then type python3 get-pip.py on your terminal in order to install pip

5 - Once that pip is installed, you are ready to install cmake by typing pip install cmake on your terminal

6 - Now go to https://github.com/opencv/opencv and download the entire repository of opencv

7 - On your terminal cd to the folder where you downloaded the opencv repository and compile it with cmake typing

mkdir release

cd release

cmake -D CMAKE_BULD_TYPE=RELEASE -D BUILD_PYTHON_SUPPORT=ON USE_V4L=ON WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON USE_GStreamer=ON ..

make

make install

8 - Now cd to the directory where yourscript.py is located and you should be able to execute your script with no problem typing on your terminal python3 yourscript.py

Let me know if this solves the issue

Hope this helps

Employee
  • 3,109
  • 5
  • 31
  • 50
  • Thanks for the advice. I downloaded Python 3 and got pip working, however, I couldn't get cmake to work correctly. I tried another method where I installed opencv by having pip dowload a .whl file for opencv. But now when I run the program, I get this: `ImportError: DLL load failed: %1 is not a valid win32 application` – Spaceguy44 Nov 11 '17 at 01:12
  • I should also clarify that I'm using windows 10. – Spaceguy44 Nov 11 '17 at 01:19
  • Do not try to install opencv by pip installing its .whl file: it will not work. Could you please provide details about the problems you are encountering with cmake? – Employee Nov 12 '17 at 12:55
1

The problem (at least as far as I can tell without having your test file) is that your code fails to check the value of ret, after testvid.read(). When this call returns False, then there are no more frames to display, so not surprisingly, imshow() will fail.

Jonathan March
  • 5,800
  • 2
  • 14
  • 16
0

Do not do anything in that directory -- that is a package manager private cache directory and if you mess with it, who knows what you'll break. Your Canopy python environment is in Canopy\edm\envs\User.

Without further investigation I don't know whether your script should run in Python 2.7, but if you want to try in Python 3 as suggested by the first answer, you can easily install a Python 3.5 environment in your Canopy installation, following these instructions: http://docs.enthought.com/canopy/configure/python-environments.html

Jonathan March
  • 5,800
  • 2
  • 14
  • 16
  • I tried it with Python 3.5 in Enthought Canopy and it gives me a similar error. This time Canopy installed opencv 3.2.0. – Spaceguy44 Nov 11 '17 at 03:16