python opencv捕獲攝像頭并顯示內(nèi)容的實(shí)現(xiàn)
1、捕獲攝像頭和實(shí)時(shí)顯示
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()
補(bǔ)充:python-----從本地?cái)z像頭和網(wǎng)絡(luò)攝像頭截取圖片
import cv2 # 獲取本地?cái)z像頭 # folder_path 截取圖片的存儲(chǔ)目錄 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) # 存儲(chǔ)為圖像 if cv2.waitKey(1) & 0xFF == ord('q'): break i += 1 cap.release() cv2.destroyAllWindows() # 獲取網(wǎng)絡(luò)攝像頭,格式:rtsp://username:pwd@ip/ # folder_path 截取圖片的存儲(chǔ)目錄 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) # 存儲(chǔ)為圖像 if cv2.waitKey(1) & 0xFF == ord('q'): break i += 1 cap.release() cv2.destroyAllWindows() # 測(cè)試 if __name__ == '__main__': folder_path = 'D:\\img_from_camera\\' get_img_from_camera_local(folder_path)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Django Web開(kāi)發(fā)中django-debug-toolbar的配置以及使用
正在發(fā)愁怎么調(diào)試Django,就遇到了Django Debug Toolbar這個(gè)利器。下面這篇文章主要給大家介紹了關(guān)于django web開(kāi)發(fā)中django-debug-toolbar的配置以及使用的相關(guān)資料,文中通過(guò)圖文及示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2018-05-05基于Python對(duì)數(shù)據(jù)shape的常見(jiàn)操作詳解
今天小編就為大家分享一篇基于Python對(duì)數(shù)據(jù)shape的常見(jiàn)操作詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12python網(wǎng)絡(luò)爬蟲(chóng)實(shí)戰(zhàn)
實(shí)踐來(lái)源于理論,做爬蟲(chóng)前肯定要先了解相關(guān)的規(guī)則和原理,網(wǎng)絡(luò)爬蟲(chóng)又稱(chēng)為網(wǎng)頁(yè)蜘蛛,網(wǎng)絡(luò)機(jī)器人,更經(jīng)常的稱(chēng)為網(wǎng)頁(yè)追逐者,是一種按照一定的規(guī)則,自動(dòng)地抓取萬(wàn)維網(wǎng)信息的程序或者腳本。一句話概括就是網(wǎng)上信息搬運(yùn)工。本篇文章帶你深入了解,需要的朋友可以參考下2021-09-09python多線程http下載實(shí)現(xiàn)示例
python多線程http下載實(shí)現(xiàn)示例,大家參考使用吧2013-12-12Python Multinomial Naive Bayes多項(xiàng)貝葉斯模型實(shí)現(xiàn)原理介紹
這篇文章主要介紹了Python Multinomial Naive Bayes多項(xiàng)貝葉斯模型實(shí)現(xiàn)原理,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-09-09