#pip3 install opencv-python
import cv2
from datetime import datetime
FILENAME = 'myvideo.avi'
WIDTH = 1280
HEIGHT = 720
FPS = 24.0
# 必须指定CAP_DSHOW(Direct Show)参数初始化摄像头,否则无法使用更高分辨率
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
# 设置摄像头设备分辨率
cap.set(cv2.CAP_PROP_FRAME_WIDTH, WIDTH)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, HEIGHT)
# 设置摄像头设备帧率,如不指定,默认600
cap.set(cv2.CAP_PROP_FPS, 24)
# 建议使用XVID编码,图像质量和文件大小比较都兼顾的方案
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter(FILENAME, fourcc, FPS, (WIDTH, HEIGHT))
start_time = datetime.now()
while True:
ret, frame = cap.read()
if ret:
out.write(frame)
# 显示预览窗口
cv2.imshow('Preview_Window', frame)
# 录制5秒后停止
if (datetime.now()-start_time).seconds == 5:
cap.release()
break
# 监测到ESC按键也停止
if cv2.waitKey(3) 0xff == 27:
cap.release()
break
out.release()
cv2.destroyAllWindows()
# 1. 打开摄像头
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()
#2. 打开摄像头并截图
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) # 保存路径
break
cap.release()
cv2.destroyAllWindows()
#3. 打开摄像头并定时截图
def video_demo():
print('开始')
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW) # 电脑自身摄像头
i = 0#定时装置初始值
photoname = 1#文件名序号初始值
while True:
i = i + 1
reg, frame = cap.read()
frame = cv2.flip(frame, 1) # 图片左右调换
cv2.imshow('window', frame)
if i == 50: # 定时装置,定时截屏,可以修改。
filename = str(photoname) + '.png' # filename为图像名字,将photoname作为编号命名保存的截图
cv2.imwrite('C:/Users/Administrator/Desktop/m' + '\\' + filename, frame) # 截图 前面为放在桌面的路径 frame为此时的图像
print(filename + '保存成功') # 打印保存成功
i = 0 # 清零
photoname = photoname + 1
if photoname >= 20: # 最多截图20张 然后退出(如果调用photoname = 1 不用break为不断覆盖图片)
# photoname = 1
break
if cv2.waitKey(1) 0xff == ord('q'):
break
# 释放资源
cap.release()
video_demo()
cv2.destroyAllWindows()
#-*- coding: utf-8 -*-
# import 进openCV的库
import cv2
###调用电脑摄像头检测人脸并截图
def CatchPICFromVideo(window_name, camera_idx, catch_pic_num, path_name):
cv2.namedWindow(window_name)
#视频来源,可以来自一段已存好的视频,也可以直接来自USB摄像头
cap = cv2.VideoCapture(camera_idx)
#告诉OpenCV使用人脸识别分类器
classfier = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")
#识别出人脸后要画的边框的颜色,RGB格式, color是一个不可增删的数组
color = (0, 255, 0)
num = 0
while cap.isOpened():
ok, frame = cap.read() #读取一帧数据
if not ok:
break
grey = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #将当前桢图像转换成灰度图像
#人脸检测,1.2和2分别为图片缩放比例和需要检测的有效点数
faceRects = classfier.detectMultiScale(grey, scaleFactor = 1.2, minNeighbors = 3, minSize = (32, 32))
if len(faceRects) > 0: #大于0则检测到人脸
for faceRect in faceRects: #单独框出每一张人脸
x, y, w, h = faceRect
#将当前帧保存为图片
img_name = "%s/%d.jpg" % (path_name, num)
#print(img_name)
image = frame[y - 10: y + h + 10, x - 10: x + w + 10]
cv2.imwrite(img_name, image,[int(cv2.IMWRITE_PNG_COMPRESSION), 9])
num += 1
if num > (catch_pic_num): #如果超过指定最大保存数量退出循环
break
#画出矩形框
cv2.rectangle(frame, (x - 10, y - 10), (x + w + 10, y + h + 10), color, 2)
#显示当前捕捉到了多少人脸图片了,这样站在那里被拍摄时心里有个数,不用两眼一抹黑傻等着
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame,'num:%d/100' % (num),(x + 30, y + 30), font, 1, (255,0,255),4)
#超过指定最大保存数量结束程序
if num > (catch_pic_num): break
#显示图像
cv2.imshow(window_name, frame)
c = cv2.waitKey(10)
if c 0xFF == ord('q'):
break
#释放摄像头并销毁所有窗口
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
# 连续截100张图像,存进image文件夹中
CatchPICFromVideo("get face", 0, 99, "/image")
到此这篇关于python通过opencv调用摄像头操作实例分析的文章就介绍到这了,更多相关python使用opencv调用摄像头操作内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!