python opencv捕獲攝像頭并顯示內(nèi)容的實現(xiàn)
更新時間:2019年07月11日 10:29:42 作者:小呆丶
這篇文章主要介紹了python opencv捕獲攝像頭并顯示內(nèi)容的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
1、捕獲攝像頭和實時顯示
import cv2 import numpy as np import pickle import matplotlib.pyplot as plt cap = cv2.VideoCapture(0) while True: ret,frame = cap.read() # Our operations on the frame come here gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Display the resulting frame cv2.imshow('frame',gray) if cv2.waitKey(1) & 0xFF == ord('q'): break # When everything done, release the capture cap.release() cv2.destroyAllWindows()
2、從攝像頭內(nèi)抓拍圖片
import cv2 import numpy as np import pickle import matplotlib.pyplot as plt cap = cv2.VideoCapture(0) index = 0 while True: ret,frame = cap.read() # Our operations on the frame come here gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Display the resulting frame cv2.imshow('frame',gray) if cv2.waitKey(1) & 0xFF == ord('p'): cv2.imwrite("kk.jpg",frame) index = index + 1 if cv2.waitKey(1) & 0xFF == ord('q'): break # When everything done, release the capture cap.release() cv2.destroyAllWindows()
補充:python-----從本地攝像頭和網(wǎng)絡攝像頭截取圖片
import cv2 # 獲取本地攝像頭 # folder_path 截取圖片的存儲目錄 def get_img_from_camera_local(folder_path): cap = cv2.VideoCapture(0) i = 1 while True: ret, frame = cap.read() cv2.imshow("capture", frame) print str(i) cv2.imwrite(folder_path + str(i) + '.jpg', frame) # 存儲為圖像 if cv2.waitKey(1) & 0xFF == ord('q'): break i += 1 cap.release() cv2.destroyAllWindows() # 獲取網(wǎng)絡攝像頭,格式:rtsp://username:pwd@ip/ # folder_path 截取圖片的存儲目錄 def get_img_from_camera_net(folder_path): cap = cv2.VideoCapture('rtsp://username:pwd@ip/') i = 1 while True: ret, frame = cap.read() cv2.imshow("capture", frame) print str(i) cv2.imwrite(folder_path + str(i) + '.jpg', frame) # 存儲為圖像 if cv2.waitKey(1) & 0xFF == ord('q'): break i += 1 cap.release() cv2.destroyAllWindows() # 測試 if __name__ == '__main__': folder_path = 'D:\\img_from_camera\\' get_img_from_camera_local(folder_path)
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Django Web開發(fā)中django-debug-toolbar的配置以及使用
正在發(fā)愁怎么調(diào)試Django,就遇到了Django Debug Toolbar這個利器。下面這篇文章主要給大家介紹了關于django web開發(fā)中django-debug-toolbar的配置以及使用的相關資料,文中通過圖文及示例代碼介紹的非常詳細,需要的朋友可以參考下2018-05-05基于Python對數(shù)據(jù)shape的常見操作詳解
今天小編就為大家分享一篇基于Python對數(shù)據(jù)shape的常見操作詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-12-12Python Multinomial Naive Bayes多項貝葉斯模型實現(xiàn)原理介紹
這篇文章主要介紹了Python Multinomial Naive Bayes多項貝葉斯模型實現(xiàn)原理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2022-09-09