赞
踩
一、打开摄像头
import cv2
import numpy as np
def video_demo():
capture = cv2.VideoCapture(0)#0为电脑内置摄像头
while(True):
ret, frame = capture.read()#摄像头读取,ret为是否成功打开摄像头,true,false。 frame为视频的每一帧图像
frame = cv2.flip(frame, 1)#摄像头是和人对立的,将图像左右调换回来正常显示。
cv2.imshow("video", frame)
c = cv2.waitKey(50)
if c == 27:
break
video_demo()
cv2.destroyAllWindows()
二、打开摄像头并截图
import cv2
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW) # 打开摄像头
while (1):
# get a frame
ret, frame = cap.read()
frame = cv2.flip(frame, 1) # 摄像头是和人对立的,将图像左右调换回来正常显示
# show a frame
cv2.imshow("capture", frame) # 生成摄像头窗口
if cv2.waitKey(1) & 0xFF == ord('q'): # 如果按下q 就截图保存并退出
cv2.imwrite("test.png", frame) # 保存
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。