AttributeError:模块“dlib”没有属性“get_frontal_face_detector”
Posted
技术标签:
【中文标题】AttributeError:模块“dlib”没有属性“get_frontal_face_detector”【英文标题】:AttributeError: module 'dlib' has no attribute 'get_frontal_face_detector' 【发布时间】:2021-07-30 22:34:19 【问题描述】:我正在尝试运行代码以直接从网络摄像头识别人脸,但我遇到了以下错误。
C:\Users\user\python.exe C:/Users/user/PycharmProjects/FaceRecognition/Attendance.py
Traceback (most recent call last):
File "C:/Users/user/PycharmProjects/FaceRecognition/Attendance.py", line 3, in <module>
import face_recognition
File "C:\Users\user\AppData\Roaming\Python\Python38\site-packages\face_recognition\__init__.py", line 7, in <module>
from .api import load_image_file, face_locations, batch_face_locations, face_landmarks, face_encodings, compare_faces, face_distance
File "C:\Users\user\AppData\Roaming\Python\Python38\site-packages\face_recognition\api.py", line 17, in <module>
face_detector = dlib.get_frontal_face_detector()
AttributeError: module 'dlib' has no attribute 'get_frontal_face_detector'`
我已经下载了人脸识别库,并且在与项目文件夹相同的文件夹中也有 dlib 依赖项,但我遇到了这个问题。
这是我的全部代码
import cv2
import numpy as np
import face_recognition
import os
from datetime import datetime
# from PIL import ImageGrab
path = 'ImagesAttendance' images = [] classNames = [] myList = os.listdir(path) print(myList) for cl in myList:
curImg = cv2.imread(f'path/cl')
images.append(curImg)
classNames.append(os.path.splitext(cl)[0]) print(classNames)
def findEncodings(images):
encodeList = []
for img in images:
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
encode = face_recognition.face_encodings(img)[0]
encodeList.append(encode)
return encodeList
def markAttendance(name):
with open('Attendance.csv', 'r+') as f:
myDataList = f.readlines()
nameList = []
for line in myDataList:
entry = line.split(',')
nameList.append(entry[0])
if name not in nameList:
now = datetime.now()
dtString = now.strftime('%H:%M:%S')
f.writelines(f'nname,dtString')
#### FOR CAPTURING SCREEN RATHER THAN WEBCAM
# def captureScreen(bbox=(300,300,690+300,530+300)):
# capScr = np.array(ImageGrab.grab(bbox))
# capScr = cv2.cvtColor(capScr, cv2.COLOR_RGB2BGR)
# return capScr
encodeListKnown = findEncodings(images) print('Encoding Complete')
cap = cv2.VideoCapture(0)
while True:
success, img = cap.read()
# img = captureScreen()
imgS = cv2.resize(img, (0, 0), None, 0.25, 0.25)
imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
facesCurFrame = face_recognition.face_locations(imgS)
encodesCurFrame = face_recognition.face_encodings(imgS, facesCurFrame)
for encodeFace, faceLoc in zip(encodesCurFrame, facesCurFrame):
matches = face_recognition.compare_faces(encodeListKnown, encodeFace)
faceDis = face_recognition.face_distance(encodeListKnown, encodeFace)
# print(faceDis)
matchIndex = np.argmin(faceDis)
if matches[matchIndex]:
name = classNames[matchIndex].upper()
# print(name)
y1, x2, y2, x1 = faceLoc
y1, x2, y2, x1 = y1 * 4, x2 * 4, y2 * 4, x1 * 4
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.rectangle(img, (x1, y2 - 35), (x2, y2), (0, 255, 0), cv2.FILLED)
cv2.putText(img, name, (x1 + 6, y2 - 6), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2)
markAttendance(name)
cv2.imshow('Webcam', img) cv2.waitKey(1)
【问题讨论】:
【参考方案1】:Dlib 是一个了不起的库,但它的安装成本很高。我建议你使用 deepface。它包装了 dlib,但它带有几个尖端的面部检测器。除了 dlib 之外,它们都是基于 tensorflow 的。这就是为什么安装和运行这些检测器会比核心 dlib 更容易。
img_path = "img1.jpg"
backends = ['opencv', 'ssd', 'mtcnn', 'dlib', 'retinaface']
img = DeepFace.detectFace(img_path, detector_backend = backend[2])
【讨论】:
以上是关于AttributeError:模块“dlib”没有属性“get_frontal_face_detector”的主要内容,如果未能解决你的问题,请参考以下文章
Google App Engine - ModuleNotFoundError:没有名为“dlib”的模块
模块“dlib”没有属性“get_frontal_face_detector”错误
AttributeError:模块 'dbus' 没有属性 'lowlevel'
AttributeError:模块'keras'没有属性'initializers'