Face Recognition Example
Face recognition on HuskyLens 2 lets you count faces, read IDs, and track key feature coordinates in real time, like a smart attendance camera. It’s like a visual spreadsheet that logs who is in frame and where their eyes, mouth, and nose are positioned.
1.Face Recognition
Before You Start:
1.1 Face Recognition - Output Relevant Data
Under the Face Recognition function, when a face appears on the HuskyLens 2 screen, it can be detected and framed, allowing the acquisition of the total number of faces detected and related data for a specified face.
I2C Example Program:
from unihiker import GUI
from pinpong.board import Board
from dfrobot_huskylensv2 import *
u_gui=GUI()
Board().begin()
huskylens = HuskylensV2_I2C()
huskylens.knock()
huskylens.switchAlgorithm(ALGORITHM_FACE_RECOGNITION)
line1=u_gui.draw_text(text="Total faces:",x=0,y=0,font_size=20, color="#0000FF")
line2=u_gui.draw_text(text="Center face:",x=0,y=40,font_size=20, color="#0000FF")
line3=u_gui.draw_text(text="First face ID:",x=0,y=80,font_size=20, color="#0000FF")
while True:
huskylens.getResult(ALGORITHM_FACE_RECOGNITION)
if (huskylens.available(ALGORITHM_FACE_RECOGNITION)):
line1.config(text=(str("Total faces:") + str((huskylens.getCachedResultNum(ALGORITHM_FACE_RECOGNITION)))))
line2.config(text=(str("Center face:") + str((huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION).ID if huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION) else -1))))
line3.config(text=(str("First face ID:") + str(huskylens.getCachedResultByIndex(ALGORITHM_FACE_RECOGNITION, 1-1).ID if huskylens.getCachedResultByIndex(ALGORITHM_FACE_RECOGNITION, 1-1) else -1)))
UART Example Program:
The sample code provided below is for I2C mode only. If you need to use UART mode, simply modify the code by following the initialization method shown in the program below.
from unihiker import GUI
from pinpong.board import Board
from dfrobot_huskylensv2 import *
u_gui=GUI()
Board().begin()
huskylens = HuskylensV2_UART(tty_name="/dev/ttySP0", baudrate=115200)
huskylens.knock()
huskylens.switchAlgorithm(ALGORITHM_FACE_RECOGNITION)
line1=u_gui.draw_text(text="Total faces:",x=0,y=0,font_size=20, color="#0000FF")
line2=u_gui.draw_text(text="Center face:",x=0,y=40,font_size=20, color="#0000FF")
line3=u_gui.draw_text(text="First face ID:",x=0,y=80,font_size=20, color="#0000FF")
while True:
huskylens.getResult(ALGORITHM_FACE_RECOGNITION)
if (huskylens.available(ALGORITHM_FACE_RECOGNITION)):
line1.config(text=(str("Total faces:") + str((huskylens.getCachedResultNum(ALGORITHM_FACE_RECOGNITION)))))
line2.config(text=(str("Center face:") + str((huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION).ID if huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION) else -1))))
line3.config(text=(str("First face ID:") + str(huskylens.getCachedResultByIndex(ALGORITHM_FACE_RECOGNITION, 1-1).ID if huskylens.getCachedResultByIndex(ALGORITHM_FACE_RECOGNITION, 1-1) else -1)))
Click 'Run' in Mind+ and wait for the program to run.
Point the HuskyLens 2 camera at the face in the frame to learn it. For detailed instructions on how to learn faces, refer to: Face Recognition.
Once learning is complete, aim at the learned face and observe the output results on the M10 screen.
Output Result: As shown below, you can obtain the total number of faces detected in the frame, whether or not the face has been learned; you can specify the face ID near the center of the camera frame, and the ID of the first detected face (faces not learned will have an ID of 0).
1.2 Get Detailed Data of a Specific Face in the Frame
It can acquire facial feature and position data of a specified face. The readable face data includes: Face ID, Face Name, Width, Height, X and Y coordinates of the face's center point, X/Y coordinates of the left/right eye, X/Y coordinates of the left/right corner of the mouth, and X/Y coordinates of the nose.
The example program below shows how to get the facial feature data of the face closest to the center of the camera frame. This data can also be obtained for unlearned faces.
from unihiker import GUI
from pinpong.board import Board
from dfrobot_huskylensv2 import *
u_gui=GUI()
Board().begin()
huskylens = HuskylensV2_I2C()
huskylens.knock()
huskylens.switchAlgorithm(ALGORITHM_FACE_RECOGNITION)
line1=u_gui.draw_text(text="Center face ID:",x=0,y=0,font_size=15, color="#0000FF")
line2=u_gui.draw_text(text="Leye coords:",x=0,y=40,font_size=15, color="#0000FF")
line3=u_gui.draw_text(text="Reye coords:",x=0,y=80,font_size=15, color="#0000FF")
line4=u_gui.draw_text(text="Lmouth coords:",x=0,y=120,font_size=15, color="#0000FF")
line5=u_gui.draw_text(text="Rmouth coords:",x=0,y=160,font_size=15, color="#0000FF")
line6=u_gui.draw_text(text="Nose coords",x=0,y=200,font_size=15, color="#0000FF")
while True:
huskylens.getResult(ALGORITHM_FACE_RECOGNITION)
if (huskylens.available(ALGORITHM_FACE_RECOGNITION)):
line1.config(text=(str("Center face ID:") + str((huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION).ID if huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION) else -1))))
line2.config(text=(str("Leye coords:") + str((str((huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION).leye_x if huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION) else -1)) + str((str(",") + str((huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION).leye_y if huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION) else -1))))))))
line3.config(text=(str("Reye coords:") + str((str((huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION).reye_x if huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION) else -1)) + str((str(",") + str((huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION).reye_y if huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION) else -1))))))))
line4.config(text=(str("Lmouth coords:") + str((str((huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION).lmouth_x if huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION) else -1)) + str((str(",") + str((huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION).lmouth_y if huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION) else -1))))))))
line5.config(text=(str("Rmouth coords:") + str((str((huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION).rmouth_x if huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION) else -1)) + str((str(",") + str((huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION).rmouth_y if huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION) else -1))))))))
line6.config(text=(str("Nose coords") + str((str((huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION).nose_x if huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION) else -1)) + str((str(",") + str((huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION).nose_y if huskylens.getCachedCenterResult(ALGORITHM_FACE_RECOGNITION) else -1))))))))
Output Result: As shown below, after running the program, the M10 screen displays the Face ID and the facial feature coordinate data for that face. Since this face has not been learned, the Face ID is 0.
In addition to the data mentioned above, more face data can be acquired, such as the total count of a specific face in the frame, the name of that face, and data related to the first instance of that face. (This data can also be obtained for unlearned faces).
Taking a learned face as an example, the example program is as follows:
from unihiker import GUI
from pinpong.board import Board
from dfrobot_huskylensv2 import *
u_gui=GUI()
Board().begin()
huskylens = HuskylensV2_I2C()
huskylens.knock()
huskylens.switchAlgorithm(ALGORITHM_FACE_RECOGNITION)
line1=u_gui.draw_text(text="Total faces ID1:",x=0,y=0,font_size=20, color="#0000FF")
line2=u_gui.draw_text(text="Face ID1 name:",x=0,y=40,font_size=20, color="#0000FF")
line3=u_gui.draw_text(text="First face ID:",x=0,y=80,font_size=20, color="#0000FF")
line4=u_gui.draw_text(text="Coords:",x=0,y=120,font_size=20, color="#0000FF")
while True:
huskylens.getResult(ALGORITHM_FACE_RECOGNITION)
if (huskylens.available(ALGORITHM_FACE_RECOGNITION)):
line1.config(text=(str("Total faces ID1:") + str((huskylens.getCachedResultNumByID(ALGORITHM_FACE_RECOGNITION, 1)))))
line2.config(text=(str("Face ID1 name:") + str((huskylens.getCachedResultByID(ALGORITHM_FACE_RECOGNITION, 1).name if huskylens.getCachedResultByID(ALGORITHM_FACE_RECOGNITION, 1) else -1))))
line4.config(text=(str("Coords:") + str((str((huskylens.getCachedIndexResultByID(ALGORITHM_FACE_RECOGNITION, 1, 1-1).xCenter if huskylens.getCachedIndexResultByID(ALGORITHM_FACE_RECOGNITION, 1, 1-1) else -1)) + str((str(",") + str((huskylens.getCachedIndexResultByID(ALGORITHM_FACE_RECOGNITION, 1, 1-1).yCenter if huskylens.getCachedIndexResultByID(ALGORITHM_FACE_RECOGNITION, 1, 1-1) else -1))))))))
Output Result: As shown below, after running the program, the M10 screen displays the total count of Face ID 1 in the frame, the name of that face, and the center XY coordinates of the first detected Face ID 1.
Was this article helpful?
