想安装 OpenCV,对库进行 pip 安装:
pip3 install opencv-python ③安装 face_recognition API
最后,我们将使用 face_recognition,这号称是世界上最简单的面向 Python 的人脸识别 API 。
想安装它,请运行下列命令:
pip install dlib pip install face_recognition 深入了解实现方式
【超有趣!手把手教你用Python实现实时“人脸检测”】现在你已设置好了系统,终于可以深入了解实际的实现方式 。首先,我们将迅速构建程序,然后对其分解以了解我们所做的工作 。
先创建一个文件 face_detector.py,然后拷贝如下所示的代码:
# import libraries import cv2 importface_recognition# Get a reference towebcam video_capture =cv2.VideoCapture("/dev/video1")# Initialize variables face_locations = []while True:# Grab a single frame of videoret, frame = video_capture.read()# Convert the image from BGR color (whichOpenCV uses) to RGB color (which face_recognition uses)rgb_frame = frame[:, :, ::-1]# Find all the faces in the current frameof videoface_locations =face_recognition.face_locations(rgb_frame)# Display the resultsfor top, right, bottom, left inface_locations:# Draw a box around the facecv2.rectangle(frame, (left, top),(right, bottom), (0, 0, 255), 2)# Display the resulting imagecv2.imshow('Video', frame)# Hit 'q' on the keyboard to quit!if cv2.waitKey(1) & 0xFF == ord('q'):break# Release handle tothe webcam video_capture.release() cv2.destroyAllwindows() 然后,输入以下命令,运行该 Python 文件:
python face_detector.py 如果一切正常,会弹出一个新窗口,实时人脸检测在运行中 。
总结一下,这是我们上述代码执行的操作:
- 首先,我们定义了将进行视频分析的硬件 。
- 此后,我们实时捕捉视频,逐帧捕捉 。
- 然后,我们处理每帧,并提取图像中所有人脸的位置 。
- 最后,我们以视频形式渲染这些帧以及人脸位置 。
人脸检测的用例
乐趣并不仅限于此!我们能做的另一件很酷的事情就是围绕上述代码构建完整的用例 。而且你无需从头开始,我们只要对代码进行几处小小的改动即可 。
比如说,假设你想构建一个基于摄像头的自动系统来实时跟踪说话人的位置 。根据其位置,系统转动摄像头,以便说话人始终在视频的中间 。
我们该如何解决这个问题?第一步是构建识别视频中一个人或多个人的系统,并关注说话人的位置 。
不妨看看我们如何实现这一点 。为了本文需要,我从 Youtube 上下载了一段视频(https://youtu.be/A_-KqX-RazQ),视频中有个人在 2017 年 DataHack 峰会上讲话 。
首先,我们导入必要的库:
import cv2 importface_recognition 然后,阅读视频并获取长度:
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()
推荐阅读
- 工资报告:北京月薪1万感觉在吃土,八成人工作超十年月薪不过万
- 12月份手机性能排行榜 超薄智能手机排行
- 南方航空|民航业“至暗时刻” 中国三大航企2021巨亏超400亿元:行业仅2家赚钱
- 手把手教你办理电子营业执照 网上办理营业执照流程
- 正宗金骏眉品质特点 真假金骏眉的区别方法超全解析
- 鸿蒙2.0也有无字模式?这篇文章手把手教会你
- 6个隐藏超深的小米手机功能,每个都是日常所需,真是太实用了
- 爽肤水|什么爽肤水好用?这几款吸收快,补水效果好,超清爽嫩滑的肤感
- App|i茅台App首次申购超650万人次参与!中签率低至0.25%:比打新股还难
- 38种超级耐阴的花,没光也能养爆盆,养在阳台客厅超有范儿
