opencv 例子,保存video文件。在windows下,据说只支持divx压缩。没有试过是否确实。
直接安装numpy有问题的,可以下载whl文件numpy-1.9.2+mkl-cp27-none-win_amd64.whl, 这个文件是非官方的,但是使用起来似乎没有什么问题。很多帖子谈到安装问题时,都会提到这个文件。
然后在本地安装whl文件,pip install whl-file-name
例子是从国外网上教程拷贝来的,做了少许修改。经过测试,可以运行。
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
#fourcc = cv2.VideoWriter_fourcc("D", "I", "V", "X")
fourcc = cv2.VideoWriter_fourcc(*"DIVX")
writer = cv2.VideoWriter("out.avi", fourcc, 15.0, (w, h))
while(True):
# Capture frame-by-frame
ret, frame = cap.read()
if ret == True:
writer.write(frame)
cv2.imshow('webcam', frame)
key = cv2.waitKey(1) & 0xFF
if key == ord('q') or key == ord('Q'):
break
# When everything done, release the capture
cap.release()
writer.release()
cv2.destroyAllWindows()