一、人臉檢測
案例代碼如下:
import cv2
import numpy as np
video = cv2.VideoCapture('1.mp4')
face_detector = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
while True:
retval,image = video.read() # retval boolean類型表名是否獲得了圖片
if not retval:
print('視頻讀取完成,沒有圖片')
break
faces = face_detector.detectMultiScale(image)
for x,y,w,h in faces:
cv2.rectangle(image,pt1=(x,y),pt2 = (x+w,y+h),color=[0,0,255],thickness=2)
cv2.imshow('video',image)
key = cv2.waitKey(1)
if key == ord('q'):
break
cv2.waitKey(1)
video.release()
二、馬賽克處理
視頻幀率、寬度、高度、圖片個數(shù)計算:
import cv2
import numpy as np
video = cv2.VideoCapture('1.mp4')
# 獲得視頻中的屬性
fps = video.get(propId=cv2.CAP_PROP_FPS) #得到幀率
width = video.get(propId=cv2.CAP_PROP_FRAME_WIDTH) #寬度
height = video.get(propId=cv2.CAP_PROP_XI_HEIGHT) #高度
count = video.get(propId=cv2.CAP_PROP_FRAME_COUNT) #多少圖片
print('--視頻幀率',fps)
print(width,height,count)
face_detector = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
while True:
retval,image = video.read() # retval boolean類型表名是否獲得了圖片
if not retval:
print('視頻讀取完成,沒有圖片')
break
faces = face_detector.detectMultiScale(image)
for x,y,w,h in faces:
# cv2.rectangle(image,pt1=(x,y),pt2 = (x+w,y+h),color=[0,0,255],thickness=2)
face = image[y:y+h,x:x+w]
face = face[::10,::10]
face = np.repeat(face,10,axis=0)
face = np.repeat(face,10,axis =1)
image[y:y+h,x:x+w] = face[:h,:w]
cv2.imshow('video',image)
key = cv2.waitKey(1)
if key == ord('q'):
break
cv2.waitKey(1)
video.release()
三、寫視頻
案例代碼如下:文章來源:http://www.zghlxwxcb.cn/news/detail-602309.html
import cv2
import numpy as np
video = cv2.VideoCapture('1.mp4')
wr = cv2.VideoWriter(filename = 'gray.mp4',
fourcc = cv2.VideoWriter.fourcc(*'MP4'),
fps = 24, #視頻幀率
framesize = (640,360)) #圖片尺寸
# 獲得視頻中的屬性
fps = video.get(propId=cv2.CAP_PROP_FPS) #得到幀率
width = video.get(propId=cv2.CAP_PROP_FRAME_WIDTH) #寬度
height = video.get(propId=cv2.CAP_PROP_XI_HEIGHT) #高度
count = video.get(propId=cv2.CAP_PROP_FRAME_COUNT) #多少圖片
print('--視頻幀率',fps)
print(width,height,count)
face_detector = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
while True:
retval,image = video.read() # retval boolean類型表名是否獲得了圖片
if not retval:
print('視頻讀取完成,沒有圖片')
break
image = cv2.resize(image,(640,360))
gray = cv2.cvtColor(image,code = cv2.COLOR_BGR2GRAY)
np.repeat(gray.reshape(360,640,1),3,axis=2) #藍綠紅變?yōu)槿S,但值是一樣的
# wr.write(gray)
faces = face_detector.detectMultiScale(image)
for x,y,w,h in faces:
# cv2.rectangle(image,pt1=(x,y),pt2 = (x+w,y+h),color=[0,0,255],thickness=2)
face = image[y:y+h,x:x+w]
face = face[::10,::10]
face = np.repeat(face,10,axis=0)
face = np.repeat(face,10,axis =1)
image[y:y+h,x:x+w] = face[:h,:w]
wr.write(image) #彩色圖片是三維的,而黑白圖片是二維的
cv2.imshow('video',image)
key = cv2.waitKey(1)
if key == ord('q'):
break
cv2.waitKey(1)
video.release()
wr.release()
四、攝像頭識別人臉
案例代碼如下:文章來源地址http://www.zghlxwxcb.cn/news/detail-602309.html
import cv2
import numpy as np
# 打開本機攝像頭
cap = cv2.VideoCapture(0)
face_detector = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
while True:
flag,frame = cap.read() #flag是否讀取了圖片
if not flag:
break
# 轉(zhuǎn)為灰度圖片
gray = cv2.cvtColor(frame,code = cv2.COLOR_BGR2GRAY)
faces = face_detector.detectMultiScale(gray,scaleFactor=1.1,minNeighbors=10)
for x,y,w,h in faces:
cv2.rectangle(frame,
pt1=(x,y),
pt2=(x+w,y+h),
color=[0,0,255],
thickness=2)
cv2.imshow('face',frame)
key = cv2.waitKey(1000//24)
if key == ord('q'):
break
cv2.destroyAllWindows()
cap.release()
到了這里,關(guān)于OpenCv之視頻人臉識別的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!