人脸检测——基于face_recognition库

1.face_recognition库安装

        安装face_recognition库需要依赖很多模块。

2.基于face_recognition人脸检测

        代码如下:

# 1.导入库文件
import face_recognition
import cv2
import time

# 2.记录节拍
t = time.time()

# 3.将jpg文件加载到numpy 数组中
image = face_recognition.load_image_file("img_1.jpg")
frame = cv2.imread("img_1.jpg")

# 4.使用默认的给予HOG模型查找图像中所有人脸
# 这个方法已经相当准确了,但还是不如CNN模型那么准确,因为没有使用GPU加速
face_locations = face_recognition.face_locations(image)

# 使用CNN模型
# face_locations = face_recognition.face_locations(image, number_of_times_to_upsample=0, model="cnn")

# 5.打印:我从图片中找到了 多少 张人脸
print("I found {} face(s) in this photograph.".format(len(face_locations)))

# 6.循环找到的所有人脸
for face_location in face_locations:
    # 打印每张脸的位置信息
    top, right, bottom, left = face_location
    print(
        "A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))

    # 指定人脸的位置信息,然后显示人脸图片
    cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)

cv2.imshow('image', frame)
cv2.imwrite('new_image.jpg',frame)
cv2.waitKey(1)  # 刷新界面 不然只会呈现灰色
print('运行时间{}'.format(time.time() - t))
cv2.waitKey(0)
cv2.destroyAllWindows()

        结果显示:

参考:

基于python3、 face_recognition 实现人脸检测,人脸识别_Nie_Hen的博客-CSDN博客_facerecognition实现人脸检测

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的基于face_recognition的人脸识别方案代码示例: ```python import face_recognition import cv2 # 加载已知人脸的图像和名称 known_face_encodings = [] known_face_names = [] image1 = face_recognition.load_image_file("person1.jpg") image2 = face_recognition.load_image_file("person2.jpg") image3 = face_recognition.load_image_file("person3.jpg") known_face_encodings.append(face_recognition.face_encodings(image1)[0]) known_face_encodings.append(face_recognition.face_encodings(image2)[0]) known_face_encodings.append(face_recognition.face_encodings(image3)[0]) known_face_names = ["person1", "person2", "person3"] # 打开摄像头 cap = cv2.VideoCapture(0) while True: # 读取一帧图像 ret, frame = cap.read() # 将图像转换为RGB格式 rgb_frame = frame[:, :, ::-1] # 检测人脸 face_locations = face_recognition.face_locations(rgb_frame) face_encodings = face_recognition.face_encodings(rgb_frame, face_locations) # 遍历所有检测到的人脸 for face_encoding, face_location in zip(face_encodings, face_locations): # 尝试匹配人脸 matches = face_recognition.compare_faces(known_face_encodings, face_encoding) # 如果有匹配到的人脸,则取出其名称 if True in matches: match_index = matches.index(True) name = known_face_names[match_index] else: name = "unknown" # 绘制人脸矩形框和名称 top, right, bottom, left = face_location cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2) cv2.putText(frame, name, (left, top-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) # 显示图像 cv2.imshow('Face Recognition', frame) # 按下q键退出 if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放摄像头和窗口 cap.release() cv2.destroyAllWindows() ``` 在这个代码示例中,我们使用face_recognition加载了已知人脸的图像和名称,并使用cv2.VideoCapture()方法打开了摄像头。在每一帧图像中,我们使用face_recognition检测人脸,并使用face_recognition.compare_faces()方法尝试匹配人脸。如果有匹配到的人脸,则取出其名称,并使用cv2.rectangle()和cv2.putText()方法绘制人脸矩形框和名称。最后,我们使用cv2.imshow()方法显示图像,并使用cv2.waitKey()方法等待用户按下q键退出程序。 值得注意的是,face_recognition是一个基于dlib的人脸识别,其检测速度较慢,但是识别精度较高。在实际应用中,我们需要根据具体情况选择不同的人脸识别方法以达到更好的效果。 希望这个代码对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值