我创建了一个使用局部二进制模式 (LBP) cv2.face.LBPHFaceRecognizer_create(radius=2, neighbors=10, grid_x=6, grid_y=6)执行人脸识别的简单程序。
我想知道是否有办法用 FAR、FRR 和 ERR 来评估这个模型。
这是我的小程序:
PATH = glob.glob("./DATASET/Face_Recognition/CALTECH_Faces/Only_Face/*")
#model = cv2.face.LBPHFaceRecognizer_create() #Local Binary Patterns model
model = cv2.face.LBPHFaceRecognizer_create(radius=2, neighbors=10, grid_x=6, grid_y=6) #best r=2 n=10
rec = face_detect.haar()
img_h, img_w = 350, 500 # webcam window size
face_cascade = cv2.CascadeClassifier("./haarcascade_frontalface_default.xml")
cap = cv2.VideoCapture(0)
final_str = ""
face_db = [] #I create a list containing all the PATHs to the single images
for folder in PATH:
for f in glob.glob(folder+'/*.jpg'):
face_db.append(f)
faces = [] #array that will contain the individual faces
for img_path in face_db:
img = cv2.imread(img_path)
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # LBP works only on B&W images
faces.append(gray_img)
ids = np.array([i for i in range(0, len(faces))]) # np array np with indices from 0 to total number of images
model.train(faces, ids) #addestro il modello
print("Create the model...\n")
model.save("./DATASET/Face_Recognition/CALTECH_Faces/Only_Face/1_LBPH_model.yml")
print("The model is ready. Accendo la webcam\n")
model.read("./DATASET/Face_Recognition/CALTECH_Faces/Only_Face/1_LBPH_model.yml")
#histograms = model.getHistograms()
# apply HAAR
while(cap.isOpened()): # I go through it as long as the webcam is on
_, frame = cap.read()
frame = cv2.resize(frame, (img_w, img_h))
results = rec.process_frame(frame)
for res in results:
img = frame[res[1]:res[1]+res[3], res[0]:res[0]+res[2]]
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
label, confidence = model.predict(gray)
# since label contains all the path of the image for example
# "./DATASET/Face_Recognition/CALTECH_Faces/Only_Face/Andrea/Andrea_1.jpg" I just take the name of the folder, to do it
# I take from the 51st character of label (corresponds to the first letter of the folder, before it was Andrea) then
# using extract_name, take the rest of the name up to the character before the "/".
who_is = str(face_db[label])
who_is = who_is[51:]
final_str = extract_name(who_is)
# the perfect match occurs with a confidence lower than 100
final_str = final_str if confidence<100 else "Unknow"
color = (0,255,0) if confidence<100 else (0,0,255)
label_to_show = final_str + " Conf.: " + str(round(confidence))
rec.print_rectangle(frame,res,label_to_show, color)
cv2.imshow("Biometrics", frame)
k = cv2.waitKey(1)
if(k%256 ==ord("q")):
break
#print("Soggetto: ", final_str)
#print("Confidence: ", confidence)
def extract_name(who):
final_str = ""
for i in who:
if(i != "/"):
final_str += i
else:
return final_str