网站首页 > 技术文章 正文
需要安装的库: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()
- 上一篇: python摄像头编程
- 下一篇: Python人脸识别实现方法与步骤
猜你喜欢
- 2025-05-26 智能家居安防系统:AI与Python如何打造更安全的家
- 2025-05-26 在安卓设备上运行Python的方法
- 2025-05-26 基于OpenCv的人脸识别(Python完整代码)-大盘站
- 2025-05-26 边缘计算革命——Python在IoT与自动驾驶中的新战场
- 2025-05-26 还有这操作?我用Python这个库竟然实现了隔空操作
- 2025-05-26 一文带你深入了解Python+MediaPipe实现检测人脸的功能
- 2025-05-26 树莓派制成的 — 带运动检测和摄像头的安防系统
- 2025-05-26 用投影仪+摄像头DIY一台结构光3D扫描仪
- 2025-05-26 OpenCV-python 教你如何播放视频
- 2025-05-26 探索Python中的人脸识别:深入pyfacelib库
- 258℃Python短文,Python中的嵌套条件语句(六)
- 258℃python笔记:for循环嵌套。end=""的作用,图形打印
- 257℃PythonNet:实现Python与.Net代码相互调用!
- 252℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 252℃Python实现字符串小写转大写并写入文件
- 108℃原来2025是完美的平方年,一起探索六种平方的算吧
- 91℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 83℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)