找到了这个称为人脸识别[https://github.com/ageitgey/face_recognition]的库。认为这对我的需求有用。 试图修改其中一个示例,特别是网络摄像头输入模糊,但遗憾的是输出文件不包含任何模糊的面孔。
我使用了以下两个示例:https://github.com/ageitgey/face_recognition/blob/master/examples/facerec_from_video_file.py https://github.com/ageitgey/face_recognition/blob/master/examples/blur_faces_on_webcam.py 并提出了这个
import face_recognition
import cv2
# This is a demo of running face recognition on a video file and saving the results to a new video file.
#
# PLEASE NOTE: This example requires OpenCV (the `cv2` library) to be installed only to read from your webcam.
# OpenCV is *not* required to use the face_recognition library. It's only required if you want to run this
# specific demo. If you have trouble installing it, try any of the other demos that don't require it instead.
# Open the input movie file
input_movie = cv2.VideoCapture("test2.mp4")
length = int(input_movie.get(cv2.CAP_PROP_FRAME_COUNT))
# Create an output movie file (make sure resolution/frame rate matches input video!)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
output_movie = cv2.VideoWriter('test2.avi', fourcc, 10, (1920, 1080))
# Initialize some variables
face_locations = []
frame_number = 0
while True:
# Grab a single frame of video
ret, frame = input_movie.read()
frame_number += 1
# Quit when the input video file ends
if not ret:
break
# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
# Find all the faces and face encodings in the current frame of video
face_locations = face_recognition.face_locations(small_frame, model="cnn")
# Label the results
for top, right, bottom, left in face_locations:
top *= 4
right *= 4
bottom *= 4
left *= 4
face_image = frame[top:bottom, left:right]
face_image = cv2.GaussianBlur(face_image, (99, 99), 30)
frame[top:bottom, left:right] = face_image
# Write the resulting image to the output video file
print("Writing frame {} / {}".format(frame_number, length))
output_movie.write(frame)
# All done!
input_movie.release()
cv2.destroyAllWindows()