60 thoughts on “OpenCV-Python – How to install OpenCV-Python package to Anaconda (Windows)”
Thank you for the instruction! I will now be checking if I can properly integrate it and use it in my research.
Thanks for the kind words. By the way I have just done a massive update on this post. Please check the revised instruction again! (the old version was more like a hack. This new version is more “official”, require less work, and more consistent). Good luck!
Unarguably the best instruction on the web.
Thank you for the encouraging words Shiva! I hope this post has helped you setting up OpenCV & FFMPEG on Anaconda – it took me 3 days doing trial-and-errors and I really hope that it would take the others much less time in getting things setup :)
Appending my humble thankyou to this comment.
I believe you mean the second and third lines in test.py to read:
cap=cv2.VideoCapture("input_video.mp4")
print cap.isOpened() # True = read video successfully. False - fail to read video.
The cv attribute and the CV_FOURCC macro are missing from opencv 3.0, replaced with VideoWriter::fourcc() (in the C++ implementation, at least). Haven’t got that figured out yet either.
Still haven’t got it to read a test video, but I’m only a couple of hours in :)
Hey Warren – thanks and do let us know how you get on! (at the time of testing the Python code it worked fine on my machine… things might have changed since then though I am not sure – might try out your suggestion later!) Thanks and good luck!!!
thanks for the instructions, saved me a few hours of mucking around, very clear and very appreciated
here are a couple of minor changes to your script to test FFMPEG,
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()
note that your original script fails at a couple of points
(1) in lines 2 and 3 you need to define a handle/(more properly an instance) to cv2.VideoCapture() and test whether that handle points to an open object, (in your code you’re testing whether the library cv2 is an open object)
(2) as one of the other chaps pointed out FOUR_CC has been moved, and the above syntax appears to work
thanks again
Thank you Pete! I have replaced the code in the article with yours – I have not tested this myself though. Please let us know how your test go – I’m sure this will benefit everybody! (I’m surprised this article is now one of the hottest on this site – how exciting :)
Hi Johnny, thanks for your instructions here. It’s really helpful and I appreciate it. I only have one question for you, so everything works if I use spyder to run the test codes. However, I try to use VS 2013 as my IDE, and I can import opencv correctly, however, it failed the test of ffmpeg. So do you have any suggestions of it? Thanks.
Hey Junjie – I only tested this with Anaconda Spyder IDE. (I hadn’t tried this with Visual Studio). Anyone can hep out Junjie here? :)
Hi Johnny! I’m guess that VS doesn’t recognize the path variable: %OPENCV_DIR%. So it might solve the problem by just adding the variable inside the VS’s setting. However, I’m not able to try it out myself since I have no idea what I should add… Anyway, if you have time, see if you can test it to prove my guess. Thanks.
Thank you. Nice job here.
Thanks @joseyukio – I hope it works for you! :)
Successfully done! To add a small note: If you are seeing “False” on first line when you are doing FFMPEG codec test but you have done everything correctly and you supposed to get a “True”::: Check the sample video name, and try “input_video” instead of “input_video.mp4”. Sometimes the extension mp4 is not needed to add as your system can detect the app type automatically so when you use “input_video.mp4” it turns into “input_video.mp4.mp4”. This is the issue I was facing and solved.
Excellent I’m glad you’ve got it working! And thank you so much for sharing your experience and tips – this will definitely help out lots. Thank you again!!!
I followed your instructions but, i cannot import opencv. I get this error everytime: DLL load failed with error code -1073741795
Thankyou for the solution it saved me and my team a lot of time.
Great stuff Rakesh! I’m glad the instruction has helped!
hello world.
Thanks a lot – this worked really smoothly! The only issue I had was that I had been testing it in a console window that I had opened before adding the OPENCV_DIR and adding to PATH. Whilst the cv2 import worked fine, the cap.isOpened() always returned false. What fixed it was simply opening a new console window which then knew the updated PATH variable.
Glad to hear Mario! And thanks for sharing your experience / tips! Have fun with OpenCV! :-)
I have tried out your test.py, but I got the result of 2 falses. I don’t know why. And I have got a output_video.avi, but it is 0 byte! Can you explain it to me? Thank you in advance.
Make sure to save your .py file in the same directory as your test video – and of course make sure the name in the code matches the file name.
In the test code I needed to add parentheses around the print statements. Otherwise it worked great:
import cv2
cap = cv2.VideoCapture(“test.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()
Hi !!!
Thanks you for this tutorial.This was my problem (now fixed up): DON’T USE SPACE IN ROUTES (Ex: C:\My Folder\opencv). This isn’t a good idea, use this other route and you will get a TRUE: C:\MyFolder\opencv
:D Thanksssss
Excellent! thanks for sharing :-)
The code you posted prints out true,true but the output_video.avi is an empty 6kb file.
However adding this code
while True:
ret, frame = cap.read()
out.write(frame)
cv2.imshow(‘frame’,frame)
if cv2.waitKey(1) & 0xFF ==ord(‘q’):
break
after the line
print out.isOpened() # True = write out video successfully. False – fail to write out video.
writes out the video to the file.
I have the same problem and also received an error “File “”, line 14, in
cv2.imshow(‘frame’,frame)” where line 14 is “out = cv2.VideoWriter(“output_video.avi”, fourcc, 20.0, (640, 360))”
But I received the error only after adding the code mentioned in the above comment.
I also had this problem! Please what’s the solution if it’s writing an empty 6kb file?
btw if a comment exceeds 12 lines, the post button scrolls off the bottom of the page
Hello All,
I follow the tutorial and were very good success, but one thing is missing and need some help. When i make an new .py file and make import cv2 and execute says no module cv2 found. If i go to python console and do the same works. What i’am missing ???
Works like a charm. Thanks for writing it out in such detail. The exact steps are mentioned which makes the installation so much easier.
Excellent thanks! :-)
Thank you so much for this! I had been struggling for two whole days trying figure out how exactly to do this when I stumbled upon this fantastic post. You saved me an extra day. XD
Worked like a charm for me
conda install -c menpo opencv3
Thank you so much. Works very good.
Thank you very much! works like a charm
hi My computer is Win10, Anaconda3 and I got an error:
import cv2
Traceback (most recent call last):
File “”, line 1, in
ImportError: DLL load failed
Hi! I have a problem with this, and the answer to the Test.py script is always FALSE. I can’t open a video file.
However, if I try to open a webCam device, the frames start running but the image is always black.
How can I solve this?
Using Anaconda
I have installed last version of OpenCV in my computer
Environmental variables set
cv2.pyd copied to Anaconda/…/site-packages
I would appreciate if you could help me, thanks in advance!
I have tried out your test.py, but I got the result of 2 falses. I don’t know why. And I have got a output_video.avi, but it is 0 byte. I have no spaves in file path. tried the adding the brackets on the print aswell but no luck. is a possible cause being on windows 7?
Hey Lemuel! Try the alternative test script that I’ve added in the Stackoverflow thread
Also, check your current working directory. e.g. if your current working directory is set to (say) /project1, save both the test script and the (input and output) video files in this directory. The aim is to ensure the path resolves correctly.
Thank you so much for writing the steps in such a detailed way. Nowhere else could I find it being written so well.
Thanks! :)
Hola, gracias por su blog. Excelente aporte para los que iniciamos en este paso de la programación. Casualidad o no también estoy iniciando una practica en la facultad sobre Deep Learning y estoy por recibirme de ingeniero aeronáutico.
Saludos desde Argentina.
In my case, the root of Anaconda refers to python3, meanwhile I created another env that refer to python2.7. I got message like “ImportError: Module use of python27.dll conflicts with this version of Python.” when trying “import cv2”, as your directions. Then I actived python2.7 in Anaconda and try again, but it said “ImportError: No module named cv2”. How to get around it? Thanks!
Forget it! I’ve solved the problem by set up a BRANDNEW python27 env without depending on Anaconda.
Hi Johnny,
I am running opencv on Tensorflow to deal with videos. The version change made me struggle and your tuition helped me out. Thanks a lot!
Hello Johnny , I could not find the path variable that routes to anaconda !??
From what I recall (it’s been a loooong time ago when I wrote this post!) when you install Anaconda to your laptop at the beginning, it either automatically create the PATH environmental variable for you (or ask if you wish to add it – say “yes” in that case). You need to have installed Anaconda before using this tutorial. https://www.anaconda.com/download/
Thank you for the instruction! I will now be checking if I can properly integrate it and use it in my research.
Thanks for the kind words. By the way I have just done a massive update on this post. Please check the revised instruction again! (the old version was more like a hack. This new version is more “official”, require less work, and more consistent). Good luck!
Unarguably the best instruction on the web.
Thank you for the encouraging words Shiva! I hope this post has helped you setting up OpenCV & FFMPEG on Anaconda – it took me 3 days doing trial-and-errors and I really hope that it would take the others much less time in getting things setup :)
Appending my humble thankyou to this comment.
I believe you mean the second and third lines in test.py to read:
The cv attribute and the
CV_FOURCC
macro are missing from opencv 3.0, replaced withVideoWriter::fourcc()
(in the C++ implementation, at least). Haven’t got that figured out yet either.Still haven’t got it to read a test video, but I’m only a couple of hours in :)
Hey Warren – thanks and do let us know how you get on! (at the time of testing the Python code it worked fine on my machine… things might have changed since then though I am not sure – might try out your suggestion later!) Thanks and good luck!!!
thanks for the instructions, saved me a few hours of mucking around, very clear and very appreciated
here are a couple of minor changes to your script to test FFMPEG,
note that your original script fails at a couple of points
(1) in lines 2 and 3 you need to define a handle/(more properly an instance) to cv2.VideoCapture() and test whether that handle points to an open object, (in your code you’re testing whether the library cv2 is an open object)
(2) as one of the other chaps pointed out FOUR_CC has been moved, and the above syntax appears to work
thanks again
Thank you Pete! I have replaced the code in the article with yours – I have not tested this myself though. Please let us know how your test go – I’m sure this will benefit everybody! (I’m surprised this article is now one of the hottest on this site – how exciting :)
Hi Johnny, thanks for your instructions here. It’s really helpful and I appreciate it. I only have one question for you, so everything works if I use spyder to run the test codes. However, I try to use VS 2013 as my IDE, and I can import opencv correctly, however, it failed the test of ffmpeg. So do you have any suggestions of it? Thanks.
Hey Junjie – I only tested this with Anaconda Spyder IDE. (I hadn’t tried this with Visual Studio). Anyone can hep out Junjie here? :)
Hi Johnny! I’m guess that VS doesn’t recognize the path variable: %OPENCV_DIR%. So it might solve the problem by just adding the variable inside the VS’s setting. However, I’m not able to try it out myself since I have no idea what I should add… Anyway, if you have time, see if you can test it to prove my guess. Thanks.
Thank you. Nice job here.
Thanks @joseyukio – I hope it works for you! :)
Successfully done! To add a small note: If you are seeing “False” on first line when you are doing FFMPEG codec test but you have done everything correctly and you supposed to get a “True”::: Check the sample video name, and try “input_video” instead of “input_video.mp4”. Sometimes the extension mp4 is not needed to add as your system can detect the app type automatically so when you use “input_video.mp4” it turns into “input_video.mp4.mp4”. This is the issue I was facing and solved.
Excellent I’m glad you’ve got it working! And thank you so much for sharing your experience and tips – this will definitely help out lots. Thank you again!!!
I followed your instructions but, i cannot import opencv. I get this error everytime: DLL load failed with error code -1073741795
Sorry to hear! I doubt I’d have time to test out – try asking via this StackOverflow forum link? http://stackoverflow.com/questions/23119413/how-to-install-python-opencv-through-conda/30281466#30281466
Same here, I get “ImportError: DLL load failed”. Followed all your steps… can you help me please?
Sorry to hear! I doubt I’d have time to test out – try asking via this StackOverflow forum link? http://stackoverflow.com/questions/23119413/how-to-install-python-opencv-through-conda/30281466#30281466
Thankyou for the solution it saved me and my team a lot of time.
Great stuff Rakesh! I’m glad the instruction has helped!
hello world.
Thanks a lot – this worked really smoothly! The only issue I had was that I had been testing it in a console window that I had opened before adding the OPENCV_DIR and adding to PATH. Whilst the cv2 import worked fine, the cap.isOpened() always returned false. What fixed it was simply opening a new console window which then knew the updated PATH variable.
Glad to hear Mario! And thanks for sharing your experience / tips! Have fun with OpenCV! :-)
I have tried out your test.py, but I got the result of 2 falses. I don’t know why. And I have got a output_video.avi, but it is 0 byte! Can you explain it to me? Thank you in advance.
Make sure to save your .py file in the same directory as your test video – and of course make sure the name in the code matches the file name.
In the test code I needed to add parentheses around the print statements. Otherwise it worked great:
import cv2
cap = cv2.VideoCapture(“test.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()
Hi !!!
Thanks you for this tutorial.This was my problem (now fixed up): DON’T USE SPACE IN ROUTES (Ex: C:\My Folder\opencv). This isn’t a good idea, use this other route and you will get a TRUE: C:\MyFolder\opencv
:D Thanksssss
Excellent! thanks for sharing :-)
The code you posted prints out true,true but the output_video.avi is an empty 6kb file.
However adding this code
while True:
ret, frame = cap.read()
out.write(frame)
cv2.imshow(‘frame’,frame)
if cv2.waitKey(1) & 0xFF ==ord(‘q’):
break
after the line
print out.isOpened() # True = write out video successfully. False – fail to write out video.
writes out the video to the file.
I have the same problem and also received an error “File “”, line 14, in
cv2.imshow(‘frame’,frame)” where line 14 is “out = cv2.VideoWriter(“output_video.avi”, fourcc, 20.0, (640, 360))”
But I received the error only after adding the code mentioned in the above comment.
I also had this problem! Please what’s the solution if it’s writing an empty 6kb file?
btw if a comment exceeds 12 lines, the post button scrolls off the bottom of the page
Hello All,
I follow the tutorial and were very good success, but one thing is missing and need some help. When i make an new .py file and make import cv2 and execute says no module cv2 found. If i go to python console and do the same works. What i’am missing ???
Thanks for great job
It worked!!! Thank you Mr. Johnny Chan
Great to hear! :-)
Works like a charm. Thanks for writing it out in such detail. The exact steps are mentioned which makes the installation so much easier.
Excellent thanks! :-)
Thank you so much for this! I had been struggling for two whole days trying figure out how exactly to do this when I stumbled upon this fantastic post. You saved me an extra day. XD
Worked like a charm for me
conda install -c menpo opencv3
Thank you so much. Works very good.
Thank you very much! works like a charm
hi My computer is Win10, Anaconda3 and I got an error:
Hi! I have a problem with this, and the answer to the Test.py script is always FALSE. I can’t open a video file.
However, if I try to open a webCam device, the frames start running but the image is always black.
How can I solve this?
Using Anaconda
I have installed last version of OpenCV in my computer
Environmental variables set
cv2.pyd copied to Anaconda/…/site-packages
I would appreciate if you could help me, thanks in advance!
I have tried out your test.py, but I got the result of 2 falses. I don’t know why. And I have got a output_video.avi, but it is 0 byte. I have no spaves in file path. tried the adding the brackets on the print aswell but no luck. is a possible cause being on windows 7?
Hey Lemuel! Try the alternative test script that I’ve added in the Stackoverflow thread
Also, check your current working directory. e.g. if your current working directory is set to (say)
/project1
, save both the test script and the (input and output) video files in this directory. The aim is to ensure the path resolves correctly.Thank you so much for writing the steps in such a detailed way. Nowhere else could I find it being written so well.
Thanks! :)
Hola, gracias por su blog. Excelente aporte para los que iniciamos en este paso de la programación. Casualidad o no también estoy iniciando una practica en la facultad sobre Deep Learning y estoy por recibirme de ingeniero aeronáutico.
Saludos desde Argentina.
In my case, the root of Anaconda refers to python3, meanwhile I created another env that refer to python2.7. I got message like “ImportError: Module use of python27.dll conflicts with this version of Python.” when trying “import cv2”, as your directions. Then I actived python2.7 in Anaconda and try again, but it said “ImportError: No module named cv2”. How to get around it? Thanks!
Forget it! I’ve solved the problem by set up a BRANDNEW python27 env without depending on Anaconda.
Hi Johnny,
I am running opencv on Tensorflow to deal with videos. The version change made me struggle and your tuition helped me out. Thanks a lot!
Hello Johnny , I could not find the path variable that routes to anaconda !??
From what I recall (it’s been a loooong time ago when I wrote this post!) when you install Anaconda to your laptop at the beginning, it either automatically create the PATH environmental variable for you (or ask if you wish to add it – say “yes” in that case). You need to have installed Anaconda before using this tutorial. https://www.anaconda.com/download/
where is the cv2.pyd come from?
it shows DLL load failed
Thank you!
What would change if I used python 3.7?