怎么在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捕获摄像头并显示内容问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。