Reputation: 7
I want to get the confidence but when I try clf.score(X_test)
I get the following error:
TypeError: score() takes at least 3 arguments (2 given)
This is my code:
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()
I also tried clf.score(X_test,id)
. In this case all confidence takes the value 1.0
.
Upvotes: 0
Views: 1866
Reputation: 13723
From the documentation (emphasis mine):
decision_function(X)
Predict confidence scores for samples.
The confidence score for a sample is the signed distance of that sample to the hyperplane.
You have to change a=clf.score(X_test)
to a = clf.decision_function(X_test)
in your code.
EDIT
I'm guessing you actually wish to "manually" validate your model by checking whether dataset2
's images are recognized or not. In that case you need to introduce a number of changes in your code:
Put his sentence at the very beginning of the outer loop
loop:
nbr2 = int(os.path.split(image_path)[-1].split('.')[1])
I'm assuming that the file naming conventions for dataset2
are the same than those for dataset1
, otherwise you would have to redefine nbr2
accordingly.
Don't use id
as a variable name since id
is a reserved word in Python. You could use idx
instead:
idx = clf.predict(X_test)
Replace the compound if-elif-else
statement by a dictionary (outside the for
loop):
names = {1: "john",
2: "brad",
3: "scr",
4: "natalie portman",
5: "jennifer lawrence",
6: "van diesel",
7: "jennifer aniston",
8: "leonardo dicaprio",
}
and change print(id)
to print(names.get(idx, "unknown"))
.
And last, replace a=clf.score(X_test)
by a = clf.score(X_test, nbr2)
.
Upvotes: 1