def classify_image(image): image_pil = image.convert('L') image = numpy.array(image_pil, 'uint8') faces = frontal_face_cascade.detectMultiScale(image) faces = classification_utils.group_faces(faces) (x, y, w, h) = faces[0] predicted_label_code, confidence = recognizer.predict(image[y: y+h, x: x+w]) print(predicted_label_code) print("Classified:", classification_utils.lookup_label(predicted_label_code), "with confidence", confidence) print("") return classification_utils.lookup_label(predicted_label_code)
def check_trainer_accuracy(): testRecognizer = cv2.createLBPHFaceRecognizer(neighbors=5) (train_images, train_labels) = get_images_and_labels(TRAINING_DIR) test_images = [] test_labels = [] for i in range(0, len(train_images)/10): index = random.randrange(len(train_images)-1) test_images.append(train_images[index]) test_labels.append(train_labels[index]) del train_images[index] del train_labels[index] train_label_codes = [] for label in train_labels: train_label_codes.append(classification_utils.lookup_code(label)) testRecognizer.train(train_images, numpy.array(train_label_codes)) testRecognizer.save("recognizer.dat") num_correct = 0; for i in range(0, len(test_images)): predicted_label_code, confidence = testRecognizer.predict(test_images[i]) actual_label = test_labels[i] predicted_label = classification_utils.lookup_label(predicted_label_code) if predicted_label == actual_label: num_correct += 1 print("Correctly predicted with confidence", confidence) else: print("Incorrectly recognized", actual_label, "as", predicted_label) print("Predicted with", float(num_correct)/float(len(test_images)), "accuracy")
def test_classification(path): image_path = os.path.join(path) image_pil = Image.open(image_path).convert('L') image = numpy.array(image_pil, 'uint8') face = frontal_face_cascade.detectMultiScale(image) (x, y, w, h) = face[0] predicted_label_code, confidence = recognizer.predict(image[y: y+h, x: x+w]) actual_label = os.path.split(image_path)[1].split('-')[0] predicted_label = classification_utils.lookup_label(predicted_label_code) if predicted_label == actual_label: print("Correctly predicted with confidence", confidence) else: print("Incorrectly recognized as", predicted_label)
def classify_from_webcam(): while True: video_capture = cv2.VideoCapture(0) ret, frame = video_capture.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = frontal_face_cascade.detectMultiScale(gray) faces = classification_utils.group_faces(faces) for (x, y, w, h) in faces: cv2.rectangle(gray , (x, y), (x+w, y+h), (0, 255, 0), 2) predicted_label_code, confidence = recognizer.predict(gray[y: y+h, x: x+w]) print("******************************", classification_utils.lookup_label(predicted_label_code)) cv2.imshow('ImageWindow', gray) cv2.waitKey(1) time.sleep(1)