Python openface.AlignDlib() Examples

The following are 12 code examples of openface.AlignDlib(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module openface , or try the search function .
Example #1
Source File: align.py    From deep_learning_ex with MIT License 6 votes vote down vote up
def align_face(img_path, save=False):
  img_bgr = cv2.imread(img_path)
  if img_bgr is None:
    raise Exception("Unable to load image '%s'" % img_path)

  # Convert to RGB
  img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
  # Get bounding box
  bbox = align.getLargestFaceBoundingBox(img_rgb)
  if bbox is None:
    raise Exception("Unable to find a face in '%s'" % img_path)

  print "Align '%s'" % img_path
  face = align.align(IMG_DIM, img_rgb, bbox,
    landmarkIndices=openface.AlignDlib.OUTER_EYES_AND_NOSE)

  if save:
    print "Save to '%s'" % save
    cv2.imwrite(save, face)
  return face 
Example #2
Source File: face_recognizer.py    From image_recognition with MIT License 6 votes vote down vote up
def _get_representation(self, bgr_image):
        """
        Gets the vector of a face in the image
        :param bgr_image: The input image
        :return: The vector representation
        """
        rgb_image = cv2.cvtColor(bgr_image, cv2.COLOR_BGR2RGB)

        bb = self._align.getLargestFaceBoundingBox(rgb_image)
        if bb is None:
            raise Exception("Unable to find a face in image")

        aligned_face = self._align.align(96, rgb_image, bb, landmarkIndices=openface.AlignDlib.OUTER_EYES_AND_NOSE)
        if aligned_face is None:
            raise Exception("Unable to align face bb image")

        return self._net.forward(aligned_face) 
Example #3
Source File: faceaugmentation.py    From AWSLambdaFace with GNU General Public License v3.0 6 votes vote down vote up
def get_face_vector(rgbImg, align, net):
    bbs = align.getAllFaceBoundingBoxes(rgbImg)

    if len(bbs) == 0:
        raise(Exception("Unable to find a face"))
    elif len(bbs) > 1:
        raise(Exception("Found more than one face"))
    else:
        print "Found exactly one face in image (no error)"

    print('bbs', bbs)
        
    alignedFace = align.align(
        96,
        rgbImg,
        bbs[0],
        landmarkIndices=openface.AlignDlib.OUTER_EYES_AND_NOSE)

    if alignedFace is None:
        raise("Unable to align image")

    print('aligned face shape', alignedFace.shape)
    rep = net.forward(alignedFace)

    return rep 
Example #4
Source File: faceknn.py    From AWSLambdaFace with GNU General Public License v3.0 6 votes vote down vote up
def is_face_present(rgbImg, align, net, knn):
    # get face vectors
    bbs = align.getAllFaceBoundingBoxes(rgbImg)
    face_vectors = []
    for bb in bbs:
        alignedFace = align.align(
            96,
            rgbImg,
            bb,
            landmarkIndices=openface.AlignDlib.OUTER_EYES_AND_NOSE)

        if alignedFace is None:
            raise("Unable to align image")

        rep = net.forward(alignedFace)
        face_vectors.append(rep)

    # classify the faces in the image
    for face_vector in face_vectors:
        face_prediction = int(knn.predict(face_vector)[0])
        if( face_prediction == 0 ):
            return True

    return False 
Example #5
Source File: compare_similarity.py    From WannaPark with GNU General Public License v3.0 5 votes vote down vote up
def get_face(filename):
    # Create a HOG face detector using the built-in dlib class
    predictor_model = "shape_predictor_68_face_landmarks.dat"
    
    face_detector = dlib.get_frontal_face_detector()
    face_pose_predictor = dlib.shape_predictor(predictor_model)
    face_aligner = openface.AlignDlib(predictor_model)

    win = dlib.image_window()

    # Load the image into an array
    image = io.imread(filename)

    # Run the HOG face detector on the image data.
    # The result will be the bounding boxes of the faces in our image.
    detected_faces = face_detector(image, 1)

    # Open a window on the desktop showing the image
    win.set_image(image)

    # Loop through each face we found in the image
    for i, face_rect in enumerate(detected_faces):
        # Detected faces are returned as an object with the coordinates 
        # of the top, left, right and bottom edges
        face1 = image[face_rect.top():face_rect.bottom(), face_rect.left():face_rect.right()]
        # Draw a box around each face we found
        win.add_overlay(face_rect)
        
        # Get the the face's pose
        pose_landmarks = face_pose_predictor(image, face_rect)
        alignedFace = face_aligner.align(534, image, face_rect, landmarkIndices=openface.AlignDlib.OUTER_EYES_AND_NOSE)

        # Draw the face landmarks on the screen.
        win.add_overlay(pose_landmarks)
    return face1, alignedFace
#---------------------------------------------------------------------------------------- 
Example #6
Source File: secure_camera.py    From WannaPark with GNU General Public License v3.0 5 votes vote down vote up
def get_face(filename):
	image = io.imread(filename)

	# Run the HOG face detector on the image data.
	# The result will be the bounding boxes of the faces in our image.
	detected_faces = face_detector(image, 1)

	print("I found {} faces in the file {}".format(len(detected_faces), filename))

	# Open a window on the desktop showing the image
	win.set_image(image)

	# Loop through each face we found in the image
	for i, face_rect in enumerate(detected_faces):
		# Detected faces are returned as an object with the coordinates 
		# of the top, left, right and bottom edges
		print("- Face #{} found at Left: {} Top: {} Right: {} Bottom: {}".format(i, face_rect.left(), face_rect.top(), face_rect.right(), face_rect.bottom()))
		face1 = image[face_rect.top():face_rect.bottom(), face_rect.left():face_rect.right()]
		# Draw a box around each face we found
		win.add_overlay(face_rect)
		
		# Get the the face's pose
		pose_landmarks = face_pose_predictor(image, face_rect)
		alignedFace = face_aligner.align(256, image, face_rect, landmarkIndices=openface.AlignDlib.OUTER_EYES_AND_NOSE)

		# Draw the face landmarks on the screen.
		# win.add_overlay(pose_landmarks)
		
	return alignedFace 
Example #7
Source File: face_recognizer.py    From image_recognition with MIT License 5 votes vote down vote up
def __init__(self, align_path, net_path):
        # Init align and net
        """
        Dlib / Openface Face recognizer
        :param align_path: Dlib align path
        :param net_path: Openface neural network path
        """
        self._align = openface.AlignDlib(os.path.expanduser(align_path))
        self._net = openface.TorchNeuralNet(os.path.expanduser(net_path), imgDim=96, cuda=False)
        self._face_detector = dlib.get_frontal_face_detector()
        self._trained_faces = [] 
Example #8
Source File: util.py    From openface_mass_compare with Apache License 2.0 5 votes vote down vote up
def getRep(bgrImg, align=align, net=net):
    rgbImg = cv2.cvtColor(bgrImg, cv2.COLOR_BGR2RGB)
    bb = align.getLargestFaceBoundingBox(rgbImg)
    if bb is None:
        raise Exception("Unable to find a face")
    alignedFace = align.align(96, rgbImg, bb, landmarkIndices=openface.AlignDlib.OUTER_EYES_AND_NOSE)
    if alignedFace is None:
        raise Exception("Unable to align image")
    rep = net.forward(alignedFace)
    return rep 
Example #9
Source File: util.py    From openface_mass_compare with Apache License 2.0 5 votes vote down vote up
def getPeople(bgrImg, align=align, net=net):
    faces = []
    rgbImg = cv2.cvtColor(bgrImg, cv2.COLOR_BGR2RGB)
    bb = align.getAllFaceBoundingBoxes(rgbImg)
    if bb is None:
        raise Exception("Unable to find a face")
    for face in bb:
        alignedFace = align.align(96, rgbImg, face, landmarkIndices=openface.AlignDlib.OUTER_EYES_AND_NOSE)
        if alignedFace is None:
            print("Unable to align image")
            continue
        if not alignedFace is None:
            rep = net.forward(alignedFace)
            best = 4
            bestUid = "unknown"
            for i in reps.keys():
                if type(reps[i]) is not list:
                    reps[i] = [reps[i]]
                for r in reps[i]:
                    d = rep - r
                    dot = np.dot(d,d)
                    if dot < best:
                        best = dot
                        bestUid = i
            faces.append({
              "face_rectangle": {
                "left": face.left(),
                "top": face.top(),
                "width": face.width(),
                "height": face.height()
              },
              "uid": bestUid,
              "confidence": 1 - best/4,
              "data": data_dict.get(bestUid)
            })
    return faces 
Example #10
Source File: head_detection.py    From HumanRecognition with MIT License 5 votes vote down vote up
def head_detection_top(photos, dlibFacePredictor, f_head_annotate):
    align = openface.AlignDlib(dlibFacePredictor)
    head_total = 0
    for photo in photos:
        image = skimage.io.imread(photo.file_path)
        head_index = 0

        for head_id in photo.human_detections:
            front_head = check_front_head(photo, image, head_id, align, head_index)
            f_head_annotate.write(str(front_head) + '\n')
            head_total += 1
            head_index += 1 
Example #11
Source File: head_extraction.py    From HumanRecognition with MIT License 5 votes vote down vote up
def head_extraction_top(photos, dlibFacePredictor, imgDim, head_dir):
	align = openface.AlignDlib(dlibFacePredictor)


	for photo in photos:
		image = skimage.io.imread(photo.file_path)
		head_index = 0
		for head_id in photo.human_detections:
			align_head(photo, image, head_id, align, head_index, imgDim, head_dir)
			head_index += 1 
Example #12
Source File: head_extraction.py    From HumanRecognition with MIT License 4 votes vote down vote up
def align_head(photo, image, head_id, align, head_index, imgDim, head_dir):
	#crop head position
	xmin = int(head_id.head_bbox[0])
	ymin = int(head_id.head_bbox[1])
	width = int(head_id.head_bbox[2])
	height = int(head_id.head_bbox[3])

	#crop to square/out of boundary
	if xmin < 0:
		x_left = 0
	else:
		x_left = xmin

	if ymin < 0:
		y_up = 0
	else:
		y_up = ymin


	if (xmin+width) > image.shape[1]:
		x_right = image.shape[1]
	else:
		x_right = xmin+width

	if (ymin+height) > image.shape[0]:
		y_down = image.shape[0]
	else:
		y_down = ymin+height

	new_width = x_right - x_left
	new_height = y_down - y_up

	if new_width > new_height:
		length = new_height
	else:
		length = new_width

	new_x_left = (x_left+x_right)/2  - length/2
	new_x_right = (x_left+x_right)/2  + length/2	
	new_y_up = (y_up+y_down)/2  - length/2
	new_y_down = (y_up+y_down)/2  + length/2	

	if (new_y_up >= new_y_down) or (new_x_left >= new_x_right):
		return

	#save head image
	if config.save_head_image == True:
		head_image = image[new_y_up:new_y_down, new_x_left:new_x_right]
		head_file = os.path.join(head_dir,  photo.album_id + '_' + photo.photo_id + '_head_' + str(head_index) + '.jpg')
		skimage.io.imsave(head_file, head_image)

	#save aligned head
	if config.save_aligned_head_image == True:
		dlib_bbox = dlib.rectangle(left=new_x_left, top=new_y_up, right=new_x_right, bottom=new_y_down)
		alignedFace = align.align(imgDim, image, dlib_bbox, landmarkIndices=openface.AlignDlib.INNER_EYES_AND_BOTTOM_LIP)
		if image.ndim == 3:
			alignedFace = cv2.cvtColor(alignedFace, cv2.COLOR_RGB2BGR)
		align_head_file = os.path.join(head_dir,  photo.album_id + '_' + photo.photo_id + '_aligned_head_' + str(head_index) + '.jpg')
		cv2.imwrite(align_head_file, alignedFace)