网站首页 > 技术文章 正文
需要安装的库: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-01-09 谈一谈WEB3.0的CMS
- 2025-01-09 Bilingual 丨佛山入境人员通行证、电子通行证登记最全操作指引来了
- 2025-01-09 美军开发远程人脸识别系统,实现1公里内目标识别
- 2025-01-09 利用Python图像搜索:给你爬的美女图建一个搜索引擎
- 2025-01-09 怎样用3分钟搭建 Python 人脸识别系统
- 2025-01-09 Go 人脸识别教程
- 2025-01-09 40行代码实现人脸识别
- 2025-01-09 人脸嵌入向量的提取计算
- 2025-01-09 简单的Py人脸识别
- 2025-01-09 听心跳识人!五角大楼发明一种激光,可从200米外识人
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- oraclesql优化 (66)
- 类的加载机制 (75)
- feignclient (62)
- 一致性hash算法 (71)
- dockfile (66)
- 锁机制 (57)
- javaresponse (60)
- 查看hive版本 (59)
- phpworkerman (57)
- spark算子 (58)
- vue双向绑定的原理 (68)
- springbootget请求 (58)
- docker网络三种模式 (67)
- spring控制反转 (71)
- data:image/jpeg (69)
- base64 (69)
- java分页 (64)
- kibanadocker (60)
- qabstracttablemodel (62)
- java生成pdf文件 (69)
- deletelater (62)
- com.aspose.words (58)
- android.mk (62)
- qopengl (73)
- epoch_millis (61)
本文暂时没有评论,来添加一个吧(●'◡'●)