网站首页 > 教程文章 正文
需要安装的库:dlib==19.24.99,face_recognition,opencv等,python环境3.9.19
以识别2个人脸为例,代码如下
import cv2
import face_recognition
# 1. 加载目标人脸照片和提取特征
known_face_encodings = []
known_face_names = []
# 加载 mwj 的照片
mwj_image = face_recognition.load_image_file("lucy.jpg")
mwj_encoding = face_recognition.face_encodings(mwj_image)[0]
known_face_encodings.append(mwj_encoding)
known_face_names.append("lucy")
# 加载 sky 的照片
sky_image = face_recognition.load_image_file("lena.jpg")
sky_encoding = face_recognition.face_encodings(sky_image)[0]
known_face_encodings.append(sky_encoding)
known_face_names.append("lena")
# 2. 打开视频
video_capture = cv2.VideoCapture(0) # 替换为 0 使用摄像头
# video_capture = cv2.VideoCapture("video.mp4") # 替换为 0 使用摄像头
while video_capture.isOpened():
ret, frame = video_capture.read()
if not ret:
break
# 3. 在视频帧中检测人脸
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # 转为 RGB
face_locations = face_recognition.face_locations(rgb_frame)
face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
# 4. 将当前人脸与目标人脸进行比较
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
name = "Unknown"
# 如果匹配到人脸,标记名字
if True in matches:
match_index = matches.index(True)
name = known_face_names[match_index]
# 5. 在视频帧中标记人脸
cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
cv2.putText(frame, name, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
# 显示视频帧
cv2.imshow("Video", frame)
# 按 'q' 键退出
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 释放资源
video_capture.release()
cv2.destroyAllWindows()
如果要检测2人以上的人脸可以使用以下方式实现,代码目录新建一个文件夹faces,里面放命名好人名的头像照片,这样不管3张5张还是更多都可以检测了
import cv2
import face_recognition
import os
# 1. 加载目标人脸照片和提取特征
known_face_encodings = []
known_face_names = []
# 设置存放人脸照片的目录
faces_directory = "faces" # 替换为你的目录路径
# 遍历目录中的所有图片文件
for filename in os.listdir(faces_directory):
if filename.endswith(".jpg") or filename.endswith(".png"):
# 加载图片并提取特征
image_path = os.path.join(faces_directory, filename)
image = face_recognition.load_image_file(image_path)
encoding = face_recognition.face_encodings(image)[0]
# 将特征和名字添加到列表
known_face_encodings.append(encoding)
name = os.path.splitext(filename)[0] # 使用文件名作为名字
known_face_names.append(name)
print(f"已加载以下人脸: {known_face_names}")
# 2. 打开视频
video_capture = cv2.VideoCapture(0) # 使用摄像头
# video_capture = cv2.VideoCapture("video.mp4") # 使用视频文件
while video_capture.isOpened():
ret, frame = video_capture.read()
if not ret:
break
# 3. 在视频帧中检测人脸
rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # 转为 RGB
face_locations = face_recognition.face_locations(rgb_frame)
face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
# 4. 将当前人脸与目标人脸进行比较
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
name = "Unknown"
# 如果匹配到人脸,标记名字
if True in matches:
match_index = matches.index(True)
name = known_face_names[match_index]
# 5. 在视频帧中标记人脸
cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
cv2.putText(frame, name, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
# 显示视频帧
cv2.imshow("Video", frame)
# 按 'q' 键退出
# if cv2.waitKey(1) & 0xFF == ord('q'):
# 按 'Esc' 键退出
if cv2.waitKey(1) & 0xFF == 27: # 27 是 Esc 键的键值
break
# 释放资源
video_capture.release()
cv2.destroyAllWindows()
猜你喜欢
- 2025-04-30 Ai机器视觉之口罩检测(口罩视觉检测方案)
- 2025-04-30 C#WinForm调用Yolov8Net实现自动识别
- 2025-04-30 探索Python中的人脸识别:深入pyfacelib库
- 2025-04-30 Pytorch利用CNN卷积神经网络进行多数字(0-9)识别
- 2025-04-30 【AI&ML】如何使用Google Colaboratory进行视频处理
- 2025-04-30 自动识别影视场景中的演员(自动识别电影)
- 2025-04-30 Python编程 - 基于OpenCV实现人脸识别(实践篇)爬虫+人脸识别
- 2025-04-30 使用Python和YOLO检测车牌(python判断车牌号归属地)
- 2025-04-30 用python给图片批量打水印(python批量去水印)
- 2025-04-30 简单易懂的人脸识别!用PythonOpenCV实现(适合初学者)!附源码
- 最近发表
- 标签列表
-
- location.href (44)
- document.ready (36)
- git checkout -b (34)
- 跃点数 (35)
- 阿里云镜像地址 (33)
- qt qmessagebox (36)
- md5 sha1 (32)
- mybatis plus page (35)
- semaphore 使用详解 (32)
- update from 语句 (32)
- vue @scroll (38)
- 堆栈区别 (33)
- 在线子域名爆破 (32)
- 什么是容器 (33)
- sha1 md5 (33)
- navicat导出数据 (34)
- 阿里云acp考试 (33)
- 阿里云 nacos (34)
- redhat官网下载镜像 (36)
- srs服务器 (33)
- pico开发者 (33)
- https的端口号 (34)
- vscode更改主题 (35)
- 阿里云资源池 (34)
- os.path.join (33)