然后 , 阅读视频并获取长度:
input_movie =cv2.VideoCapture("sample_video.mp4") length = int(input_movie.get(cv2.CAP_PROP_FRAME_COUNT)) 之后 , 我们创建一个拥有所需分辨率和帧速率的输出文件 , 与输入文件类似 。
加载说话人的示例图像以便在视频中识别他:
image =face_recognition.load_image_file("sample_image.jpeg") face_encoding =face_recognition.face_encodings(image)[0]known_faces = [ face_encoding, ] 这一切都已完成 , 现在我们运行一个循环 , 它将执行以下操作:
- 从视频中提取帧 。
- 找到所有人脸 , 并识别它们 。
- 创建一个新视频 , 将原始帧与标注的说话人人脸位置相结合 。
# Initialize variables face_locations = [] face_encodings = [] face_names = [] frame_number = 0while True:# Grab a single frame of videoret, frame = input_movie.read()frame_number += 1# Quit when the input video file endsif not ret:break# Convert the image from BGR color (whichOpenCV uses) to RGB color (which face_recognition uses)rgb_frame = frame[:, :, ::-1]# Find all the faces and face encodings inthe current frame of videoface_locations =face_recognition.face_locations(rgb_frame, model="cnn")face_encodings =face_recognition.face_encodings(rgb_frame, face_locations)face_names = []for face_encoding in face_encodings:# See if the face is a match for theknown face(s)match =face_recognition.compare_faces(known_faces, face_encoding, tolerance=0.50)name = Noneif match[0]:name = "Phani Srikant"face_names.append(name)# Label the resultsfor (top, right, bottom, left), name inzip(face_locations, face_names):if not name:continue# Draw a box around the facecv2.rectangle(frame, (left, top),(right, bottom), (0, 0, 255), 2)# Draw a label with a name below thefacecv2.rectangle(frame, (left, bottom -25), (right, bottom), (0, 0, 255), cv2.FILLED)font = cv2.FONT_HERSHEY_DUPLEXcv2.putText(frame, name, (left + 6,bottom - 6), font, 0.5, (255, 255, 255), 1)# Write the resulting image to the outputvideo fileprint("Writing frame {} /{}".format(frame_number, length))output_movie.write(frame)# All done! input_movie.release() cv2.destroyAllWindows() 然后代码会给出这样的输出:
人脸检测真是了不起的本领 。
结论
恭喜!你现在知道如何为许多潜在用例构建人脸检测系统了 。 深度学习是非常迷人的领域 , 我很期望下一步的方向 。
我们在本文中学习了如何利用开源工具构建具有实际用途的实时人脸检测系统 。
我鼓励各位构建众多这样的应用 , 并自己试一试 。 相信我 , 你能学到好多东西 , 而且蛮有意思 。
【51CTO原创稿件 , 合作站点转载请注明原文作者和出处为51CTO.com】
推荐阅读
- 计算机专业大一下学期,该选择学习Java还是Python
- 想自学Python来开发爬虫,需要按照哪几个阶段制定学习计划
- 未来想进入AI领域,该学习Python还是Java大数据开发
- 2021年Java和Python的应用趋势会有什么变化?
- 非计算机专业的本科生,想利用寒假学习Python,该怎么入手
- 用Python制作图片验证码,这三行代码完事儿
- 手把手配置HLS流媒体服务器
- 历时 1 个月,做了 10 个 Python 可视化动图,用心且精美...
- 为何在人工智能研发领域Python应用比较多
- 对于非计算机专业的同学来说,该选择学习Python还是C
