Example #1
0
def get_images_and_labels(path):

    # put training images in 'path' directory
    image_dirs = [d for d in os.listdir(path)]

    print image_dirs

    # array of face images
    images = []

    # label assigned to image
    labels = []

    for dir in image_dirs:
        if not dir.startswith('.'):
            image_paths = [os.path.join(path + "/" + dir, f) for f in filter( lambda f: not f.startswith('.'), os.listdir(path + "/" + dir))]

            for image_path in image_paths:
                image_pil = Image.open(image_path).convert('L')
                image = numpy.array(image_pil, 'uint8')

                # get label of image from directory name
                label = ':' + dir + ':'

                faces = frontal_face_cascade.detectMultiScale(image, minSize=(100,100))

                faces = classification_utils.group_faces(faces)

                for (x, y, w, h) in faces:
                    images.append(image[y: y+h, x: x+w])
                    labels.append(label)

    return images, labels
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 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)