怎么在python中使用opencv捕获摄像头并显示内容?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
创新互联建站从2013年成立,是专业互联网技术服务公司,拥有项目做网站、成都网站制作网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元山海关做网站,已为上家服务,为山海关各地企业和个人服务,联系电话:135182197921、捕获摄像头和实时显示
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、从摄像头内抓拍图片
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-----从本地摄像头和网络摄像头截取图片
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() # 获取网络摄像头,格式: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)
关于怎么在python中使用opencv捕获摄像头并显示内容问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。