Windows下openCV与Python使用

Install OpenCV&Python on Windows

tag: opencv python


  • [x] win7,win10下调用camera,保存mp4,均测试。
  • [x] 涉及软件都已经放在百度云

1. 安装python

  • python2.7下载地址,点击左边或直接在evernote里下载附件。

  • 为什么是python2.7? 安装版本为python2.7.11,因为openCV对python3的支持还是beta阶段 ,而且你在opencv的build路径中只能找到opencv/build/python/2.7

  • 把python.exe的路径,设置在path环境变量中(一般为C:/python27)

  • 把C:\Python27\Scripts 也设置为环境变量Path(为了用pip 安装)

2. 安装numpy

  • opencv/build/python/2.7里的cv2.pyd,是用numpy1.9.Xbuild的。 所以对numpy版本有要求,必须是numpy1.9. numpy1.9下载地址

3. 安装openCV

  • opencv下载地址

4.安装腾讯云微视频SDK

pip install qcloud_video

5.安装pyOSC

  • pyOSC
  • 解压缩文件夹,并在该文件夹目录下

python setup.py install

6. python2.7和openCV结合

  • 把opencv/build/python/2.7文件夹下 cv2.pyd 拷贝到 C:/Python27/lib/site-packages.
  • 此时在cmd 下,输入python ;然后输入 import cv2 ,就会有类似下图的


    Windows下openCV与Python使用_第1张图片
    975A96E5-D357-4F82-86B7-40AB65C77083.png-48.1kB
  • 把C:\opencv\build\bin里的opencv_ffmpeg310.dll 拷贝到 C:\Python27中
  • 安装 imutils
  • 步骤:
    目录C:\Python27\Scripts\ 下执行升级pip命令:

python -m pip install —upgrade pip

  • 还在上面的目录下 执行

pip install imutils

  • 安装 openh264下载地址 下载1.4 win32版本openh264-1.4.0-win32msvc.dll,下载后把dll 放进 C:\Python27

  • 安装 requests :pip install requests

  • 安装Pillow :pip install Pillow

  • 安装qrcode :qrcode source,解压目录下,执行python setup.py install

  • 安装ffmpeg :ffmpeg,解压缩到C盘根目录,并把C:\ffmpeg\bin加入到Path,在cmd下输入ffmpeg,会有一些相关提示。

  • 合并mp4:

    ffmpeg -i 1.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb 1.ts

    ffmpeg -i 2.mp4 -vcodec copy -acodec copy -vbsf h264_mp4toannexb 2.ts

    ffmpeg -i "concat:1.ts|2.ts" -acodec copy -vcodec copy -absf aac_adtstoasc output.mp4

  • 调整分辨率 :

    ffmpeg -i test.mp4 -vf scale=640:-2 test_640.mp4

  • 参考:

processing a source MP4 into a proper MP4 derivatives for Web access:

HandBrakeCLI -i "getting_a_book_orig.mp4" -o "getting_a_book.mp4" -e x264 -q 20 -a 1 -E faac -B 128 -6 dpl2 -R Auto -D 0.0 -f mp4 -X 640 -m -x cabac=0:ref=2:me=umh:bframes=0:weightp=0:subme=6:8x8dct=0:trellis=0 --vb 600 --two-pass --turbo --optimize

here is a command for converting to WebM from the derivative MP4:
MP4 to WebM

ffmpeg -quality good -qmin 10 -qmax 51 -i "getting_a_book.mp4" "getting_a_book.webm"


Sample Code:

import numpy as np
import cv2
import time
import imutils
filename = time.strftime("%m-%d-%H-%M-%S") + '.mp4'

isValid = True
w=800
h=600
videoCapture = cv2.VideoCapture(3)


#fps = videoCapture.get(cv2.cv.CV_CAP_PROP_FPS)
fps = 0;
if(fps ==0):
     fps = 25
size = (int(videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH)),
        int(videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)))

print(fps)
print(size)
fourcc = cv2.VideoWriter_fourcc(*'X264')
writer = cv2.VideoWriter(filename, fourcc, 20.0,size)
if not videoCapture.isOpened()  :
    print("can't open the camera")
#s_img = cv2.imread("test.png", -1)
#NamedWindow( "test", CV_WINDOW_NORMAL );
cv2.namedWindow("test",cv2.WINDOW_FULLSCREEN)
cv2.setWindowProperty("test", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)

while(True):
    # Capture frame-by-frame
    try :
        retLeft, frameLeft = videoCapture.read()

    except:
        print("error the take a image")
        isValid = False


    if isValid == True:
        #try:
            # Our operations on the frame come here
            #grayLeft = cv2.cvtColor(frameLeft, cv2.COLOR_BGR2GRAY)
            #hsv = cv2.cvtColor(frameLeft, cv2.COLOR_BGR2HSV)
            #grayLeft = cv2.cvtColor(frameLeft, cv2.COLOR_BGR)
            # Display the resulting frame

            # fullScreen set

            #videoWriter.write(frameLeft)
        cv2.imshow('test',frameLeft)
        #    cv2.imshow('Display image',s_img)          ## Show image in the window
        writer.write(frameLeft)

            #cv2.imshow('frameLeft',grayLeft)

        if cv2.waitKey(40) & 0xFF == ord('q'):
                break
        #except:
            #print("Error during the convertion")


# When everything done, release the capture
videoCapture.release()
writer.release()
#out.release()
cv2.destroyAllWindows()

你可能感兴趣的:(Windows下openCV与Python使用)