Not able to play video in opencv (Python 2.7) - python

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()

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
%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:
Remember! to restart your console or your IDE so it get updated/fresh variables.
Note:
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.
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.
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.

Related

Importing a module works in IDE but doesn't work in windows command line (edited)

I am new to Python so this may be a stupid question, sorry in advance if it is so!
The paths Python searches for modules using Spyder 4.1.4.:
[.., 'D:\Users\Martin\Anaconda3\lib\site-packages', ..]
The paths Python searches for when used via command line in windows:
[.., 'D:\Users\Martin\Anaconda3\lib\site-packages', ..]
Thus, both include path to
'D:\Users\Martin\Anaconda3\lib\site-packages'
where I store my cv2 module installed. Yet, Spyder can import cv2 and to my surprise cmd cannot import cv2 with an error No module named 'cv2'. What am I missing please? Can anyone verify if it works for him in command line please? (I changed/edited the question again to cut unneccessary long talk, though the essence stayed the same, in hope someone could answer).
I solved it. If you have a similar problem, you can check if your case is similar to mine and follow the steps.
I use Spyder and everything works fine. However my goal is to use Python via cmd. This could not be done because Python via cmd could not find my modules.
What I did: After typing python in cmd, Python worked and output its version by default. However, I noticed the version was different than version running in Spyder - I had two Python installations (one of which I didn't know about). I found the installation cmd uses by import sys and print(sys.executable). I uninstalled that Python installation.
Now, typing python in cmd does not do anything, it is an unknown command. Then I followed this guide to add my Python installation to Path (using Windows) by:
My Computer > Properties > Advanced System Settings > Environment Variables >
in System variables, I found "Path", clicked Edit, New, then input my Python installation path D:\Users\Martin\Anaconda3 as well as D:\Users\Martin\Anaconda3\Scripts and D:\Users\Martin\Anaconda3\Library\bin. This solved it for me.

Python not recognising libraries on Windows machine

I have been having a lot of trouble on my work Windows machine. I cannot get any libraries to work on my python. I use Thonny application for my python scripting.
I have tried setting environmental path to my current directory of python. On my PC its :
C:\Users\petrikas.lu\AppData\Local\Programs\Python\Python38-32\Scripts
Most tutorials say that it should be on C:\Python34\Scripts however thats not the case for my PC.
enter image description here
Python directory:
enter image description here
Can someone suggest me what shall I do next? I have tried many things that I have found online but my script wont compile. I am trying to import pandas library
After running a command:
python -m pip install pandas
It does not return any result. Most certainly did not generate a pandas library in my libs python folder
I would recommend setting up a Conda environment (using Anaconda or Miniconda) for your Windows machine. It's much easier to manage different environments which contain different sets of libraries.

Does any one has idea to install Opencv in spyder?

import cv2
Traceback (most recent call last):
File "", line 1, in
import cv2
ImportError: DLL load failed: The specified module could not be found.
Install Anaconda
Install OpenCV-Python to Anaconda
The following instruction works for me is inspired by this OpenCV Youtub video. So far I have got it working on both my Desktop and Laptop. Both 64-bit machines and Windows 8.1.
Download OpenCV Package
Firstly, go to the official OpenCV site to download the complete OpenCV package. Pick a version you like (2.x or 3.x). I am on Python 2.x and OpenCV 3.x - mainly because this is how the OpenCV-Python Tutorials are setup/based on.
Copy and Paste the cv2.pyd file
The Anaconda Site-packages directory (e.g. C:\Users\Johnny\Anaconda\Lib\site-packages in my case) contains the Python packages that you may import. Our goal is to copy and paste the cv2.pyd file to this directory (so that we can use the import cv2 in our Python codes.).
To do this, copy the cv2.pyd file...
From this OpenCV directory (the beginning part might be slightly different on your machine):
Python 2.7 and 32-bit machine:
C:\opencv\build\python\2.7\x84
Python 2.7 and 64-bit machine:
C:\opencv\build\python\2.7\x64
To this Anaconda directory (the beginning part might be slightly different on your machine):
C:\Users\Johnny\Anaconda\Lib\site-packages
After performing this step we shall now be able to use import cv2 in Python code. BUT, we still need to do a little bit more work to get FFMPEG (video codec) to work (to enable us to do things like processing videos.)
Set Enviromental Variables
Right-click on "My Computer" (or "This PC" on Windows 8.1) -> left-click Properties -> left-click "Advanced" tab -> left-click "Environment Variables..." button.
Add a new User Variable to point to the OpenCV (either x86 for 32-bit system or x64 for 64-bit system.) I am currently on a 64-bit machine.
32-BIT OR 64 BIT MACHINE? VARIABLE VALUE
32-bit OPENCV_DIR C:\opencv\build\x86\vc12
64-bit OPENCV_DIR C:\opencv\build\x64\vc12
Append %OPENCV_DIR%\bin to the User Variable PATH.
For example, my PATH user variable looks like this...
Before:
C:\Users\Johnny\Anaconda;C:\Users\Johnny\Anaconda\Scripts
After:
C:\Users\Johnny\Anaconda;C:\Users\Johnny\Anaconda\Scripts;%OPENCV_DIR%\bin
This is it we are done! FFMPEG is ready to be used!
Test to confirm
We need to test whether we can now do these in Anaconda (via Spyder IDE):
Import OpenCV package
Use the FFMPEG utility (to read/write/process videos)
Test 1: Can we import OpenCV?
To confrim that Anaconda is now able to import the OpenCV-Python package (namely, cv2), issue these in the IPython Console:
import cv2
print cv2.version
If the package cv2 is imported ok with no errors, and the cv2 version is printed out, then we are all good! Here is a snapshot:
import-cv2-ok-in-anaconda-python-2.png
Test 2: Can we Use the FFMPEG codec?
Place a sample input_video.mp4 video file in a directory. We want to test whether we can:
read this .mp4 video file, and
write out a new video file (can be .avi or .mp4 etc.)
To do this we need to have a test python code, call it test.py. Place it in the same directory as the sample input_video.mp4 file.
This is what test.py may look like (Note: many thanks to Pete's and Warren's suggestions in the comment field - I have replaced my original test code with his - please test it yourself and let us know if this works better):
import cv2
cap = cv2.VideoCapture("input_video.mp4")
print cap.isOpened() # True = read video successfully. False - fail to read video.
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter("output_video.avi", fourcc, 20.0, (640, 360))
print out.isOpened() # True = write out video successfully. False - fail to write out video.
cap.release()
out.release()
This test is VERY IMPORTANT. If you'd like to process video files, you'd need to ensure that Anaconda / Spyder IDE can use the FFMPEG (video codec). It took me days to have got it working. But I hope it would take you much less time! :)
Note: one more very important tip when using the Anaconda Spyder IDE. Make sure you check the Current Working Directory (CWD)!!!
Next step
Now that we have the OpenCV-Python package (cv2), a potential good next step may be to checkout the OpenCV-Python Tutorials.

tesseract command not working from command line in windows

I installed tesseract-OCR for windows and it resides in C:\Program Files\Tesseract-OCR path in my system.
I set up an environment variable by adding C:\Program Files\Tesseract-OCR in the PATH variable.
I also set up TESSDATA_PREFIX in system variable to the same above tesseract location.
Still, when I try to run the command "tesseract some path\image.tif somepath\output", it gives message as "'tesseract' is not recognized as an internal or external command".
when i run the same command from the location where tesseract is installed, it works fine but i need to have it set in the environment variables as it will also allow PYOCR wrapper to recognise it.
PYOCR is currently giving "pyocr.get_available_tools()[0]" as empty list.
any help is much appreciated.
Perhaps my answer is a little bit late, but I will give my setup that seems to work from few minutes. As you, I was using PYOCR library, and at the beginning I get an empty list when using pyocr.get_available_tools().
I'm running it on a Windows 10 laptop.
First, I installed Tesseract in version 4 in alpha from here:
https://github.com/UB-Mannheim/tesseract/wiki and then added location in my windows user PATH variable. From here, run a new command line and check that tesseract tool is detected, if not you're environmment is not properly configured!
Then, I installed PyOCR using a simple pip pyocr and use the follow imports
before using pyocr functions:
import pyocr
import pyocr.builders
EDIT:
I've look in current source files of PYOCR and I've found this:
def is_available():
_set_environment()
return util.is_on_path(TESSERACT_CMD)
PyOCR is just looking for tesseract.exe in your os Path variable, so I think you just have to solve the mention issue and it will work.
EDIT 2:
I updated the download link with UB Mannheim version. Seems more efficient on my data. Also, I downgraded to Tesseract 3, because a lot of flags was not supported in 4.0 alpha build.
Hope it can help a little bit,
Best regards
Please check the correct path of installation of Tesseract-OCR. Setting the correct path, i.e. C:\Program Files (x86)\Tesseract-OCR worked for me.

Why python.exe stopped working?

I try to use OpenCV for Python. I have just two lines of the code:
import cv
capture = cv.CreateFileCapture('test.avi')
If I run this code from the command line, Windows creates a window with the following message:
python.exe stopped working
A problem caused the program to stop working correctly.
Windows will close the program and notify you if a solution is available.
What can be the a reason of that?
I would like to add some details. Not sure if they are relevant. In the examples that I found people use cvCreateFileCapture instead of cv.CreateFileCapture. But in this case my program generate a NameError (cvCreateFileCapture is not found).
In general I was able to do a simple stuff with the OpenCV (so, it is installed and it works). For example I was able to change format of an image:
import cv
im = cv.LoadImageM("test.jpg")
print type(im)
cv.SaveImage("test.png", im)
ADDED
"In OpenCV2.2\samples\python" I found many *.py samples. I run some of them and they work fine (I see some animation and so on). The I tried to find a file that contains "CreateFileCapture". I found only one such file (minidemo.py) and I run it. As a result I got the same problem as described above.
Python on opencv has changed, see here make sure you have the latest opencv install - you might also need numpy

Categories

Resources