一、讀取寫入視頻文件 1 import cv2 2 3 # 創建一個視屏捕獲對象 4 videoCapture = cv2.VideoCapture('AVI.avi') 5 6 # 獲取視頻的屬性值,cv2.CAP_PROP_FPS獲取視頻幀率 7 fps = videoCapture.get(c ...
一、讀取寫入視頻文件
1 import cv2 2 3 # 創建一個視屏捕獲對象 4 videoCapture = cv2.VideoCapture('AVI.avi') 5 6 # 獲取視頻的屬性值,cv2.CAP_PROP_FPS獲取視頻幀率 7 fps = videoCapture.get(cv2.CAP_PROP_FPS) 8 9 # cv2.CAP_PROP_FRAME_WIDTH/HEIGHT 返回float類型 獲取視頻幀的寬高 10 size = int(videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH)), \ 11 int(videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)) 12 13 ''' 14 創建一個寫入對象,將幀寫入輸出的視頻 15 cv2.VideoWriter_fourcc()函數指定編碼器為 I420 16 fps 和 size 指定輸出的幀率和尺寸 17 ''' 18 videoWrite = cv2.VideoWriter('Out.avi', 19 cv2.VideoWriter_fourcc('I', '4', '2', '0'), 20 fps, size 21 ) 22 23 ''' 24 對捕獲到的視頻對象進行讀取幀,success表示是否成功讀取一幀,frame表示當前幀。 25 迴圈讀取寫入輸出視頻。 26 ''' 27 success, frame = videoCapture.read() 28 while success: 29 videoWrite.write(frame) 30 success, frame = videoCapture.read()
二、捕獲攝像頭幀
1 import cv2 2 3 cameraCapture = cv2.VideoCapture(0) 4 5 fps = 30 6 7 size = int(cameraCapture.get(cv2.CAP_PROP_FRAME_WIDTH)), \ 8 int(cameraCapture.get(cv2.CAP_PROP_FRAME_HEIGHT)) 9 10 videoWriter = cv2.VideoWriter( 11 'OutVideo_GRAB.avi', 12 cv2.VideoWriter_fourcc('I', '4', '2', '0'), 13 fps, 14 size 15 ) 16 17 success, frame = cameraCapture.read() 18 19 numFramesRemaining = 10 * fps 20 while success and numFramesRemaining > 0: 21 videoWriter.write(frame) 22 success, frame = cameraCapture.read() 23 numFramesRemaining -= 1
和視頻的讀取寫入沒有什麼差異,都是需要先創建一個VideoCapture Object來操作,下述是細微差別:
3 Line:VideoCapture(0),其中 0 代表設備,還可以1,2,3 分別代表不同的攝像頭(如果存在),也可以輸入網路攝像頭,直接替換成URL即可
5 Line:需要手動設置fps的值
19 Line:需要設定一個時間,numFramesRemaining代表持續捕獲300個幀,而每30個幀為一秒,所以將生成一個10秒鐘的視頻文件
三、視窗顯示圖片
1 import cv2 2 3 img = cv2.imread('CopyPic.png') 4 cv2.imshow('', img) 5 cv2.waitKey() 6 cv2.destroyAllWindows()
四、視窗顯示攝像頭
1 import cv2 2 3 # 檢測視窗是否被點擊 4 clicked = False 5 6 7 # 定義滑鼠事件處理函數 8 def onMouse(event: int, x, y, flags, param): 9 global clicked 10 if event == cv2.EVENT_LBUTTONUP: 11 clicked = True 12 13 14 # 創建一個視頻對象 15 cameraCapture = cv2.VideoCapture(0) 16 # 定義視窗命 17 cv2.namedWindow('Camera') 18 # 將滑鼠回調函數,將滑鼠事件處理函數和視窗關聯起來 19 cv2.setMouseCallback('Camera', onMouse) 20 21 print('Showing camera feed. Click window or press any key to stop.') 22 # 獲取當前時間的攝像頭幀 23 success, frame = cameraCapture.read() 24 25 # 迴圈獲取當前時間的攝像頭幀,當按下任意按鍵 or 點擊滑鼠時則停止顯示 26 while success and cv2.waitKey(1) == -1 and not clicked: 27 cv2.imshow('Camera', frame) 28 success, frame = cameraCapture.read() 29 30 # 關閉視窗 31 cv2.destroyAllWindows() 32 33 # 釋放攝像頭資源 34 cameraCapture.release()
針對多攝像頭,我們需要先探明攝像頭的設備號:
1 import cv2 2 3 for item in range(10): 4 # 創建一個object 5 camera = cv2.VideoCapture(item) 6 7 # 查詢此攝像頭是否能打開,如果不能則跳過,並輸出一條 Error Message 8 if not camera.isOpened(): 9 print(f"Can\'t open camera {item}") 10 continue 11 12 # 讀取攝像頭幀率 13 while True: 14 success, frame = camera.read() 15 # 當攝像頭幀讀取失敗則跳過 16 if not success: 17 break 18 19 cv2.imshow(f'Camera device number: {item}', frame) 20 21 # 等待1毫秒,檢查用戶是否有鍵盤輸入‘q’ 22 if cv2.waitKey(1) == ord('q'): 23 break 24 25 camera.release() 26 cv2.destroyAllWindows()