`` `“我正在尝试启动 LBHPFaceRecognizer,但问题是每次我使用 cv2.face 时,它总是给我一个属性错误。不仅仅是 LBHPFaceRecognizer。我尝试通过添加括号来使用函数启动它,尝试了 createLBHPFaceRecognizer,尝试了 LBHPFaceRecognizer_create(),但没有任何效果。另外,在我输入 cv2.face 后,会弹出“LBHPFaceRecognizer”选项。我提供了下面的代码,它位于最后几行,代码下面是我的点列表。我尝试降级 opencv-contrib-python,但没有成功............ ......................
`import cv2
import cv2.face
import os
def generate_dataset():
face_classifier = cv2.CascadeClassifier("C:/Users/Abdallah/AppData/Local/Programs/Python/Python312/Lib/site-packages/cv2/data/haarcascade_frontalface_default.xml")
def face_cropped(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_classifier.detectMultiScale(gray, 1.3, 5)
# scaling factor=1.3
# Minimum neighbor = 5
if len(faces) == 0:
return None
for (x, y, w, h) in faces:
cropped_face = img[y:y+h, x:x+w]
return cropped_face
cap = cv2.VideoCapture(0)
id = 1
img_id = 0
while True:
ret, frame = cap.read()
if ret:
if face_cropped(frame) is not None:
img_id += 1
face = cv2.resize(face_cropped(frame), (200, 200))
face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
file_name_path = "C:/Users/Abdallah/AppData/Local/Programs/Python/Python312/Lib/site-packages/cv2/data/data/user." + str(id) + "." + str(img_id) + ".jpg"
cv2.imwrite(file_name_path, face)
cv2.putText(face, str(img_id), (50, 50), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2)
#(50,50) is the origin point from where text is to be written
#font scale=1
#thickness=2
cv2.imshow("Cropped face", face)
if cv2.waitKey(1) == 13 or int(img_id) == 200:
break
else:
print("No face detected")
else:
print("Error: Failed to read frame from the camera")
cap.release()
cv2.destroyAllWindows()
print("Collecting samples is completed......")
#generate_dataset()
from PIL import Image
import numpy as np
def train_classifier(data_dir):
path = [os.path.join(data_dir, f) for f in os.listdir(data_dir)]
faces = []
ids = []
for image in path:
img = Image.open(image).convert('L')
imageNp = np.array(img, 'uint8')
id = int(os.path.split(image)[1].split(".")[1])
faces.append(imageNp)
ids.append(id)
ids = np.array(ids)
clf = cv2.face.LBPHFaceRecognizer
clf.train(faces, ids)
clf.write("classifier.xml")
train_classifier("C:/Users/Abdallah/AppData/Local/Programs/Python/Python312/Lib/site-packages/cv2/data/data")`
absl-py 2.1.0
astunparse 1.6.3
certifi 2024.2.2
charset-normalizer 3.3.2
colorama 0.4.6
contourpy 1.2.0
cycler 0.12.1
engineering-notation 0.10.0
filelock 3.13.4
flatbuffers 24.3.25
fonttools 4.50.0
fsspec 2024.3.1
gast 0.5.4
google-pasta 0.2.0
grpcio 1.62.2
h5py 3.11.0
huggingface-hub 0.22.2
idna 3.7
joblib 1.3.2
keras 3.3.2
kiwisolver 1.4.5
libclang 18.1.1
Markdown 3.6
markdown-it-py 3.0.0
MarkupSafe 2.1.5
matplotlib 3.8.3
mdurl 0.1.2
ml-dtypes 0.3.2
namex 0.0.8
numpy 1.26.4
opencv-contrib-python 4.9.0.80
opencv-python 4.9.0.80
opt-einsum 3.3.0
optree 0.11.0
packaging 24.0
pandas 2.2.1
pillow 10.2.0
pip 24.0
protobuf 4.25.3
Pygments 2.17.2
pyparsing 3.1.2
python-dateutil 2.9.0.post0
pytz 2024.1
tensorflow-intel 2.16.1
termcolor 2.4.0
threadpoolctl 3.4.0
tk-tools 0.16.0
tokenizers 0.19.1
tqdm 4.66.2
transformers 4.40.1
typing_extensions 4.11.0
tzdata 2024.1
urllib3 2.2.1
Werkzeug 3.0.2
wheel 0.43.0
wrapt 1.16.0
......
Traceback (most recent call last):
File "C:\\Users\\Abdallah\\Downloads\\Code Linear Regression-20240324\\deep.py", line 68, in \<module\>
train_classifier("C:/Users/Abdallah/AppData/Local/Programs/Python/Python312/Lib/site-packages/cv2/data/data")
File "C:\\Users\\Abdallah\\Downloads\\Code Linear Regression-20240324\\deep.py", line 65, in train_classifier
clf = cv2.face.LBPHFaceRecognizer
^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'cv2.face' has no attribute 'LBPHFaceRecognizer'
Process finished with exit code 1`
``
更改此:
clf = cv2.face.LBPHFaceRecognizer
至:
clf = cv2.face.LBPHFaceRecognizer_create()