FACE MESH USING PYTHON

    The face mesh is a face geometry solution that estimates 468 3D face landmarks in real-time even on mobile devices.
The output of the following code will be like this,

Packages to install : 

  1. pip install opencv-python.
  2. pip install mediapipe.

Program : 

import cv2
import mediapipe as mp
import time

# Creating objects for face mesh
mpFaceMesh = mp.solutions.face_mesh
faceMesh = mpFaceMesh.FaceMesh(max_num_faces=2)
# Creating objects for drawing utilities
mpDraw = mp.solutions.drawing_utils
drawSpecs = mpDraw.DrawingSpec(thickness=1, circle_radius=1, color=(0, 255, 0))

# Capturing video from the web cam
video = cv2.VideoCapture(0)

previousTime = 0
currentTime = 0

while True:
success, img = video.read()
# Converting BGR image to RGB image
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

# Getting landmark points and storing in multiFaceLandmarks variable
result = faceMesh.process(imgRGB)
multiFaceLanmarks = result.multi_face_landmarks

# This 'if' block will run when it's having the facelandmark points
if multiFaceLanmarks:
for faceLandmarks in multiFaceLanmarks:
mpDraw.draw_landmarks(img, faceLandmarks, mpFaceMesh.FACEMESH_CONTOURS, drawSpecs, drawSpecs)

for index, faceLandmark in enumerate(faceLandmarks.landmark):
imgh, imgw, imgc = img.shape
x, y = int(faceLandmark.x * imgw), int(faceLandmark.y * imgh)

# calculating frames per second
currentTime = time.time()
fps = 1 / (currentTime - previousTime)
previousTime = currentTime

# This will show the fps value on screen
cv2.putText(img, f"FPS: {int(fps)}", (30, 50), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 2)

cv2.imshow("FACE MESH", img)
key = cv2.waitKey(1)
if key == 13:
exit()

Comments

Popular posts from this blog

MOTION DETECTION AND TRACKING USING OPENCV AND PYTHON

BASIC HAND TRACKING USING PYTHON

FACE DETECTION USING MEDIAPIPE AND PYTHON