【发布时间】:2015-04-11 20:56:07
【问题描述】:
我是人脸识别的新手。我正在尝试借助 bytefish facerec 框架进行人脸识别。它工作正常,但结果不是很准确。因此,我想设置阈值。按照他的页面 (https://github.com/bytefish/facerec) 上的建议,我应该能够做到。但是,页面上的解释不是很清楚。所以这就是我正在做的事情。
我的分类器
def predict(self, q):
distances = []
for xi in self.X:
xi = xi.reshape(-1,1)
d = self.dist_metric(xi, q)
distances.append(d)
if len(distances) > len(self.y):
raise Exception("More distances than classes. Is your distance metric correct?")
distances = np.asarray(distances)
# Get the indices in an ascending sort order:
idx = np.argsort(distances)
# Sort the labels and distances accordingly:
sorted_y = self.y[idx]
sorted_distances = distances[idx]
# Take only the k first items:
sorted_y = sorted_y[0:self.k]
sorted_distances = sorted_distances[0:self.k]
# Make a histogram of them:
hist = dict((key,val) for key, val in enumerate(np.bincount(sorted_y)) if val)
# And get the bin with the maximum frequency:
predicted_label = max(hist.iteritems(), key=op.itemgetter(1))[0]
# A classifier should output a list with the label as first item and
# generic data behind. The k-nearest neighbor classifier outputs the
# distance of the k first items. So imagine you have a 1-NN and you
# want to perform a threshold against it, you should take the first
# item
return [predicted_label, { 'labels' : sorted_y, 'distances' : sorted_distances }]
我的模型
def predict(self, X):
q = self.feature.extract(X)
return self.classifier.predict(q)
我的 server.py 生成输出
def get_prediction(image_data):
image = preprocess_image(image_data)
prediction = model.predict(image)
predicted_label = prediction[0]
classifier_output = prediction[1]
distance = classifier_output['distances'][0]
#distance = classifier.predict(self, q)
#distance = 11
if distance > 10.0:
return "nonsense"
else:
print prediction
所以问题是我无法在这里获得距离。请帮忙
【问题讨论】:
标签: python opencv face-recognition