JeVoisBase  1.20
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  helper.itext('JeVois-Pro Face Landmarks Detection', 0, -1)
49 
50  # Get the next camera image at processing resolution (may block until it is captured):
51  image = inframe.getCvRGBp()
52  iw, ih = image.shape[1], image.shape[0]
53 
54  # Start measuring image processing time:
55  self.timer.start()
56 
57  # Detect face landmarks:
58  results = self.face_mesh.process(image)
59 
60  # Draw results:
61  if results.multi_face_landmarks is not None:
62  for face_landmarks in results.multi_face_landmarks:
63  self.draw_landmarks(helper, iw, ih, face_landmarks)
64  if self.use_brect:
65  brect = self.calc_bounding_rect(iw, ih, face_landmarks)
66  helper.drawRect(brect[0], brect[1], brect[2], brect[3], 0x6040ffff, True)
67 
68  # Write frames/s info from our timer:
69  fps = self.timer.stop()
70  helper.iinfo(inframe, fps, winw, winh);
71 
72  # End of frame:
73  helper.endFrame()
74 
75  # ###################################################################################################
76  def calc_bounding_rect(self, iw, ih, landmarks):
77  landmark_array = np.empty((0, 2), int)
78 
79  for _, landmark in enumerate(landmarks.landmark):
80  landmark_x = min(int(landmark.x * iw), iw - 1)
81  landmark_y = min(int(landmark.y * ih), ih - 1)
82  landmark_point = [np.array((landmark_x, landmark_y))]
83  landmark_array = np.append(landmark_array, landmark_point, axis=0)
84  x, y, w, h = cv2.boundingRect(landmark_array)
85  return [x, y, x + w, y + h]
86 
87  # ###################################################################################################
88  def draw_landmarks(self, helper, iw, ih, landmarks):
89  lpx = []
90  lpy = []
91  col = 0xff00ff00
92 
93  for index, landmark in enumerate(landmarks.landmark):
94  if landmark.visibility < 0 or landmark.presence < 0: 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 
demo.int
int
Definition: demo.py:37
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.PyFaceMesh.calc_bounding_rect
def calc_bounding_rect(self, iw, ih, landmarks)
Definition: PyFaceMesh.py:76
PyFaceMesh.PyFaceMesh.mp_face_mesh
mp_face_mesh
Definition: PyFaceMesh.py:35
PyFaceMesh.PyFaceMesh.draw_landmarks
def draw_landmarks(self, helper, iw, ih, landmarks)
Definition: PyFaceMesh.py:88
jevois::Timer