git:https://github.com/linyi0604/Computer-Vision
1 # coding:utf8
2
3 import cv2 4
5
6 """
7 捕獲攝像頭10s的視頻信息 寫入一個avi文件 8 """
9
10 cameraCapture = cv2.VideoCapture(0) # 傳入0代表0號攝像頭
11 fps = 30
12 size = ( 13 int(cameraCapture.get(cv2.CAP_PROP_FRAME_WIDTH)), 14 int(cameraCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)) 15 ) 16
17 videoWriter = cv2.VideoWriter( 18 "outputVid.avi", 19 cv2.VideoWriter_fourcc("I", "4", "2", "0"), 20 fps, 21 size 22 ) 23
24 success, frame = cameraCapture.read() 25 numFramesRemaining = 10 * fps - 1
26 while success and numFramesRemaining: 27 videoWriter.write(frame) 28 success, frame = cameraCapture.read() 29 numFramesRemaining -= 1
30
31 cameraCapture.release() 32
33
34 """
35 如果使用一組攝像頭或多個攝像頭 36 用grab和retrieve方法代替 37
38 success0 = cameraCapture.grab() 39 success1 = cameraCapture.grab() 40 if success0 and success1: 41 frame0 = cameraCapture0.retrieve() 42 frame1 = cameraCapture1.retrieve() 43
44
45 """