一、讀取寫(xiě)入視頻文件
1 import cv2 2 3 # 創(chuàng)建一個(gè)視屏捕獲對(duì)象 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 創(chuàng)建一個(gè)寫(xiě)入對(duì)象,將幀寫(xiě)入輸出的視頻 15 cv2.VideoWriter_fourcc()函數(shù)指定編碼器為 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 對(duì)捕獲到的視頻對(duì)象進(jìn)行讀取幀,success表示是否成功讀取一幀,frame表示當(dāng)前幀。 25 循環(huán)讀取寫(xiě)入輸出視頻。 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
和視頻的讀取寫(xiě)入沒(méi)有什么差異,都是需要先創(chuàng)建一個(gè)VideoCapture Object來(lái)操作,下述是細(xì)微差別:
3? ?Line:VideoCapture(0),其中 0 代表設(shè)備,還可以1,2,3 分別代表不同的攝像頭(如果存在),也可以輸入網(wǎng)絡(luò)攝像頭,直接替換成URL即可
5? ?Line:需要手動(dòng)設(shè)置fps的值
19 Line:需要設(shè)定一個(gè)時(shí)間,numFramesRemaining代表持續(xù)捕獲300個(gè)幀,而每30個(gè)幀為一秒,所以將生成一個(gè)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 # 檢測(cè)窗口是否被點(diǎn)擊 4 clicked = False 5 6 7 # 定義鼠標(biāo)事件處理函數(shù) 8 def onMouse(event: int, x, y, flags, param): 9 global clicked 10 if event == cv2.EVENT_LBUTTONUP: 11 clicked = True 12 13 14 # 創(chuàng)建一個(gè)視頻對(duì)象 15 cameraCapture = cv2.VideoCapture(0) 16 # 定義窗口命 17 cv2.namedWindow('Camera') 18 # 將鼠標(biāo)回調(diào)函數(shù),將鼠標(biāo)事件處理函數(shù)和窗口關(guān)聯(lián)起來(lái) 19 cv2.setMouseCallback('Camera', onMouse) 20 21 print('Showing camera feed. Click window or press any key to stop.') 22 # 獲取當(dāng)前時(shí)間的攝像頭幀 23 success, frame = cameraCapture.read() 24 25 # 循環(huán)獲取當(dāng)前時(shí)間的攝像頭幀,當(dāng)按下任意按鍵 or 點(diǎn)擊鼠標(biāo)時(shí)則停止顯示 26 while success and cv2.waitKey(1) == -1 and not clicked: 27 cv2.imshow('Camera', frame) 28 success, frame = cameraCapture.read() 29 30 # 關(guān)閉窗口 31 cv2.destroyAllWindows() 32 33 # 釋放攝像頭資源 34 cameraCapture.release()
?
針對(duì)多攝像頭,我們需要先探明攝像頭的設(shè)備號(hào):文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-747004.html
1 import cv2 2 3 for item in range(10): 4 # 創(chuàng)建一個(gè)object 5 camera = cv2.VideoCapture(item) 6 7 # 查詢此攝像頭是否能打開(kāi),如果不能則跳過(guò),并輸出一條 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 # 當(dāng)攝像頭幀讀取失敗則跳過(guò) 16 if not success: 17 break 18 19 cv2.imshow(f'Camera device number: {item}', frame) 20 21 # 等待1毫秒,檢查用戶是否有鍵盤(pán)輸入‘q’ 22 if cv2.waitKey(1) == ord('q'): 23 break 24 25 camera.release() 26 cv2.destroyAllWindows()
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-747004.html
到了這里,關(guān)于【Python】【OpenCV】視頻幀和攝像頭幀操作 and 窗口顯示的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!