I have always struggled when trying to set up OpenCV for Python before. So I decide to write this post to help myself in the future and share with you =). My setup is for OpenCV 2.2 but I think you can apply for any version of OpenCV.
Step 1: Download and install Python 2.7 from http://www.python.org/getit/releases/2.7.2/. You need to install the 32bit version of Python. OpenCV currently does not work with Python 64bit.
Step 2: Download and install OpenCV 2.2 from http://sourceforge.net/projects/opencvlibrary/files/. Note that this version only supports Python 2.7 (not 3.x).
Step 3: Download and install NumPy 1.6.1 and SciPy 0.9.0 from: (you need to choose the files which support Python 2.7)
- http://sourceforge.net/projects/numpy/files/NumPy/1.6.1/
- http://sourceforge.net/projects/scipy/files/scipy/0.9.0/
Step 4:Setup Windows Path in Environment Variables
- Add “C:/Python2.7;C:/OpenCV2.2/bin” to PATH variable (You need to change the directory to where you install Python and OpenCV).
- Create PYTHONPATH variable and put “C:/OpenCV2.2/Python2.7/Lib/site-packages” as value.
Update: For OpenCV2.3, There are two other options for this step:
- As Kyle commented below, you can copy the content of folder “C:\opencv\build\python\2.7” (for opencv 2.2: C:/OpenCV2.2/Python2.7/Lib/site-packages) to folder “C:\Python27\Lib\site-packages“
- OR, add these two lines at the beginning of your program:
import sys sys.path.append("C:\OpenCV2.2\Python2.7\Lib\site-packages")
Yosh!!! You have finished setup OpenCV for Python in Windows. Open Python IDLE, create a new file, add the program below and run it:
import cv
cv.NamedWindow("camera", 1)
capture = cv.CaptureFromCAM(0)
while True:
img = cv.QueryFrame(capture)
cv.ShowImage("camera", img)
if cv.WaitKey(10) == 27:
break
cv.DestroyWindow("camera")
(This is the program to show your webcam)
UPDATE:
- OpenCV has just released version 2.3.1 which is more stable than 2.2 I think. You can download it here and try it (the setup is same as 2.2).
- About the black screen you encounter with the example, I think it’s because your camera is not supported. My camera works fine. You can find more information here.
I'm Thuy, a final year Computer Science student from National University of Singapore (NUS). I currently live in Singapore. I am interested in software engineering, mobile application and game development.
You da man– this was described perfectly! Thanks so much!
I can not get video from file, could you indicate waht is wrong?
the programm below get a None frame, the file exist on the path, open the window but without image.
I print : capture= and frame= None
The programm is :
import sys
sys.path.append(“C:\\Python27\\opencv\\modules\\python\\src2″)
sys.path.append(“C:\\Python27\\opencv\\modules”)
sys.path.append(“C:\\Python27\\opencv\\modules\\highgui\\include\\opencv2″)
import cv
if __name__ == ‘__main__’:
cv.NamedWindow(“Example2″, 1)
capture=cv.CaptureFromFile(“C:\\Python27\\PruebasRLOpy27\\Megamind.avi”)
print ‘capture=’,capture
loop = True
while(loop):
frame = cv.QueryFrame(capture)
print ‘frame=’,frame
if (frame == None):
break;
cv.ShowImage(“Example2″, frame)
char = cv.WaitKey(33)
if (char != -1):
if (ord(char) == 27):
loop = False
cv.DestroyWindow(“Example2″)
Pingback: Using embedded vision for autonomous car obstacle avoidance | Jeff's Inventions
Hi,
Thanks man, you are great.
simple and effective…
it worksssssssssssssss
Extremely Helpful. Thanks a ton
Oh man, have been trying forever to get this to work, and it’s so simple. Thanks for the guide!
Many thanks – worked first time!
Hi again,
I wanted to ask if it is possible to adjust brightness or zoom-in from the code above? Are there any functions available in the OpenCV for this specific purpose?
Thanks!
thank you alot
do you tink i can install opencv on a 64 bit cumputer
You just made everything go so smoothly!
Thanks for such awesome post.
Luu Gia Thuy! Thanks so much! This is great.
However, I tried doing this with a later version of OpenCV. 2.4 and the directory structure has changed completely.
I have copied the files from “modules/python/src2/” to python library.
However where is the bin files? There are like 6-8 different bins, GPU, x86,vc9, vc10, mingw?
which one do I set the PATH variables to? Please help, and thanks in advance
Thanks Raymond!
Been at this for hours as I decided to upgrade my version of OpenCV on Windows 7-64(not my call, it’s for a client).
This helped immensely, although I am still having issues with these Python modules loading the appropriate DLLs, your comment has brought me much closer. This should be included in the Install instructions.
Tested on Linux and it works fine. thanks
I am trying to get OpenCV up and running with Python 2.7 (or 2.6) on a Windows Vista (32-bit) platform; but, have not been able to complete the following from your setup instructions
Step 2: Download and install OpenCV 2.2 from http://sourceforge.net/projects/opencvlibrary/files/. Note that this version only supports Python 2.7 (not 3.x).
I actually have downloaded both 2.2 and 2.3 and I have NumPy, SciPy, CMake, etc. all installed. I also have cygwin (for C/C++ compilers). I have unzipped the *.exe for OpenCV; but, then I start to have problems.
Please provide some more detail on the actual installation of OpenCV (either 2.2 or 2.3) for either Python 2.6 or 2.7.
Best regards,
–V. Stokes
Uppsala, Sweden
Thanks. Great work!
Although I have python 2.6 but your tutorial was very helpful to me.
Thank you very much for your explanations.
This made setting up opencv a breeze. Thanks for posting.
Has anybody tried to use OpenCV on Google App Engine ( Python ) ? Is it possible ?
plz reply me i dont know why i didnt get reply from you
Thank you for this post! The instructions on OpenCV’s website are so confusing, I would have been lost without your instructions. I’ve finally got Opencv 2.3 running with Python.
Thanks!
You’re welcome Ashish! =)
i am getting the following error,
RuntimeError: module compiled against API version 6 but this version of numpy is 4
Traceback (most recent call last):
File “C:\OpenCV2.3\opencv\samples\python2\edge.py”, line 1, in
import cv
File “C:\Python27\lib\site-packages\cv.py”, line 1, in
from cv2.cv import *
ImportError: numpy.core.multiarray failed to import
i am using python2.7, openCV2.3.1, numpy for 2.7
hi Luu Gia Thuy ,
i am using python 2.6 and opencv 2.0 to code my academic project.
i have problem in face detection and face recognition code which extracts detected faces from jpeg image.
so, can you please give me a code for face detection and recognition.the code can load a jpeg image draws a rectangular box for detected faces in the image and it should save the face detected faces in another image. another is the face recognised image can be compared with one another image. please i am suffering lot from this problem i hope u will give solution.
For face detection, I think you can find it in OpenCV samples folder. Sorry but don’t have code for face recognition.
thank u can u tell me the path specificaly
Thank you very much! I couldn’t setup exactly with other site’s setup information, but your way worked well!
In my case, I installed Python 2.7 x64 version, so I couldn’t install numpy and scipy. If anyone installed Python like me, then you can re-install Python x86 version and it will be worked very well.
cool man hats off to you , initally i was trying visual studio but was not sucessfull from last 5 days then switch to python it is really nice. once again thanks to you.
Thanks man it helped me alot
Thanks for your explanations.
I did everything but when I try to run 2.3.1 version of facedetect.py, I get…
Traceback (most recent call last):
File “C:\opencv\samples\python\facedetect.py”, line 10, in
import cv2.cv as cv
ImportError: No module named cv2.cv
Hi George, have you copied 2 files cv.py and cv.pyd from C:\opencv\build\python\2.7 to the folder C:\Python27\Lib\site-packages (assuming that your install directories are the same)?
When copying the files across i just seem to get this error
Traceback (most recent call last):
File “C:/Users/Dom/Documents/Python Practice/Open Cv.py”, line 2, in
import cv
ImportError: DLL load failed: The specified module could not be found.
When i down load the file there is only “cv.lib” and “cv.pyd” no” cy.py”
Hi Gia Thuy,thanks a lot for your excellent tutorial,it has been of great help for me.
I had the same problem “ImportError: num py.core.multiarray failed to import” when using openCV 2.2 + python2.7 + visual studio 2008.
The problem was,I guess, that I used OpenCV-2.2.0-win32-vs2010.exe that should not be compatible with vs2008.
I solved the issue installing the following :
OpenCV-2.1.0-win32-vs2008 + ActivePython-2.6.7.20-win32-x86 + vs2008 + numpy-1.6.1-win32-superpack-python2.6 + scipy-0.9.0-win32-superpack-python2.6
With the above your sample code for camera image processing works fine (no need to copy files “cv.py” and “cv.pyd”)
Hope this helps,thanks again very much for your support !
Thank you for your information! =)
Hi !
First of all, thank you very much! This little tutorial has been of great help, since the official install guide ( http://opencv.willowgarage.com/wiki/InstallGuide ) doesn’t really explain, it just mentions all generally and one can get easily lost.
Ok, here I go (I’m a total noob by the way, so please go easy on me
):
I have downloaded and installed OpenCV 2.3.1 (OpenCV-2.3.1-win-superpack.exe), you already mentioned in a response to Chris that the windows superpack version is already compiled, which i didn’t knew at all and was confused about “intall” meaning to download source and compile.
I’m using Windows 7 SP1 32bit, followed your steps through step3, and everything went better than expected. Now, I didn’t understand in step 4, when setting the path in environment variables, i have no “C:/OpenCV2.2/bin” folder on my opencv install, it doesn’t exist. Neither does exist “C:/OpenCV2.2/Python2.7/Lib/site-packages”.
Does this have to do with the fact that I’m using Open CV 2.3.1, and this version does not have those folders in that order? I have about 3 days going on on this.
However, i tried your finall response to Chris,
“Gia Thuy
September 18, 2011 at 9:39 AM
I never thought of that. Can you try to use this compiled version http://sourceforge.net/projects/opencvlibrary/files/opencv-win/2.3.1/OpenCV-2.3.1-win-superpack.exe/download. After that, try to copy 2 files cv.py and cv2.pyd from \build\python\2.7 to folder Python27\Lib\site-packages
Hope you can run it this time =) ”
I copied the 2 files just as you said, and your code to show the webcam worked. But I’m still wondering about the Python and OpenCV path, as i have not created them. Cheking in the windows cmd.exe, Python and OpenCV paths don’t exist. Should I add the paths, or what would you suggest?
Hi Henry,
Step 4 is only applicable for OpenCV2.2. I forgot to note that. Sorry about that. For this step, you need to copy 2 files “cv.py” and “cv.pyd” from folder “C:\opencv\build\python\2.7″ to “C:\Python27\Lib\site-packages” (assume that your directory as like this). Hope this helps.
Oh that explains it. Thank you very much for your help!
http://luugiathuy.com/2011/02/setup-opencv-for-python/comment-page-1/#comment-1287
That worked for me after several attempts! Thank you
Thanks for your help Gia!
-Frank.
You’re welcome Frank =)
I am doing project on object tracking using camera with robot navigation,
ive been trying to create a simple program with python2.7 which utilises opencv2.2 to get a video feed from my webcam and display it on the screen. I know im partly there because the window is created and the light on my webcam flicks on, but it just doesnt seem to show anything in the window. hopefully you can explain what im doing wrong.
this was the program implemented,
import cv
2
3 cv.NamedWindow(“camera”, 1)
4 capture = cv.CaptureFromCAM(0)
5
6 while True:
7 img = cv.QueryFrame(capture)
8 cv.ShowImage(“camera”, img)
9 if cv.WaitKey(10) == 27:
10 break
11 cv.DestroyWindow(“camera”)
I havent installed any library for webcam/usb logitech cam……so please suggest if any libraries are required to be installed.
reply
I believe I followed the guide exactly. I installed python 2.7.2, I installed numpy and scipy versions specified, built opencv according to this guide: http://opencv.willowgarage.com/wiki/MinGW , added the environment variables, restarted the computer, and I still am getting this error: “ImportError: DLL load failed: The specified module could not be found.” on the line “from cv2.cv import *” in cv.py. Any help would be very appreciated, as I have already wasted way too many hours trying to get this to work.
Hi Chris! Did you install the 64-bit python? You need to install the 32-bit version. I think that’s why you got DLL error.
No, I have 32 bit python. Thanks for the replay!
erm, *reply
Any other ideas?
Hi Chris. Can you try open Python IDLE and execute “from cv2.cv import *”?
Same error.
Was the guide I mentioned in OP sufficient for python support? Everybody says “Install open cv”, which turns out to actually mean (at least as far as I have found) to compile and install it (which I think I have done successfully). Are there any binary installers for opencv, or is the only way to get it by source? I don’t completely trust that it compiled successfully, even though it didn’t give compile errors.
Also, if it is important, I am running windows 7 32 bit.
I never thought of that. Can you try to use this compiled version http://sourceforge.net/projects/opencvlibrary/files/opencv-win/2.3.1/OpenCV-2.3.1-win-superpack.exe/download. After that, try to copy 2 files cv.py and cv2.pyd from \build\python\2.7 to folder Python27\Lib\site-packages
Hope you can run it this time =)
Still getting that same error when using the precompiled version. Do I need to extract it to a particular directory?
Hi Thuy,
I installed both Python and OpenCV and get the camera capturing program work.
But still I cannot load an image from a jpg file. I tried it in IDLE and it replies with:
================================RESTART================================
And I’ll have to import cv again.
Have you ever encountered such situation? Thank you.
Alright now. Just some problem in compiling.
Hi,
I’ve tried to install OpenCV 2.2, OpenCV2.3 and 2.3.1 on Windows7 64bit with python 2.7.1 32bit.
OpenCV 2.2 loads but shows black screen when running the sample.
When trying ‘import cv’ with OpenCV 2.3 and 2.3.1 I get:
RuntimeError: module compiled against API version 6 but this version of numpy is 4
Traceback (most recent call last):
File "", line 1, in
ImportError: numpy.core.multiarray failed to import
Does anyone knows how to solve the problem?
Thanks
Hi mike,
Can you try to install the latest version of numpy 1.6.1 for python 2.7 at http://sourceforge.net/projects/numpy/files/NumPy/1.6.1/
I have just tried it and it works find for OpenCV2.3.1
And for the black screen when you run the example. I think it’s because your camera is not supported =( I can’t help with this.
????????
hy,, i have get new problem. i have success installed but when i try your code i didn’t see picture on frame but my webcam is on,, why ?? can u help me
Hi,
My camera works fine for me. I think it’s because your camera is not supported. I found this a thread about this http://stackoverflow.com/questions/7247475/opencv-2-3-c-qtgui-problem-initializing-some-specific-usb-devices-and-setups
Hi,
My python.exe frezeze when i try
import cv
cv.NamedWindow(“Building”,1)
im= cv.LoadImageM(“airplane.jpg”)
cv.ShowImage(“Building”,im)
also is there any tutorial when i can learn the opencv for python and .
rgrds,
kavish
Hi, which version of opencv are u using? I prefer 2.1 or 2.3 since the 2.2 one is not stable I think.
You can looking for the book “Learning OpenCV: Computer Vision with the OpenCV Library”. The code of the book is in C++, but you can easily convert to Python. Hope it helps!
What a shame… only 32bits versions,
it could be reallllllly useful to have a 64 bits ver
especially when you deal with Python…
I get a error saying “ImportError: DLL load failed: %1 is not a valid Win32 application.” when I import cv module, please help me out
Hi Amogha,
I suspect that you are using python 64bit. I think you need 32bit version to use opencv. Hope it helps!
I successfully compiled OpenCV 2.4.1 64bit with VS2010 Express on Win7-64.
I am now stuck on the same error as Amogha.
I really need to use 64bit python for other areas of my application, mainly in that it will be quite a memory intensive application and being limited in RAM(opencv will mainly be used for video) is a very bad idea for my client’s needs.
Are you aware of any bug fixes in the works (also tried to register at cython.org to report my own and am having trouble with their registration process)?
Any insight would be greatly appreciated.
Regards,
John V. Busch
For the record, I had OpenCV 2.1.0 64bit working in Python 64 on Windows 7-64 prior to my latest effort to upgrade to 2.4.1; albeit there seemed to be bugs.
Regards,
John V. Busch
i have the same problem a black frame, think because i’m working with windows 7.
the rest of examples work fine.
with opencv 2.1 i think works, but i need 2.2
Hi,
OpenCV has just released version 2.3. I’ve just tried it and it worked fine on my laptop. I think they have fixed the camera’s problem. May you try it?
Yes, i tried with it, but the problem now is the suport for cv import, cv2 work fine but not cv.
Thanks.
I will try with this version:
http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv
Yes works fine, the two imports (cv and cv2). In addition the web page is very useful.
Thanks jojaquix
Hi,
I cant seem to import cv. Keep getting the error: (import cv2 works fine, but I cant run your example code with cv2)
ImportError: DLL load failed: %1 is not a valid Win32 application.
Do you have any idea how to fix this. Appreciate any help.
Thanks,
Sachin
Hi, try with
http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv
works fine for my on Win 7 x86.
Hi Sachin,
I think it’s because you are using Python 64bit. You should use 32bit version for opencv. Hope it helps!
hi,
U developed a nice application i tryed it out its working fine.
I am using the same bluetooth chat application to develop Bluetooth tic tac toe game .Playing game via bluetooth between two android phones
Can i get ur help .
Hi Thuy,
Have you have run into problems in accessing AVI or MPG files in OpenCV2.2 and Python2.7? Of the 4 computers in which I have installed OpenCV using your detailed instructions, in only one of them the acquisition is functioning… I use that computer to program, and then I create a .exe (using py2exe) that runs well in all of them. But if I try to run for example the simple script in the other 3 PCs:
import cv
videofile = cv.CaptureFromFile(‘a.avi’)
frame = cv.QueryFrame(videofile)
print type(videofile)
print type(frame)
it returns:
type ‘cv.Capture’
type ‘NoneType’
OpenCV seems to be correctly installed in all computers and functions perfectly well for static images (jpgs) and I can run all the examples of the OpenCV Python install. It’s only the videos that are not read… I have installed and uninstalled several codec packages without success (but when I uninstall K-lite for example, not even my .exe functions). So, I suspect it is really a codec problem… Any idea on how to solve this and get it to work in all computers? Thanks.
Good instructions, thanks. I am able to get your code to run, unfortunately the ‘camera’ window is solid black. I can see the LED on my webcam turn on, so it is accessing the camera.
Do you have any suggestions on how to debug this? Do I need to install support for my webcam (Microsoft Lifecam) or configure opencv to read from it properly?
I found this thread on StackOverflow, but no luck using their code yet either:
http://stackoverflow.com/questions/2601194/displaying-webcam-feed-using-opencv-and-python
Thanks.
Hi Ryan,
I have not encountered this problem. Was there a dialog window for selecting the camera? Can you try to pass other values to CaptureFromCAM, such as -1 or 1 instead of 0.
Thank you very much!
One comment:
I actually had to copy the contents of “C:\OpenCV2.2\Python2.7\Lib\site-packages” into “C:\Python27\Lib\site-packages”, otherwise python couldn’t find the cv package and I got “ImportError: No module named cv”
Hi Kyle,
Thank you for your comment. If you set the PYTHONPATH in your environment variable as I describe. You don’t need to copy that folder.
Hi There,
Thanks alot for this wonderful tutorial, it really helped me get where to start in terms of how to install Opencv but well am having problems still. I can’t be able to use the library because i get an error in my IDE(Eclipse).
I followed the steps you listed.
Am using Windows 7 as my Operating system , OpenCV2.2 , Python2.7 plus the rest of the libraries that you talked about for python2.7.
However i get the following error in my IDE when i try to import the cv library.
“Unresolved import:cv”
Have tried to look for a solution for two days now but things have not worked out.
Any help will be greatly appreciated
Hi Elly,
I have not tried Eclipse to code python opencv. Can you add these lines below at the beginning of your code to see whether it work:
import sys
sys.path.append("C:\OpenCV2.2\Python2.7\Lib\site-packages")
You need to change the opencv path to the directory you installed it.
Hope it will work for you
it still don’t work
(
(
Hi,
What’s the problem you encountered?
Hi,
OpenCV’s just released version 2.3 which is more stable than 2.2 I think. I’ve just tried it and it worked fine. May you try it?
You are my hero! I just wasted hours trying to compile openCV with MinGW and Cmake, but it kept failing when it encountered python objects… now I think the problem may have been that I didn’t have scipy installed. Might have to try that…
Thanks for your comment =) Hope that it will solve your problem. Let me know if you encounter any issue =)
Hey gia
When I run the code, the output is all black on the window. When I tried it on another computer, it asked me in a dialog box, displaying camera selected is an integrated cam. Then the screen is just gray.
I added the variables as you said. The scipy and numpy files you have provided links for are not compatible with python 2.7, I had to download the compatible versions.
Another problem which I faced was that even though I had added the environment variables, I had to change the “inlcude” variable path aswell as the “Lib” paths to opencv and python2.7. You didn’t mention that. Other wise I was facing an error with “import cv” command regarding numpy and multiarray crap.
I have run some sample programs in opencv2.2. Some of them run while others give error with “urllib2″ or ”
“sys”. Please help me run the camera
Hi Rehan,
For the scipy and numpy install files, I just provided the links of the folder to download the correct version. You should not download the recommended download file.
After you install python 2.7, then correct version of numpy and scipy (which are supported python 2.7), they will be add to the folder Lib\site-packages of Python. Would you like to check whether they are there or not? If not, I think you need to install numpy and scipy again.
What do you mean “change the include variable and lib paths to opencv and python2.7″. I think we do not need this for opencv to run in python
About the problems regarding to “urllib2″ or “sys”, can you give me the code of the program?
Hello!…. I’m fairly new and I have struggled quite a lot trying to make OpenCV works with Python (specifically in Eclipse)…. in step 4 where do i set the windows path (in the ‘MyComputer’_ properties_ advance_environment variables? if yes, do I create PYTHONPATH in the “user variables” or in the “system variables”?
Thank you
Leo
Hi Leo,
You can create PYTHONPATH in the “system variables” so that other users can use it. You can refer to this for different between “user variables” and “system variables”. Hope it helps.
Cool..thanks!!…. one thing, have you gotten any issue with CaptureFromFile?… whenever i run that command my program crashes…
Leo
Hi Leo, I have also encountered this problem. I have tried to search the solution, but could not find it (The developers of OpenCV have fixed this problem, but I think they have not released the patch yet. You can find it here https://code.ros.org/trac/opencv/ticket/788).
If you want to use this function, I think you should use version 2.1 instead.
Hi Leo,
To build the source, you can follow the instruction here. Hope it helps.
I have just built the source code of OpenCV 2.2 (downloaded from their SVN) in Windows. It worked perfectly for CaptureFromFile
Thank you very much for your explanations. They help me a lot.
You’re welcome
Good job. It took me a lot of time as well. I have done what you did, but at some point it turns out that some functions are not available although described in the documentation. I had to compile OpenCV with C++, and then link Python to OpenCV.
But seems like OpenCV 2.2 has better support for Python ^^.
Thanks Phong