前面几篇文章我们尝试了使用opencv完成图像人脸识别以及识别后贴图或者打马赛克的方法。
偶尔我们也会有需求在视频中将人脸马赛克化,opencv也提供了相应的方法来实现这个功能。
视频究其本质是图像按照一定的帧率去播放。如果需要将视频中的人脸马赛克化,那么我们可以逐帧输出图像后进行识别人脸再对其马赛克化,最终将所有的图像再按一定的帧率组合播放。
#识别视频人脸并增加马赛克
#实现原理:cv2读取视频后逐帧识别人脸并增加马赛克/贴图,处理完毕后保存视频
import cv2
# laod opencv schema
classifier = cv2.CascadeClassifier("haarcascade_frontalface_alt2.xml")#实践下来貌似这个schema匹配度最高
blockimg = "block.jpg"#贴图路径
#马赛克化
def do_mosaic(frame, x, y, w, h, neighbor=20):
fh, fw = frame.shape[0], frame.shape[1]
if (y + h > fh) or (x + w > fw):
return
for i in range(0, h - neighbor, neighbor): # 关键点0 减去neightbour 防止溢出
for j in range(0, w - neighbor, neighbor):
rect = [j + x, i + y, neighbor, neighbor]
color = frame[i + y][j + x].tolist() # 关键点1 tolist
left_up = (rect[0], rect[1])
right_down = (rect[0] + neighbor - 1, rect[1] + neighbor - 1) # 关键点2 减去一个像素
cv2.rectangle(frame, left_up, right_down, color, -1)
#贴图处理
def do_blockpic(frame, x, y, w, h):
resizeimg = cv2.imread(blockimg)
resizeimg_f = cv2.resize(resizeimg,(w,h))
frame[y:y+h, x:x+w] = resizeimg_f
#识别人脸
def do_identifyFace(frame):
color = (0, 255, 0)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # convert to grey
# begin to identify face
faceRects = classifier.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=3, minSize=(32, 32))
if len(faceRects): # get faces if above zero
for faceRect in faceRects: # loop each face
x, y, w, h = faceRect
#do_blockpic(frame, x, y, w, h)
do_mosaic(frame, x, y, w, h)
#main starts
srcVideo = "srcVideo.mp4"#源视频
savedVideo = "savedVideo.mp4"#处理后的视频
cap = cv2.VideoCapture(srcVideo)
if not cap.isOpened():
print("error to open source video")
exit()
print("got source video")
w = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
h = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
fps = cap.get(cv2.CAP_PROP_FPS)
fcount = cap.get(cv2.CAP_PROP_FRAME_COUNT)
print("total frames %s"%fcount)#获取所有帧数
writer = cv2.VideoWriter(savedVideo, cv2.VideoWriter_fourcc('m', 'p', '4', 'v'), int(fps), (int(w), int(h)))
print("start handle source video")
i = 0
while cap.isOpened():
success, frame = cap.read()
while success:
do_identifyFace(frame)
print("finish frame %s"%i)
writer.write(frame)
#cv2.imwrite("frame%s.jpg"%i, frame)
i += 1
success, frame = cap.read()
if (cv2.waitKey(20) & 0xff) == ord('q'):
break
cap.release()
print("finish handle source video")
writer.release()
cv2.destroyAllWindows()
处理后的视频效果