Exemple #1
0
def process(picture_path):
    frame = cv2.imread(picture_path, cv2.CV_LOAD_IMAGE_GRAYSCALE)
    faces_raw = detect_feature(frame, FACE_CASCADES)

    faces = []
    for (x, y, w, h) in faces_raw:
        image = frame.copy()

        # Get the right offsets for top and bottom
        offset_y_top, offset_y_bottom = get_offset(y, h, image.shape[0])
        y -= offset_y_top
        h += offset_y_bottom

        # Crop to the face Region of Interest
        face_roi = image[y:y + h, x:x + w]

        # Try to detect eyes in this face
        face_eyes = detect_feature(face_roi, EYES_CASCADES)

        # If we find both eyes, calculate the center and rotate the frame to align the eyes
        if face_eyes.size == 8:
            eyes = [get_center(face_eyes[0]), get_center(face_eyes[1])]
            sort_eyes(eyes)
            eyes = to_frame_coordinates(eyes, x, y)
            image = rotate(image, get_angle_from_eyes(eyes[0], eyes[1]),
                           eyes[0])

        # Crop to the face
        image = image[y:y + h, x:x + w]

        # Rezise the face to a standard size
        image = cv2.resize(image, (FACE_WIDTH, FACE_HEIGHT),
                           interpolation=cv2.INTER_CUBIC)

        # Apply the Tan Triggs transformation
        image = tantriggs.tantriggs(image)

        debug_save(image)

        faces.append(image)

    return faces
Exemple #2
0
def process(picture_path):
    frame = cv2.imread(picture_path, cv2.CV_LOAD_IMAGE_GRAYSCALE)
    faces_raw = detect_feature(frame, FACE_CASCADES)

    faces = []
    for (x, y, w, h) in faces_raw:
        image = frame.copy()

        # Get the right offsets for top and bottom
        offset_y_top, offset_y_bottom = get_offset(y, h, image.shape[0])
        y -= offset_y_top
        h += offset_y_bottom

        # Crop to the face Region of Interest
        face_roi = image[y:y + h, x:x + w]

        # Try to detect eyes in this face
        face_eyes = detect_feature(face_roi, EYES_CASCADES)

        # If we find both eyes, calculate the center and rotate the frame to align the eyes
        if face_eyes.size == 8:
            eyes = [get_center(face_eyes[0]), get_center(face_eyes[1])]
            sort_eyes(eyes)
            eyes = to_frame_coordinates(eyes, x, y)
            image = rotate(image, get_angle_from_eyes(eyes[0], eyes[1]), eyes[0])

        # Crop to the face
        image = image[y:y + h, x:x + w]

        # Rezise the face to a standard size
        image = cv2.resize(image, (FACE_WIDTH, FACE_HEIGHT), interpolation=cv2.INTER_CUBIC)

        # Apply the Tan Triggs transformation
        image = tantriggs.tantriggs(image)

        debug_save(image)

        faces.append(image)

    return faces
Exemple #3
0
def process(frame):
    # frame = cv2.imread(picture_path, cv2.CV_LOAD_IMAGE_GRAYSCALE)
    faces_raw = detect_feature(frame, FACE_CASCADES)

    faces = []
    for (x, y, w, h) in faces_raw:
        image = frame.copy()

        # Crop to the face Region of Interest
        # TODO: there's an error when y - offset goes off limits
        face_roi = image[y - FACE_OFFSET_Y:y + h + FACE_OFFSET_Y, x:x + w]

        # Try to detect eyes in this face
        face_eyes = detect_feature(face_roi, EYES_CASCADES)

        # If we find both eyes, calculate the center and rotate the frame to align the eyes
        if face_eyes.size == 8:
            eyes = [get_center(face_eyes[0]), get_center(face_eyes[1])]
            sort_eyes(eyes)

            eyes = to_frame_coordinates(eyes, x, y)
            image = rotate(image, get_angle_from_eyes(eyes[0], eyes[1]),
                           eyes[0])

        # Crop to the face
        image = image[y - FACE_OFFSET_Y:y + h + FACE_OFFSET_Y, x:x + w]

        # Rezise the face to a standard size
        image = cv2.resize(image, (FACE_WIDTH, FACE_HEIGHT),
                           interpolation=cv2.INTER_CUBIC)

        # Apply the Tan Triggs transformation
        image = tantriggs.tantriggs(image)

        debug_save(image)

        faces.append(image)

    return faces
Exemple #4
0
def process(frame):
    # frame = cv2.imread(picture_path, cv2.CV_LOAD_IMAGE_GRAYSCALE)
    faces_raw = detect_feature(frame, FACE_CASCADES)

    faces = []
    for (x, y, w, h) in faces_raw:
        image = frame.copy()

        # Crop to the face Region of Interest
        # TODO: there's an error when y - offset goes off limits
        face_roi = image[y - FACE_OFFSET_Y:y + h + FACE_OFFSET_Y, x:x + w]

        # Try to detect eyes in this face
        face_eyes = detect_feature(face_roi, EYES_CASCADES)

        # If we find both eyes, calculate the center and rotate the frame to align the eyes
        if face_eyes.size == 8:
            eyes = [get_center(face_eyes[0]), get_center(face_eyes[1])]
            sort_eyes(eyes)

            eyes = to_frame_coordinates(eyes, x, y)
            image = rotate(image, get_angle_from_eyes(eyes[0], eyes[1]), eyes[0])

        # Crop to the face
        image = image[y - FACE_OFFSET_Y:y + h + FACE_OFFSET_Y, x:x + w]

        # Rezise the face to a standard size
        image = cv2.resize(image, (FACE_WIDTH, FACE_HEIGHT), interpolation=cv2.INTER_CUBIC)

        # Apply the Tan Triggs transformation
        image = tantriggs.tantriggs(image)

        debug_save(image)

        faces.append(image)

    return faces
Exemple #5
0
def main_loop(directory):
    image_index = 0
    image = numpy.empty(0)

    # Open the default webcam
    capture = cv2.VideoCapture(0)

    while True:
        # Read a frame from the capture device and show it
        _, frame = capture.read()
        cv2.imshow(MAIN_WINDOW, frame)

        key = 0xFF & cv2.waitKey(10)

        if key == 13 or key == 10:  # Enter
            faces = detect_feature(frame, face_cascades)

            # We want just one face, because we're training the detector
            if faces.size == 4:
                x, y, w, h = faces[0]
                face_roi = frame[y:y + h, x:x + w]

                # Try to detect eyes in this face
                eyes = detect_feature(face_roi, eyes_cascades)

                # Create a window with custom attributes
                cv2.namedWindow(FACE_WINDOW, CV_WINDOW_AUTOSIZE | CV_GUI_NORMAL)

                # Initialize our state machine for the eyes
                eyes_fsm = EyesFSM.EyesFSM(face_roi.shape)

                # Setup a callback function for mouse events
                cv2.setMouseCallback(FACE_WINDOW, on_mouse_event, (eyes_fsm, face_roi))

                # Make a copy of the image to save it later
                image = frame.copy()

                # Populate the state machine with the eyes just found
                for e in eyes:
                    eyes_fsm.set_current_eye_pos(e)
                    eyes_fsm.next()

                # Update the window to show the eyes
                eyes_fsm.update_window(FACE_WINDOW, face_roi)

        elif key == ord('s') and image.size > 0 and eyes_fsm and eyes_fsm.is_valid():
            # Find a non-existing filename for the new image
            while True:
                filename = os.path.join(directory, '%s.png' % image_index)

                if not os.path.exists(filename):
                    break
                image_index += 1

            # Sort the eyes
            eyes_fsm.sort_eyes()

            # Get the eyes position relative to the frame from the face ROI
            eye_left = eyes_fsm.get_frame_coordinates(eyes_fsm.eyes[0], x, y)
            eye_right = eyes_fsm.get_frame_coordinates(eyes_fsm.eyes[1], x, y)

            image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

            # Rotate the whole frame to align eyes
            image = rotate(image, get_angle_from_eyes(eye_left, eye_right), eye_left)

            # Crop to the face
            image = image[y - FACE_OFFSET_Y: y + h + FACE_OFFSET_Y, x: x + w]

            # Rezise the face to a standard size
            image = cv2.resize(image, (260, 315), interpolation=cv2.INTER_CUBIC)

            # cv2.destroyWindow(FACE_WINDOW)
            cv2.imshow(FINAL_FACE_WINDOW, image)

            image = tantriggs.tantriggs(image)
            cv2.imshow(TANTRIGGS_WINDOW, image)

            print("Saving image to %s." % filename)
            cv2.imwrite(filename, image)

            image_index += 1
            image = numpy.empty(0)

        elif key == ord('f'):
            cv2.imwrite(os.path.join(directory, "%s%s%s%s.png" % (x, y, h, w)), frame)

        elif key == 27:  # Escape
            break