#pip3 install opencv-python
import cv2
from datetime import datetime
FILENAME = 'myvideo.avi'
WIDTH = 1280
HEIGHT = 720
FPS = 24.0
# 必須指定CAP_DSHOW(Direct Show)參數(shù)初始化攝像頭,否則無法使用更高分辨率
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
# 設(shè)置攝像頭設(shè)備分辨率
cap.set(cv2.CAP_PROP_FRAME_WIDTH, WIDTH)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, HEIGHT)
# 設(shè)置攝像頭設(shè)備幀率,如不指定,默認(rèn)600
cap.set(cv2.CAP_PROP_FPS, 24)
# 建議使用XVID編碼,圖像質(zhì)量和文件大小比較都兼顧的方案
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)
# 顯示預(yù)覽窗口
cv2.imshow('Preview_Window', frame)
# 錄制5秒后停止
if (datetime.now()-start_time).seconds == 5:
cap.release()
break
# 監(jiān)測到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為電腦內(nèi)置攝像頭
while(True):
ret, frame = capture.read()#攝像頭讀取,ret為是否成功打開攝像頭,true,false。 frame為視頻的每一幀圖像
frame = cv2.flip(frame, 1)#攝像頭是和人對立的,將圖像左右調(diào)換回來正常顯示。
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) # 攝像頭是和人對立的,將圖像左右調(diào)換回來正常顯示
# 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. 打開攝像頭并定時(shí)截圖
def video_demo():
print('開始')
cap = cv2.VideoCapture(0, cv2.CAP_DSHOW) # 電腦自身攝像頭
i = 0#定時(shí)裝置初始值
photoname = 1#文件名序號初始值
while True:
i = i + 1
reg, frame = cap.read()
frame = cv2.flip(frame, 1) # 圖片左右調(diào)換
cv2.imshow('window', frame)
if i == 50: # 定時(shí)裝置,定時(shí)截屏,可以修改。
filename = str(photoname) + '.png' # filename為圖像名字,將photoname作為編號命名保存的截圖
cv2.imwrite('C:/Users/Administrator/Desktop/m' + '\\' + filename, frame) # 截圖 前面為放在桌面的路徑 frame為此時(shí)的圖像
print(filename + '保存成功') # 打印保存成功
i = 0 # 清零
photoname = photoname + 1
if photoname >= 20: # 最多截圖20張 然后退出(如果調(diào)用photoname = 1 不用break為不斷覆蓋圖片)
# photoname = 1
break
if cv2.waitKey(1) 0xff == ord('q'):
break
# 釋放資源
cap.release()
video_demo()
cv2.destroyAllWindows()
#-*- coding: utf-8 -*-
# import 進(jìn)openCV的庫
import cv2
###調(diào)用電腦攝像頭檢測人臉并截圖
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是一個(gè)不可增刪的數(shù)組
color = (0, 255, 0)
num = 0
while cap.isOpened():
ok, frame = cap.read() #讀取一幀數(shù)據(jù)
if not ok:
break
grey = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #將當(dāng)前楨圖像轉(zhuǎn)換成灰度圖像
#人臉檢測,1.2和2分別為圖片縮放比例和需要檢測的有效點(diǎn)數(shù)
faceRects = classfier.detectMultiScale(grey, scaleFactor = 1.2, minNeighbors = 3, minSize = (32, 32))
if len(faceRects) > 0: #大于0則檢測到人臉
for faceRect in faceRects: #單獨(dú)框出每一張人臉
x, y, w, h = faceRect
#將當(dāng)前幀保存為圖片
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): #如果超過指定最大保存數(shù)量退出循環(huán)
break
#畫出矩形框
cv2.rectangle(frame, (x - 10, y - 10), (x + w + 10, y + h + 10), color, 2)
#顯示當(dāng)前捕捉到了多少人臉圖片了,這樣站在那里被拍攝時(shí)心里有個(gè)數(shù),不用兩眼一抹黑傻等著
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(frame,'num:%d/100' % (num),(x + 30, y + 30), font, 1, (255,0,255),4)
#超過指定最大保存數(shù)量結(jié)束程序
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__':
# 連續(xù)截100張圖像,存進(jìn)image文件夾中
CatchPICFromVideo("get face", 0, 99, "/image")
到此這篇關(guān)于python通過opencv調(diào)用攝像頭操作實(shí)例分析的文章就介紹到這了,更多相關(guān)python使用opencv調(diào)用攝像頭操作內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!