我正在嘗試學習人臉檢測,我從GeeksforGeeks教程中獲得了這段代碼。但是,當我運行這兩個檔案之一時,它顯示錯誤AttributeError: module 'cv2' has no attribute 'LBPHFaceRecognizer_create'。我嘗試卸載 open cv,安裝pip install opencv-contrib-python以及重新安裝 open cv 并運行它。我目前正在運行 open cv2 4.5.5。本教程建議從cv2.face.LBPHFaceRecognizer_create()運行 cv2 中洗掉 '.face',但是當我使用 .face 運行它時,它顯示模塊 'cv2' has no attribute 'face'。請有人幫我解決這個問題
# It helps in identifying the faces
import cv2, sys, numpy, os
from cv2 import *
size = 4
haar_file = 'haarcascade_frontalface_default.xml'
datasets = 'datasets'
# Part 1: Create fisherRecognizer
print('Recognizing Face Please Be in sufficient Lights...')
# Create a list of images and a list of corresponding names
(images, labels, names, id) = ([], [], {}, 0)
for (subdirs, dirs, files) in os.walk(datasets):
for subdir in dirs:
names[id] = subdir
subjectpath = os.path.join(datasets, subdir)
for filename in os.listdir(subjectpath):
path = subjectpath '/' filename
label = id
images.append(cv2.imread(path, 0))
labels.append(int(label))
id = 1
(width, height) = (130, 100)
# Create a Numpy array from the two lists above
(images, labels) = [numpy.array(lis) for lis in [images, labels]]
# OpenCV trains a model from the images
# NOTE FOR OpenCV2: remove '.face'
model = cv2.LBPHFaceRecognizer_create()
model.train(images, labels)
for i in range[0, 20]:
if i<10:
print(i)
i = 1
else:
print('Done wit it')
# Part 2: Use fisherRecognizer on camera stream
face_cascade = cv2.CascadeClassifier(haar_file)
webcam = cv2.VideoCapture(0)
while True:
(_, im) = webcam.read()
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
cv2.rectangle(im, (x, y), (x w, y h), (255, 0, 0), 2)
face = gray[y:y h, x:x w]
face_resize = cv2.resize(face, (width, height))
# Try to recognize the face
prediction = model.predict(face_resize)
cv2.rectangle(im, (x, y), (x w, y h), (0, 255, 0), 3)
if prediction[1]<500:
cv2.putText(im, '% s - %.0f' % (names[prediction[0]], prediction[1]), (x-10, y-10), cv2.FONT_HERSHEY_PLAIN, 1, (0, 255, 0))
else:
cv2.putText(im, 'not recognized', (x-10, y-10), cv2.FONT_HERSHEY_PLAIN, 1, (0, 255, 0))
cv2.imshow('OpenCV', im)
key = cv2.waitKey(10)
if key == 27:
break
uj5u.com熱心網友回復:
我認為您可能需要明確宣告“cv2.face”而不僅僅是“face ...”
model = cv2.face.LBPHFaceRecognizer_create()
在這樣做之前...您是否確認您安裝了包含 Face 模塊的 Opencv 版本?你可以這樣檢查:
import cv2
functions = dir(cv2)
for f in functions:
print (f)
如果沒有......像這樣安裝:
pip uninstall opencv-contrib-python
pip install opencv-contrib-python
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/437276.html
