1. 程式人生 > 其它 >樹莓派+opencv實時讀取攝像頭

樹莓派+opencv實時讀取攝像頭

技術標籤:筆記opencvpython

利用python的opencv模組讀取攝像頭來獲取影象,同時呼叫datetime模組來讀取實時的時間並顯示在螢幕上

import cv2
import datetime

#獲取攝像頭視訊
cap = cv2.VideoCapture(0)
# 獲取視訊寬度
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
# 獲取視訊高度
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
#文字座標
word_x = int(frame_width / 10)
word_y = int(frame_height / 10)

while (cap.isOpened()):
    ret,frame = cap.read()
    time_text = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    cv2.putText(frame, time_text, (word_x,word_y),
                cv2.FONT_HERSHEY_SIMPLEX,1,(55,255,155),2)
    cv2.imshow("real_time",frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):    #等待按鍵q按下
        break

cap.release()
cv2.destroyAllWindows()