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

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

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

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

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

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

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

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

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

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

Leave a reply