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.4.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: mediapipe_helper-0.0.4.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.4.tar.gz
Algorithm Hash digest
SHA256 cb83e5c96b5e4df1972cf81eff5efa79a95f418d4c824a82195981ecf800ba10
MD5 23e5d386b8fab95d758b380bfd1f27fb
BLAKE2b-256 07e310d6f8610ee4d56ed8cc79d9ac6317a13f408aaa0a912da7a48405b39b8d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for mediapipe_helper-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 cf88e9652a67a66640dd1c508a305344d5d0b4c5221ab560506ddee261b88254
MD5 fb755eef409a62ec4ecea2d353b405e0
BLAKE2b-256 f22648baa0e73828f0202b74bd42f912958ffe3938f6915085fe653fd0341077

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