怎么在python中使用opencv捕获摄像头并显示内容?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
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、从摄像头内抓拍图片
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捕获摄像头并显示内容问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。
网页题目:怎么在python中使用opencv捕获摄像头并显示内容-创新互联
文章转载:https://www.cdcxhl.com/article8/cdesop.html
成都网站建设公司_创新互联,为您提供搜索引擎优化、用户体验、网站导航、商城网站、移动网站建设、网页设计公司
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联