@@ -132,23 +132,45 @@ def on_event(self, event):
132132 if G .K_F3 == event .key :
133133 self ._predictorMaskRightEye = not self ._predictorMaskRightEye
134134 return
135+
136+ def _updateEyesImage (self , tracked ):
137+ if self ._eyesSurface is None : return
138+
139+ def drawPoints (img , points , rect , color = Colors .RED ):
140+ A = rect [0 ]
141+ rectDim = rect [1 ] - A
142+ # remove points outside of the rect
143+ points = (points - A ) / rectDim
144+ mask = np .logical_and (0.0 <= points , points <= 1.0 )
145+ mask = np .all (mask , axis = - 1 )
146+ points = points [mask ]
147+
148+ img = np .repeat (img [..., None ], 3 , axis = 2 ) # make RGB
149+ hw = np .array (img .shape [:2 ][::- 1 ])
150+ points = np .multiply (points , hw [None ]).astype (np .int32 )
151+ color = np .array (color , np .uint8 )
152+ for p in points :
153+ img [p [1 ], p [0 ]] = color
154+ return img
155+
156+ eyes = np .concatenate ([
157+ drawPoints (tracked ['left eye' ], tracked ['face points' ], tracked ['left eye area' ]),
158+ drawPoints (tracked ['right eye' ], tracked ['face points' ], tracked ['right eye area' ])
159+ ], axis = 1 )
160+ numpyToSurfaceBind (eyes , self ._eyesSurface )
161+ return
135162
136163 def on_tick (self , deltaT ):
137164 lastTracked = None
138165 tracked = self ._tracker .track ()
139166 if not (tracked is None ):
140167 self ._currentMode .on_sample (tracked )
168+ self ._faceMesh = tracked ['face points' ].copy ()
141169
142170 if not (self ._cameraView is None ):
143171 numpyToSurfaceBind (tracked ['raw' ][..., ::- 1 ], self ._cameraSurface )
144172
145- if self ._eyesView is not None :
146- eyes = np .concatenate ([tracked ['left eye' ], tracked ['right eye' ]], axis = 1 )
147- numpyToSurfaceBind (eyes , self ._eyesSurface )
148-
149- if self ._showFaceMesh :
150- self ._faceMesh = tracked ['face points' ].copy ()
151-
173+ self ._updateEyesImage (tracked )
152174 lastTracked = {
153175 'tracked' : tracked ,
154176 'pos' : np .array (self ._smoothedPrediction , np .float32 )
@@ -218,7 +240,7 @@ def _renderInfo(self, fps):
218240
219241 self .drawText ('FPS: %.1f' % (fps , ), (5 , 95 + 25 + 25 ), Colors .BLACK )
220242
221- if self ._faceMesh is not None :
243+ if self ._showFaceMesh and not ( self . _faceMesh is None ) :
222244 scaled = np .multiply (self ._faceMesh , self .WH [None ])
223245 scaled = scaled .astype (np .int32 )
224246 for p in scaled :
0 commit comments