1 import numpy as np 2 import cv2 3 # 讀取視訊檔案 4 cap = cv2.VideoCapture(`./law.mp4`) 5 # 或者電影每秒的幀數 6 fps = cap.get(cv2.CAP_PROP_FPS) 7 # 判斷視訊是否一直開啟 8 while (cap.isOpened()): 9 success,frame = cap.read() 10 # 視訊顯示 11 cv2.imshow(`law`, frame) 12 # 設定視窗 13 cv2.resizeWindow(`law`, 512,288) 14 # 判斷退出條件 15 if cv2.waitKey(int(1000//fps)) ==ord(`q`): 16 break 17 # 清除快取退出 18 cv2.destroyAllWindows()
# 獲取攝像頭 : 只需要將讀取視訊的檔案裡的值換成 0
1 # 值為0表示調取攝像頭 2 cap = cv2.VideoCapture(0) 3 4 while (cap.isOpened()): 5 sucess,frame = cap.read() 6 cv2.imshow(`capture`, frame) 7 if cv2.waitKey(41) == ord(`q`): 8 break 9 # 關閉攝像頭 10 cap.release() 11 cv2.destroyAllWindows()