60 thoughts on “OpenCV-Python – How to install OpenCV-Python package to Anaconda (Windows)”

  1. Thank you for the instruction! I will now be checking if I can properly integrate it and use it in my research.

    1. 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!

    1. 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 :)

  2. 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 :)

    1. 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!!!

  3. 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

    1. 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 :)

  4. 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.

    1. Hey Junjie – I only tested this with Anaconda Spyder IDE. (I hadn’t tried this with Visual Studio). Anyone can hep out Junjie here? :)

      1. 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.

  5. 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.

    1. 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!!!

  6. I followed your instructions but, i cannot import opencv. I get this error everytime: DLL load failed with error code -1073741795

  7. Same here, I get “ImportError: DLL load failed”. Followed all your steps… can you help me please?

  8. 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.

  9. 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.

    1. 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.

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

  11. 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

  12. 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.

    1. 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.

    2. I also had this problem! Please what’s the solution if it’s writing an empty 6kb file?

  13. 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

  14. It worked!!! Thank you Mr. Johnny Chan

    import cv2
    cap = cv2.VideoCapture(“ball_tracking.mp4”)
    warning: Error opening file (/build/opencv/modules/videoio/src/cap_ffmpeg_impl.hpp:578)
    cap = cv2.VideoCapture(“ball_tracking_example.mp4”)
    print cap.isOpened()
    True
    fourcc = cv2.VideoWriter_fourcc(*’XVID’)
    out = cv2.VideoWriter(“output_video.avi”, fourcc, 20.0, (640, 360))
    print out.isOpened()
    True
    cap.release()
    out.release()

  15. Works like a charm. Thanks for writing it out in such detail. The exact steps are mentioned which makes the installation so much easier.

  16. 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

  17. 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

  18. 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!

  19. 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?

      1. 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.

  20. 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.

  21. 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!

    1. Forget it! I’ve solved the problem by set up a BRANDNEW python27 env without depending on Anaconda.

  22. 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!

  23. 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/

Comments are closed.