OpenCV获取Usb相机视频流

一、设备:使用相机 BMT相影720p
二、一般usb摄像头在linux下的节点都为video,此处在我的pc上摄像头设备节点为/dev/video1,
所VideoCapture传入参数为201
三、关于属性可以查找以下链接:
https://docs.opencv.org/3.2.0/d4/d15/group__videoio__flags
__base.html#gaeb8dd9c89c10a5c63c139bf7c4f5704d

#!/usr/bin/env python
# *-* coding:utf-8 *-*

import cv2

def main():
	capture = cv2.VideoCapture(201)

	#获得码率及尺寸  
	fps = capture.get(cv2.CAP_PROP_FPS)  
	size = (int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)),   
	        int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))) 
	fourcc = cv2.VideoWriter_fourcc(*'MJPG')
	#TypeError: 'int' object is not callable
    #fourcc = cv2.CAP_PROP_FOURCC('M', 'J', 'P', 'G')
  
	#指定写视频的格式, I420-avi, MJPG-mp4  
	#videoWriter = cv2.VideoWriter('oto_other.mp4', fourcc, 20.0, (640,480))
	videoWriter = cv2.VideoWriter('oto_other1.mp4', fourcc, fps, size)

	#判断是否正常开启
	print capture.isOpened()
	frameNum = 1
	while(capture.isOpened()):
		_, frame = capture.read()
		print frameNum
		frameNum += 1
		#frame = cv2.resize(frame, (960, 540))
		cv2.imshow('frame', frame)
                videoWriter.write(frame) #写视频帧

		'''
		#每10帧存储一张图片
		if frameNum%10 == 1:
			cv2.imwrite('image'+str(frameNum)+'.jpg', frame)
		'''
		if cv2.waitKey(1) == ord('q'):
			break

	capture.release()
	cv2.destroyAllWindows()

if __name__ == '__main__':
	main()

关于fourcc函数:

fourcc()
static int cv::VideoWriter::fourcc	(	char 	c1,
char 	c2,
char 	c3,
char 	c4 
)		

Concatenates 4 chars to a fourcc code.
Returns a fourcc code
This static method constructs the fourcc code of the codec to be used in
the constructor VideoWriter::VideoWriter or VideoWriter::open

你可能感兴趣的:(视音频与图像处理)