Skip to content

Instantly share code, notes, and snippets.

@gyk
Last active August 29, 2015 14:07
Show Gist options
  • Save gyk/d30a3275c1fb6aaf18cf to your computer and use it in GitHub Desktop.
Save gyk/d30a3275c1fb6aaf18cf to your computer and use it in GitHub Desktop.

Install OpenCV Python Binding on Windows

  1. Download OpenCV pre-built binaried from SourceForge;

  2. Set the output path of self-extracting archive to D:\Bin, and all the files will be extracted to D:\Bin\opencv;

  3. Copy cv2.pyd from %OPENCV%\build\python\2.7 to %PYTHON%\Lib\site-packages. Link in the OpenCV official site. Run Python to check whether it's been installed:

    Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win
    32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import cv2
    >>> cv2.__version__
    '2.4.10'
  4. In order to use VideoCapture, copy ffmpeg dll from %OPENCV%\sources\3rdparty\ffmpeg\opencv_ffmpeg.dll (opencv_ffmpeg_64.dll if on win64) to some directory in PATH, and rename it to opencv_ffmpeg($version_number_without_dots).dll On my machine specifically, I copied D:\Bin\opencv\sources\3rdparty\ffmpeg\opencv_ffmpeg.dll to D:\Python27\Scripts\opencv_ffmpeg2410.dll. Check this SO question for details.

  5. Now it should be able to run some background subtraction demo (code modified from http://docs.opencv.org/trunk/doc/py_tutorials/py_video/py_bg_subtraction/py_bg_subtraction.html):

    import numpy as np
    import cv2
    
    cap = cv2.VideoCapture(r'SampleVideo.avi')
    fgbg = cv2.BackgroundSubtractorMOG2()
    
    while(1):
        ret, frame = cap.read()
        if not ret:
            print 'EOF'
            break
    
        fgmask = fgbg.apply(frame)
    
        cv2.imshow('frame', fgmask)
        k = cv2.waitKey(30) & 0xff
        if k == 27:
            break
    
    cap.release()
    cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment