JeVoisBase  1.16
JeVois Smart Embedded Machine Vision Toolkit Base Modules
Share this page:
PyFaceMesh.py
Go to the documentation of this file.
1 import pyjevois
2 if pyjevois.pro: import libjevoispro as jevois
3 else: import libjevois as jevois
4 import cv2
5 import numpy as np
6 import mediapipe as mp
7 
8 ## Face mesh detection using MediaPipe
9 #
10 # Detect face landmarks using MediaPipe in Python
11 #
12 # This code is derived from sample_facemesh.py at https://github.com/Kazuhito00/mediapipe-python-sample
13 #
14 # @author Laurent Itti
15 #
16 # @videomapping JVUI 0 0 30.0 CropScale=RGB24@512x288:YUYV 1920 1080 30.0 JeVois PyFaceMesh
17 # @email itti\@usc.edu
18 # @address University of Southern California, HNB-07A, 3641 Watt Way, Los Angeles, CA 90089-2520, USA
19 # @copyright Copyright (C) 2021 by Laurent Itti, iLab and the University of Southern California
20 # @mainurl http://jevois.org
21 # @supporturl http://jevois.org/doc
22 # @otherurl http://iLab.usc.edu
23 # @license GPL v3
24 # @distribution Unrestricted
25 # @restrictions None
26 # @ingroup modules
27 class PyFaceMesh:
28  # ###################################################################################################
29  ## Constructor
30  def __init__(self):
31  # Instantiate a JeVois Timer to measure our processing framerate:
32  self.timer = jevois.Timer("mesh", 100, jevois.LOG_INFO)
33 
34  # Instantiate mediapipe face mesh:
35  self.mp_face_mesh = mp.solutions.face_mesh
36  self.face_mesh = self.mp_face_mesh.FaceMesh(max_num_faces = 1, min_detection_confidence = 0.7,
37  min_tracking_confidence = 0.5)
38  self.use_brect = True; # true to show a bounding rectangle
39 
40  # ###################################################################################################
41  ## Process function with GUI output
42  def processGUI(self, inframe, helper):
43  # Start a new display frame, gets its size and also whether mouse/keyboard are idle:
44  idle, winw, winh = helper.startFrame()
45 
46  # Draw full-resolution input frame from camera:
47  x, y, w, h = helper.drawInputFrame("c", inframe, False, False)
48 
49  # Get the next camera image at processing resolution (may block until it is captured):
50  image = inframe.getCvRGBp()
51  iw, ih = image.shape[1], image.shape[0]
52 
53  # Start measuring image processing time:
54  self.timer.start()
55 
56  # Detect face landmarks:
57  results = self.face_mesh.process(image)
58 
59  # Draw results:
60  if results.multi_face_landmarks is not None:
61  for face_landmarks in results.multi_face_landmarks:
62  draw_landmarks(helper, iw, ih, face_landmarks)
63  if self.use_brect:
64  brect = calc_bounding_rect(iw, ih, face_landmarks)
65  helper.drawRect(brect[0], brect[1], brect[2], brect[3], 0x6040ffff, True)
66 
67  # Write frames/s info from our timer:
68  fps = self.timer.stop()
69  helper.iinfo(inframe, fps, winw, winh);
70 
71  # End of frame:
72  helper.endFrame()
73 
74 # ###################################################################################################
75 def calc_bounding_rect(iw, ih, landmarks):
76  landmark_array = np.empty((0, 2), int)
77 
78  for _, landmark in enumerate(landmarks.landmark):
79  landmark_x = min(int(landmark.x * iw), iw - 1)
80  landmark_y = min(int(landmark.y * ih), ih - 1)
81  landmark_point = [np.array((landmark_x, landmark_y))]
82  landmark_array = np.append(landmark_array, landmark_point, axis=0)
83  x, y, w, h = cv2.boundingRect(landmark_array)
84  return [x, y, x + w, y + h]
85 
86 # ###################################################################################################
87 def draw_landmarks(helper, iw, ih, landmarks):
88  lpx = []
89  lpy = []
90  col = 0xff00ff00
91 
92  for index, landmark in enumerate(landmarks.landmark):
93  if landmark.visibility < 0 or landmark.presence < 0:
94  continue
95 
96  landmark_x = min(int(landmark.x * iw), iw - 1)
97  landmark_y = min(int(landmark.y * ih), ih - 1)
98  lpx.append(landmark_x)
99  lpy.append(landmark_y)
100 
101  helper.drawCircle(landmark_x, landmark_y, 1, col, False)
102 
103  if len(lpx) > 0:
104  helper.drawLine(lpx[55], lpy[55], lpx[65], lpy[65], col)
105  helper.drawLine(lpx[65], lpy[65], lpx[52], lpy[52], col)
106  helper.drawLine(lpx[52], lpy[52], lpx[53], lpy[53], col)
107  helper.drawLine(lpx[53], lpy[53], lpx[46], lpy[46], col)
108 
109  helper.drawLine(lpx[285], lpy[285], lpx[295], lpy[295], col)
110  helper.drawLine(lpx[295], lpy[295], lpx[282], lpy[282], col)
111  helper.drawLine(lpx[282], lpy[282], lpx[283], lpy[283], col)
112  helper.drawLine(lpx[283], lpy[283], lpx[276], lpy[276], col)
113 
114  helper.drawLine(lpx[133], lpy[133], lpx[173], lpy[173], col)
115  helper.drawLine(lpx[173], lpy[173], lpx[157], lpy[157], col)
116  helper.drawLine(lpx[157], lpy[157], lpx[158], lpy[158], col)
117  helper.drawLine(lpx[158], lpy[158], lpx[159], lpy[159], col)
118  helper.drawLine(lpx[159], lpy[159], lpx[160], lpy[160], col)
119  helper.drawLine(lpx[160], lpy[160], lpx[161], lpy[161], col)
120  helper.drawLine(lpx[161], lpy[161], lpx[246], lpy[246], col)
121 
122  helper.drawLine(lpx[246], lpy[246], lpx[163], lpy[163], col)
123  helper.drawLine(lpx[163], lpy[163], lpx[144], lpy[144], col)
124  helper.drawLine(lpx[144], lpy[144], lpx[145], lpy[145], col)
125  helper.drawLine(lpx[145], lpy[145], lpx[153], lpy[153], col)
126  helper.drawLine(lpx[153], lpy[153], lpx[154], lpy[154], col)
127  helper.drawLine(lpx[154], lpy[154], lpx[155], lpy[155], col)
128  helper.drawLine(lpx[155], lpy[155], lpx[133], lpy[133], col)
129 
130  helper.drawLine(lpx[362], lpy[362], lpx[398], lpy[398], col)
131  helper.drawLine(lpx[398], lpy[398], lpx[384], lpy[384], col)
132  helper.drawLine(lpx[384], lpy[384], lpx[385], lpy[385], col)
133  helper.drawLine(lpx[385], lpy[385], lpx[386], lpy[386], col)
134  helper.drawLine(lpx[386], lpy[386], lpx[387], lpy[387], col)
135  helper.drawLine(lpx[387], lpy[387], lpx[388], lpy[388], col)
136  helper.drawLine(lpx[388], lpy[388], lpx[466], lpy[466], col)
137 
138  helper.drawLine(lpx[466], lpy[466], lpx[390], lpy[390], col)
139  helper.drawLine(lpx[390], lpy[390], lpx[373], lpy[373], col)
140  helper.drawLine(lpx[373], lpy[373], lpx[374], lpy[374], col)
141  helper.drawLine(lpx[374], lpy[374], lpx[380], lpy[380], col)
142  helper.drawLine(lpx[380], lpy[380], lpx[381], lpy[381], col)
143  helper.drawLine(lpx[381], lpy[381], lpx[382], lpy[382], col)
144  helper.drawLine(lpx[382], lpy[382], lpx[362], lpy[362], col)
145 
146  helper.drawLine(lpx[308], lpy[308], lpx[415], lpy[415], col)
147  helper.drawLine(lpx[415], lpy[415], lpx[310], lpy[310], col)
148  helper.drawLine(lpx[310], lpy[310], lpx[311], lpy[311], col)
149  helper.drawLine(lpx[311], lpy[311], lpx[312], lpy[312], col)
150  helper.drawLine(lpx[312], lpy[312], lpx[13], lpy[13], col)
151  helper.drawLine(lpx[13], lpy[13], lpx[82], lpy[82], col)
152  helper.drawLine(lpx[82], lpy[82], lpx[81], lpy[81], col)
153  helper.drawLine(lpx[81], lpy[81], lpx[80], lpy[80], col)
154  helper.drawLine(lpx[80], lpy[80], lpx[191], lpy[191], col)
155  helper.drawLine(lpx[191], lpy[191], lpx[78], lpy[78], col)
156 
157  helper.drawLine(lpx[78], lpy[78], lpx[95], lpy[95], col)
158  helper.drawLine(lpx[95], lpy[95], lpx[88], lpy[88], col)
159  helper.drawLine(lpx[88], lpy[88], lpx[178], lpy[178], col)
160  helper.drawLine(lpx[178], lpy[178], lpx[87], lpy[87], col)
161  helper.drawLine(lpx[87], lpy[87], lpx[14], lpy[14], col)
162  helper.drawLine(lpx[14], lpy[14], lpx[317], lpy[317], col)
163  helper.drawLine(lpx[317], lpy[317], lpx[402], lpy[402], col)
164  helper.drawLine(lpx[402], lpy[402], lpx[318], lpy[318], col)
165  helper.drawLine(lpx[318], lpy[318], lpx[324], lpy[324], col)
166  helper.drawLine(lpx[324], lpy[324], lpx[308], lpy[308], col)
167 
PyFaceMesh.draw_landmarks
def draw_landmarks(helper, iw, ih, landmarks)
Definition: PyFaceMesh.py:87
PyFaceMesh.PyFaceMesh.face_mesh
face_mesh
Definition: PyFaceMesh.py:36
PyFaceMesh.PyFaceMesh.timer
timer
Definition: PyFaceMesh.py:32
PyFaceMesh.PyFaceMesh.__init__
def __init__(self)
Constructor.
Definition: PyFaceMesh.py:30
PyFaceMesh.PyFaceMesh
Face mesh detection using MediaPipe.
Definition: PyFaceMesh.py:27
PyFaceMesh.PyFaceMesh.use_brect
use_brect
Definition: PyFaceMesh.py:38
PyFaceMesh.PyFaceMesh.processGUI
def processGUI(self, inframe, helper)
Process function with GUI output.
Definition: PyFaceMesh.py:42
PyFaceMesh.calc_bounding_rect
def calc_bounding_rect(iw, ih, landmarks)
Definition: PyFaceMesh.py:75
PyFaceMesh.PyFaceMesh.mp_face_mesh
mp_face_mesh
Definition: PyFaceMesh.py:35
jevois::Timer