Not able to play video in opencv (Python 2.7)

Simon Buus Jensen

I have the following working code in python using opencv (it is tested on another environment), but for some reason I tried to play the video on my computer with Windows 10, Python27 and OpenCV 3.0.0. And it does not play.

Question: what can I do to play video in opencv under python 2.7? and what I am doing wrong?

Here is my code:

import cv2

cap = cv2.VideoCapture("output.avi")
cv2.namedWindow("input")
f = True
while (f):
    f, img = cap.read()
    if f == True:
        cv2.imshow("input", img)
        ch = cv2.waitKey(33)
        if ch == 32:
            break

cap.release()
cv2.destroyAllWindows()
maytham-ɯɐɥʇʎɐɯ

I have had the similar issue, and I have fixed the following way:

In order to get your Python 2.7 to play videos using OpenCV library, you need to have the proper OpenCV codex.

To confirm if it is an OpenCV codex issue, first you need to copy opencv_world300.lib TEMPORARY from you OpenCV library to your Python folder. In my case OpenCV installed under (you should check where your OpenCV installed)

C:\lib\opencv\build\x86\vc12\lib

As mentioned copy it to your Python folder, like again in my case c:\python27 (find out where your Python folder is located).

Than start to compile your code and see if you can play your video.

If that helps and succeeded, this means your path and environment variables is not correctly set. Therefore, you need to set the following path and environment variables:

Go in your windows System -> Advance system settings -> Environment Variables (if you don't know how look at this)

And add following in User variables, this is how my variables are setup.

Edit your variable name path and add new variable values:

%OPENCV_HOME%\x86\vc12\bin
%PYTHON_HOME%
%PYTHON_HOME%\Scripts
%PYTHON_HOME%\DLLs

So it will look like this in Windows enter image description here

%OPENCV_HOME%\x86\vc12\bin;%PYTHON_HOME%;%PYTHON_HOME%\Scripts;%PYTHON_HOME%\DLLs;....etc.

Create new variable name and variable value for following three components:

PYTHON_HOME=C:\Python27\

OPENCV_HOME=C:\lib\opencv\build

NUMPY_HOME=%PYTHON_HOME%\Lib\site-packages\numpy

So it will look like this in Windows for Python as example and the rest is the same way: enter image description here

Remember! to restart your console or your IDE so it get updated/fresh variables.

Note:

  1. This way it should automatically find opencv_world300.lib and remember to delete it from Python folder as we move it only to clarify if that was the issue.
  2. You need to type the variables manually and not copy and paste, some times in Windows you might get a lot of problems by just copying them, I have had that issue. Spcially with (_) Underscore.
  3. This solution is valid for OpenCV 3.0.0 and python 2.7.11 and latest windows 10, I have not test it on other environment.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Not able to play .h264 video on OpenCV?

From Dev

Python OpenCV video format play in browser

From Dev

Can not Read or Play a Video in OpenCV+Python using VideoCapture

From Dev

Error when trying to play video in Xcode 7 / Swift 2

From Dev

openCV video saving in python

From Dev

OpenCV (Python) video subplots

From Dev

No video output OpenCV Python

From Dev

Read a video in opencv (python)

From Dev

Python opencv video writer

From Dev

Not able to open video files using OpenCV VideoCpature java class in Android

From Dev

Python Script to play a Vimeo Video

From Dev

Saving a video capture in python with openCV : empty video

From Dev

Saving a Video in OpenCV (Linux, Python)

From Dev

Cannot open video in OpenCV (Python)

From Dev

Python Opencv2 Camera with Start Flashlight, Stop Flashlight, Click Picture, Start Video Recording, Stop Video Recording buttons

From Dev

How to play 2 video at the same time?

From Dev

How to Play Video in Unity 2D?

From Dev

How to play rtsp live video in ionic 2?

From Dev

OpenCV Python Video playback - How to set the right delay for cv2.waitKey()

From Dev

Unable to write video using python and opencv2 on mac os x

From Dev

Python OpenCV video.get(cv2.CAP_PROP_FPS) returns 0.0 FPS

From Dev

How to show multiple video frames in a single window using python opencv2

From Dev

Python: Play Video file from a Buffer

From Dev

JavaScript Play Button 1 Starts Video for Play Button 2

From Dev

OpenCV play 2 videos same time

From Dev

How to record video using OpenCV and Python?

From Dev

Specify Compression Quality in Python for OpenCV Video Object

From Dev

How to Capture Video Stream with OpenCV (Python)

From Dev

Limiting video capture frame rate on python and opencv

Related Related

  1. 1

    Not able to play .h264 video on OpenCV?

  2. 2

    Python OpenCV video format play in browser

  3. 3

    Can not Read or Play a Video in OpenCV+Python using VideoCapture

  4. 4

    Error when trying to play video in Xcode 7 / Swift 2

  5. 5

    openCV video saving in python

  6. 6

    OpenCV (Python) video subplots

  7. 7

    No video output OpenCV Python

  8. 8

    Read a video in opencv (python)

  9. 9

    Python opencv video writer

  10. 10

    Not able to open video files using OpenCV VideoCpature java class in Android

  11. 11

    Python Script to play a Vimeo Video

  12. 12

    Saving a video capture in python with openCV : empty video

  13. 13

    Saving a Video in OpenCV (Linux, Python)

  14. 14

    Cannot open video in OpenCV (Python)

  15. 15

    Python Opencv2 Camera with Start Flashlight, Stop Flashlight, Click Picture, Start Video Recording, Stop Video Recording buttons

  16. 16

    How to play 2 video at the same time?

  17. 17

    How to Play Video in Unity 2D?

  18. 18

    How to play rtsp live video in ionic 2?

  19. 19

    OpenCV Python Video playback - How to set the right delay for cv2.waitKey()

  20. 20

    Unable to write video using python and opencv2 on mac os x

  21. 21

    Python OpenCV video.get(cv2.CAP_PROP_FPS) returns 0.0 FPS

  22. 22

    How to show multiple video frames in a single window using python opencv2

  23. 23

    Python: Play Video file from a Buffer

  24. 24

    JavaScript Play Button 1 Starts Video for Play Button 2

  25. 25

    OpenCV play 2 videos same time

  26. 26

    How to record video using OpenCV and Python?

  27. 27

    Specify Compression Quality in Python for OpenCV Video Object

  28. 28

    How to Capture Video Stream with OpenCV (Python)

  29. 29

    Limiting video capture frame rate on python and opencv

HotTag

Archive