Install OpenCV&Python on Windows
tag: opencv python
- [x] win7,win10下調(diào)用camera,保存mp4,均測(cè)試。
- [x] 涉及軟件都已經(jīng)放在百度云
1. 安裝python
python2.7下載地址,點(diǎn)擊左邊或直接在evernote里下載附件。
為什么是python2.7? 安裝版本為python2.7.11,因?yàn)閛penCV對(duì)python3的支持還是beta階段 ,而且你在opencv的build路徑中只能找到opencv/build/python/2.7
把python.exe的路徑,設(shè)置在path環(huán)境變量中(一般為C:/python27)
把C:\Python27\Scripts 也設(shè)置為環(huán)境變量Path(為了用pip 安裝)
2. 安裝numpy
- opencv/build/python/2.7里的cv2.pyd,是用numpy1.9.Xbuild的。 所以對(duì)numpy版本有要求,必須是numpy1.9. numpy1.9下載地址
3. 安裝openCV
4.安裝騰訊云微視頻SDK
pip install qcloud_video
5.安裝pyOSC
- pyOSC
- 解壓縮文件夾,并在該文件夾目錄下
python setup.py install
6. python2.7和openCV結(jié)合
- 把opencv/build/python/2.7文件夾下 cv2.pyd 拷貝到 C:/Python27/lib/site-packages.
-
此時(shí)在cmd 下,輸入python ;然后輸入 import cv2 ,就會(huì)有類似下圖的
975A96E5-D357-4F82-86B7-40AB65C77083.png-48.1kB - 把C:\opencv\build\bin里的opencv_ffmpeg310.dll 拷貝到 C:\Python27中
- 安裝 imutils
- 步驟:
目錄C:\Python27\Scripts\ 下執(zhí)行升級(jí)pip命令:
python -m pip install —upgrade pip
- 還在上面的目錄下 執(zhí)行
pip install imutils
安裝 openh264下載地址 下載1.4 win32版本openh264-1.4.0-win32msvc.dll,下載后把dll 放進(jìn) C:\Python27
安裝 requests :pip install requests
安裝Pillow :pip install Pillow
安裝qrcode :qrcode source,解壓目錄下,執(zhí)行python setup.py install
安裝ffmpeg :ffmpeg,解壓縮到C盤根目錄,并把C:\ffmpeg\bin加入到Path,在cmd下輸入ffmpeg,會(huì)有一些相關(guān)提示。
-
合并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
-
調(diào)整分辨率 :
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()