vision_face · Face recognition

Finds faces, analyzes age, gender and emotion, and learns faces to recognize who they are.

Class: putTextPIL() Face

Face detection, age, gender, emotion and face recognition use OpenVINO (OMZ models); face landmarks, direction and distance use MediaPipe. dlib is not used (261104).

openpibo.vision_face.MASK_ONNX = '/home/pi/.model/mask/mask.onnx'

Mask classifier — the same file as edge-lab (embedder + classifier in one, input RGB 0-255 1x224x224x3). Loaded on first use

openpibo.vision_face.DB_FORMAT = 'openpibo-facedb'

Names written inside the face database file (save_db). Files made with another model are not opened

openpibo.vision_face.THRESHOLD = 0.4

Maximum error still counted as the same person. Same value as before (dlib), 0.4, so thresholds used by student code and blocks keep working. Error = cosine distance (0-2) x SCORE_SCALE, matched to the old scale: cosine distance 0.6 is error 0.4. On 1000 pairs of the public adult LFW photos (261104), cosine 0.6 gave 0% false matches and 2.2% missed matches — to be re-checked with children’s faces in the classroom

openpibo.vision_face.MP_TO_DLIB68 = (127, 234, 93, 132, 58, 136, 150, 176, 152, 400, 379, 365, 288, 361, 323, 454, 356, 70, 63, 105, 66, 107, 336, 296, 334, 293, 300, 168, 197, 5, 4, 75, 97, 2, 326, 305, 33, 160, 158, 133, 153, 144, 362, 385, 387, 263, 373, 380, 61, 39, 37, 0, 267, 269, 291, 405, 314, 17, 84, 181, 78, 82, 13, 312, 308, 317, 14, 87)

dlib 68-point index → MediaPipe face landmark index, so that landmark_face keeps the old (dlib) numbering (jaw 0-16 · eyebrows 17-26 · nose 27-35 · eyes 36-47 · mouth 48-67)

openpibo.vision_face.REID_REF5 = array([[0.31556875, 0.46157411],        [0.68262292, 0.45983393],        [0.5002625 , 0.64050536],        [0.34947188, 0.82469196],        [0.65343646, 0.82325089]])

Alignment reference points for face-reidentification-retail-0095 (ratios of 128x128, same values as the OMZ face_recognition_demo). Image-left eye · image-right eye · nose tip · left mouth corner · right mouth corner

openpibo.vision_face.MEAN68 = array([[0.009, 0.428],        [0.017, 0.503],        [0.026, 0.58 ],        [0.045, 0.662],        [0.079, 0.751],        [0.178, 0.872],        [0.243, 0.916],        [0.36 , 0.973],        [0.522, 0.996],        [0.685, 0.966],        [0.803, 0.903],        [0.868, 0.856],        [0.962, 0.729],        [0.989, 0.64 ],        [1.   , 0.556],        [1.001, 0.477],        [0.999, 0.402],        [0.096, 0.341],        [0.136, 0.314],        [0.198, 0.299],        [0.284, 0.301],        [0.384, 0.31 ],        [0.587, 0.304],        [0.687, 0.29 ],        [0.774, 0.284],        [0.841, 0.296],        [0.887, 0.321],        [0.491, 0.392],        [0.493, 0.468],        [0.495, 0.535],        [0.497, 0.576],        [0.413, 0.621],        [0.446, 0.637],        [0.502, 0.639],        [0.559, 0.634],        [0.591, 0.616],        [0.192, 0.412],        [0.235, 0.393],        [0.302, 0.389],        [0.375, 0.415],        [0.315, 0.423],        [0.248, 0.427],        [0.614, 0.409],        [0.683, 0.379],        [0.75 , 0.38 ],        [0.797, 0.396],        [0.741, 0.414],        [0.673, 0.414],        [0.323, 0.748],        [0.4  , 0.716],        [0.451, 0.708],        [0.507, 0.713],        [0.564, 0.705],        [0.618, 0.711],        [0.701, 0.739],        [0.623, 0.805],        [0.572, 0.821],        [0.512, 0.827],        [0.454, 0.824],        [0.404, 0.811],        [0.341, 0.748],        [0.462, 0.737],        [0.509, 0.738],        [0.557, 0.736],        [0.685, 0.74 ],        [0.559, 0.776],        [0.511, 0.78 ],        [0.463, 0.777]])

Mean 68 points (ratios of the face box) returned when no face landmarks are found, so that 68 points are always returned as with dlib before (MediaPipe points averaged over 300 LFW photos, 261104)

openpibo.vision_face.putTextPIL(img, text, points, size=30, colors=(255, 255, 255))[source]

Writes text on an image. (Korean/English supported - uses pillow)- COPY

Parameters:
  • img (numpy.ndarray) – Image object

  • text (str) – Text to display

  • points (tuple(int, int)) – Top-left coordinates of the text block (x, y)

  • size (int) – Font size of the text

  • colors (tuple(int, int, int)) – Text color BGR value (b, g, r) or hex value ‘#rrggbb’

class openpibo.vision_face.Face[source]

Bases: object

Functions: detect_face() landmark_face() analyze_face() mask_face() init_db() train_face() delete_face() recognize() get_db() save_db() load_db() detect_mesh()

A class that performs various face-related functions. It can do the following.

  • Detects faces.

  • Learns/saves/deletes faces.

  • Recognizes learned faces.

  • Estimates age/gender/emotion from a face.

Face database:

Refers to the instance variable facedb, where face data is registered, loaded and deleted.

The face database format is a nested list [[], []]; the first list holds the face names and the second list holds the trained face data (256 numbers each).

It can also be saved to a file so the face information is kept after the instance is deleted.

example:

from openpibo.vision_face import Face

face = Face()
# 아래의 모든 예제 이전에 위 코드를 먼저 사용합니다.
preload()[source]

Loads all face models in advance (so the first use does not wait). You usually do not need to call it — each feature loads only the models it needs the first time you use it.

example:

face.preload()
detect_face(img)[source]

Detects faces.

example:

img = camera.read()
face.detect_face(img)
Parameters:

img (numpy.ndarray) – Image object

Returns:

Array of (x1, y1, x2, y2) of the detected faces. It is a list; if several faces are detected in one image, the coordinates of all of them are included. example:: [[10, 10, 40, 50], [120, 30, 160, 70], [130, 140, 200, 260]]

detect_face_vis(img, items)[source]

Draws face boxes.

Parameters:
  • img (numpy.ndarray) – Image object

  • items (array) – List of face coordinates (x1,y1,x2,y2)

landmark_face(img, item)[source]

Detects facial landmarks (68 points). The point numbering is the same as before (dlib 68 points).

example:

img = camera.read()
items = face.detect_face(img)
face.landmark_face(img, items[0])
Parameters:
  • img (numpy.ndarray) – Image object

  • item (array) – Face coordinates (x1,y1,x2,y2)

Returns:

List of coordinates (68x2)

landmark_face_vis(img, coords)[source]

Draws facial landmarks.

example:

img = camera.read()
items = face.detect_face(img)
coords = face.landmark_face(img, items[0])
face.landmark_face_vis(img, coords)
Parameters:
  • img (numpy.ndarray) – Image object

  • coords (array) – Coordinate list returned by landmark_face

analyze_face(img, item)[source]

Estimates the age, gender and emotion of a face.

example:

img = camera.read()
items = face.detect_face(img)
item = items[0] # item은 items 중 하나
face.analyze_face(img, item)
Parameters:
  • img (numpy.ndarray) – Image object

  • item (numpy.ndarray) – Face coordinates (x1, y1, x2, y2)

Returns:

{age: 0~100, gender: Male or Female, emotions: neutral, happy, sad, surprise, anger, box:coordinates}

analyze_face_vis(img, item)[source]

Draws the age, gender and emotion of a face.

Parameters:
  • img (numpy.ndarray) – Image object

  • item (numpy.ndarray) – Face analysis result

mask_face(img, item)[source]

Tells whether a mask is worn. Same model and same computation as edge-lab (PC).

example:

img = camera.read()
items = face.detect_face(img)
face.mask_face(img, items[0])
Parameters:
  • img (numpy.ndarray) – Image object

  • item (numpy.ndarray) – Face coordinates (x1, y1, x2, y2)

Returns:

{"mask": True/False, "score": 0-100 (probability % of the chosen side), "box": coordinates}

init_db()[source]

Initializes the face database.

The initialized database is an empty nested list [[], []].

example:

face.init_db()
train_face(img, item, name)[source]

Learns a face and saves it to the face database.

example:

img = camera.read()
items = face.detect_face(img)
item = items[0] # item는 items중 하나
face.train_face(img, item, 'honggildong')
Parameters:
  • img (numpy.ndarray) – Image object

  • item (numpy.ndarray) – Top-left and bottom-right points of the detected face rectangle (x1, y1, x2, y2)

  • name (str) – Name to give the detected face

delete_face(name)[source]

Deletes a registered face.

example:

face.delete_face('honggildong')
Parameters:

name (str) – Name of the face to delete

Returns:

True / False

recognize(img, item)[source]

Recognizes registered faces.

example:

img = camera.read()
items = face.detect_face(img)
item = items[0] # item는 items중 하나
face.recognize(img, item)
Parameters:
  • img (numpy.ndarray) – Image object

  • item (numpy.ndarray) – Face coordinates (x1, y1, x2, y2)

Returns:

{"name": name, "score": error, "max": closest name} The more similar the faces, the lower the error. Judged to be the same person when the error is lower than face.threshold (default 0.4).

get_db()[source]

Checks the face database in use.

example:

face.get_db()
Returns:

facedb (list(list, list) type) example:: [ [‘honggildong’], [array([ 0.0412, -0.0731, 0.1093, …, -0.0158], dtype=float32)] # 256 numbers ]

save_db(filename)[source]

Saves the face database to a file (JSON).

example:

face.save_db('/home/pi/facedb')
Parameters:

filename (str) – Path of the face database file to save.

load_db(filename)[source]

Loads a face database file.

Files saved the old (dlib) way are not opened — please register the faces again and save.

example:

face.load_db('/home/pi/facedb')
Parameters:

filename (str) – Path of the facedb file to load.

calculate_head_orientation(keypoints)[source]

(Internal function) Calculates face direction from facial landmark data

get_angle_between_lines(start, point1, point2)[source]

Calculate angle between two lines defined by three points.

detect_mesh_vis(image, items)[source]

Draw connections between landmarks based on Mediapipe’s face mesh.

detect_mesh(image)[source]

Detects facial landmarks and estimates distance and direction

Parameters:

image (numpy.ndarray) – Image object

Returns:

List of {face distance, direction, landmarks} (up to 2 faces)