我想获得信心,但是当我尝试clf.score(X_test)
时,我收到以下错误:
TypeError:score()至少需要3个参数(给定2个)
这是我的代码:
import cv2, os
from numpy import *
import numpy as np
from PIL import Image
from sklearn.decomposition import PCA
from sklearn.decomposition import RandomizedPCA
from sklearn.naive_bayes import GaussianNB
import matplotlib.pyplot as plt
from sklearn.svm import SVC
cascadeLocation = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadeLocation)
def prepare_dataset(directory):
paths = [os.path.join(directory, filename) for filename in os.listdir(directory)]
images = []
labels = []
row = 140
col = 140
for image_path in paths:
image_pil = Image.open(image_path).convert('L')
image = np.array(image_pil, 'uint8')
nbr = int(os.path.split(image_path)[-1].split('.')[1])
print(nbr)
faces = faceCascade.detectMultiScale(image)
for (x,y,w,h) in faces:
images.append(image[y:y+col,x:x+row])
labels.append(nbr)
cv2.imshow("Reading Faces ",image[y:y+col,x:x+row])
cv2.waitKey(50)
return images,labels, row, col
directory = 'dataset'
directory2 = 'dataset2'
images, labels, row, col = prepare_dataset(directory)
n_components = 10
cv2.destroyAllWindows()
pca = PCA(n_components=n_components, whiten=True)
param_grid = {'C': [1e3, 5e3, 1e4, 5e4, 1e5],
'gamma': [0.0001, 0.0005, 0.001, 0.005, 0.01, 0.1], }
clf = SVC(kernel='rbf', class_weight='balanced')
testing_data = []
for i in range(len(images)):
testing_data.append(images[i].flatten())
pca = pca.fit(testing_data)
transformed = pca.transform(testing_data)
clf.fit(transformed,labels)
image_paths = [os.path.join(directory2, filename) for filename in os.listdir(directory2)]
la=[]
for image_path in image_paths:
pred_image_pil = Image.open(image_path).convert('L')
pred_image = np.array(pred_image_pil, 'uint8')
faces = faceCascade.detectMultiScale(pred_image)
for (x,y,w,h) in faces:
temp=np.array(pred_image[y:y+col,x:x+row]).reshape((1, -1))
X_test = pca.transform(temp)
id = clf.predict(X_test)
#a=clf.score(X_test)
if(id==1):
id="john"
elif(id==2):
id="brad"
elif(id==3):
id="scr"
elif(id==4):
id="natalie portman"
elif(id==5):
id="jennifer lawrence"
elif(id==6):
id="van diesel"
elif(id==7):
id="jennifer aniston"
elif(id==8):
id="leonardo dicaprio"
else :
id="unknown"
print(id)
cv2.imshow("Recognizing Face", pred_image[y: y + h, x: x + w])
cv2.waitKey(1000)
cv2.destroyAllWindows()
我也试过clf.score(X_test,id)
。在这种情况下,所有置信度都取值1.0
。
答案 0 :(得分:1)
来自documentation(强调我的):
<强>
decision_function(X)
强>
预测样本的置信度分数 样本的置信度得分是该样本与超平面的符号距离。
您必须在代码中将a=clf.score(X_test)
更改为a = clf.decision_function(X_test)
。
修改强>
我猜你真的希望通过检查dataset2
的图像是否被识别来“手动”验证你的模型。在这种情况下,您需要在代码中引入一些更改:
将他的句子放在外loop
循环的最开头:
nbr2 = int(os.path.split(image_path)[-1].split('.')[1])
我假设dataset2
的文件命名约定与dataset1
的文件命名约定相同,否则您必须相应地重新定义nbr2
。
不要将id
用作变量名,因为id
是Python中的保留字。您可以改为使用idx
:
idx = clf.predict(X_test)
用字典替换复合if-elif-else
语句(在for
循环之外):
names = {1: "john",
2: "brad",
3: "scr",
4: "natalie portman",
5: "jennifer lawrence",
6: "van diesel",
7: "jennifer aniston",
8: "leonardo dicaprio",
}
并将print(id)
更改为print(names.get(idx, "unknown"))
。
最后,将a=clf.score(X_test)
替换为a = clf.score(X_test, nbr2)
。