案例代码如下:
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()
视频帧率、宽度、高度、图片个数计算:
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()
案例代码如下:
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) #蓝绿红变为三维,但值是一样的
# 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()
案例代码如下:
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
# 转为灰度图片
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()