Skip to main content

A package to help you utilise landmarks in the face_landmark feature of google open source mediapipe.

Project description

py_face_landmark_helper

A package to help you utilise landmarks in the face_landmark feature of google open source mediapipe.

Installation

To install the mediapipe_helper library, you can use pip:

pip install mediapipe_helper

or

git clone https://github.com/LearningnRunning/py_face_landmark_helper

python setup.py install

Create an instance of FaceMeshProcessor

from mediapipe_helper import face_landmark_helper

# Create an instance of FaceMeshProcessor
face_mesh_processor = face_landmark_helper.FaceLandmarkProcessor()

convert_landmark

Image Path or Image Object, whatever you want.

  • face_landmarks is a list of 468 landmarks extracted from the facial image. Each landmark is represented as a tuple containing the x and y coordinates in pixels.

Using Image Path

img_path = 'image_path.jpg'

# Call the convert_landmark method to get the face landmarks
face_landmarks = face_mesh_processor.convert_landmark(img_path)
# OUTPUT: [(270, 492), (276, 453), (274, 464), (267, 400), (277, 439), (279, 418), (282, 362), (176, 338), (285, 318), (287, 297), (293, 223), (270, 497), (269, 503), (268, 507), (268, 515),  ...]

Using Image Object

import cv2

image_path = 'image_path.jpg'

# Load the image using cv2.imread()
image = cv2.imread(image_path)

# Call the convert_landmark method to get the face landmarks
face_landmarks = face_mesh_processor.convert_landmark(image)
# OUTPUT: [(270, 492), (276, 453), (274, 464), (267, 400), (277, 439), (279, 418), (282, 362), (176, 338), (285, 318), (287, 297), (293, 223), (270, 497), (269, 503), (268, 507), (268, 515),  ...]

Finding symmetric indexes

Entering index tells you the index of the landmark that is symmetrical to the center line of the face.

invert_landmark

lmk_idx = 170
lmk_invert = face_mesh_processor.invert_landmark(lmk_idx)
# OUTPUT: 395

invert_landmark_list

lmk_idx = [170, 171, 180, 190]
lmk_invert_list = face_mesh_processor.invert_landmark_list(lmk_idx)
# OUTPUT: [395, 396, 404, 414]

How to utilize indexes

# Load the image using cv2.imread()
image = cv2.imread(image_path)

# Call the convert_landmark method to get the face landmarks
face_landmarks = face_mesh_processor.convert_landmark(image)

# Use the face_landmarks list to access the coordinates of the landmark at index lmk_idx
lmk_coordinates = face_landmarks[lmk_idx]
print("Coordinates of landmark at index", lmk_idx, ":", lmk_coordinates)

# Example of inverting index and accessing corresponding landmark
lmk_invert = face_mesh_processor.invert_landmark(lmk_idx)
# Use the inverted index to access the corresponding landmark
lmk_inverted_coordinates = face_landmarks[lmk_invert]
print("Coordinates of landmark at inverted index", lmk_invert, ":", lmk_inverted_coordinates)

# Coordinates of landmark at index 170 : (189, 553)
# Coordinates of landmark at inverted index 395 : (333, 562)

Landmark list template by facial part

#  Look up template types
face_mesh_processor.mesh_template()
# OUTPUT: This is Template List, ['FACE_CONTOURS', 'FACE_FACE_OVAL', 'FACE_IRISES', 'FACE_LEFT_EYE', 'FACE_LEFT_EYEBROW', 'FACE_LEFT_IRIS', 'FACE_LIPS', 'FACE_NOSE', 'FACE_RIGHT_EYE', 'FACE_RIGHT_EYEBROW', 'FACE_RIGHT_IRIS', 'FACE_TESSELATION', 'FACE_CENTER'] 


# Usage examples: mesh_template('FACE_LIPS')
lmd_mark = face_mesh_processor.mesh_template('FACE_LIPS')
# OUTPUT : [0, 267, 269, 270, 13, 14, 17, 402, 146, 405, 409, 415, 291, 37, 39, 40, 178, 308, 181, 310, 311, 312, 185, 314, 317, 318, 61, 191, 321, 324, 78, 80, 81, 82, 84, 87, 88, 91, 95, 375]

Landmark list template by facial part

image_path = 'image_path.jpg'
# Usage examples: mesh_template('FACE_LIPS')
lmd_mark = face_mesh_processor.mesh_template('FACE_LIPS')

plot_img = face_mesh_processor.plot_landmarks(img_path, lmk_lst=lmd_mark)
# OUTPUT: array([[[165, 173, 167],[164, 174, 167],[165, 174, 167)

plot_img
plot_img

plot_landmarks's Parameters

Parameters:
image_tmp (np.ndarray): The input image on which to plot the landmarks. It should be a NumPy array representing the image.
lmk_lst (List[int], optional): A list of face landmark indices to plot. Defaults to list(range(468)).
color (Tuple[int, int, int], optional): The color to use for plotting lines and points. Defaults to (0, 255, 0) (green).
coloful_option (bool, optional): A boolean flag indicating whether to use random colors for plotting lines and points. Defaults to False.
draw_symmetrical_landmarks (bool, optional): A boolean flag indicating whether to draw symmetrical landmarks. Defaults to False.

user_color = (122, 143, 12)
plot_img_user_color = face_mesh_processor.plot_landmarks(img_path, lmk_lst=lmd_mark, color=user_color)

plot_img_colorful_sym = face_mesh_processor.plot_landmarks(img_path, lmk_lst=lmd_mark, coloful_option=True, draw_symmetrical_landmarks=True)

plot_img_user_color plot_img_user_color plot_img_colorful_sym plot_img_colorful_sym

Display the image

import matplotlib.pyplot as plt

# Assuming you have an image array named plot_img
plt.axis('off')  # Remove axes
plt.imshow(plot_img)
plt.show()  # Display the image without printing anything

from PIL import Image
Image.fromarray(plot_img)

License

mediapipe_helper is provided under the Apache License 2.0.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mediapipe_helper-0.0.5.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

mediapipe_helper-0.0.5-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

Details for the file mediapipe_helper-0.0.5.tar.gz.

File metadata

  • Download URL: mediapipe_helper-0.0.5.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.6

File hashes

Hashes for mediapipe_helper-0.0.5.tar.gz
Algorithm Hash digest
SHA256 53f555a04344f6883ff2ff2390a2ac1975ec3978caf0e2cd3d7c5fc34ff18aca
MD5 3f93d1ee21cfbbf17b575c1ccbfee9c4
BLAKE2b-256 36b76271a3293619095d9bb0f83f02a6d4c0f1b439fe473646e068d5ffa808b8

See more details on using hashes here.

File details

Details for the file mediapipe_helper-0.0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for mediapipe_helper-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 70deca9f5efecdc12f771045e73fadc055fbf5c75bffa9f85d348852023a90ae
MD5 fd6a7e4dfa801f47ea5762bd1e50420d
BLAKE2b-256 b6b1a0db2f68b2e669f2fa59b9339f20394aa7ea6ee266c09f199ac883f138fd

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page