vision_face · Face recognition¶
Finds faces, analyzes age, gender and emotion, and learns faces to recognize who they are.
Class:
putTextPIL()
Face
- 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:
objectFunctions:
detect()get_ageGender()get_age()get_gender()init_db()train_face()delete_face()recognize()get_db()save_db()load_db()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 encoded learned face data.It can also be saved to a file so the face information is kept after the instance is deleted.
example:
from openpibo.vision_detect import Face face = Face() # 아래의 모든 예제 이전에 위 코드를 먼저 사용합니다.
- detect_face(img)[source]¶
Detects faces.
example:
img = camera.read() face.detect_face(img)
- Parameters:
img (numpy.ndarray) – Image object
- Returns:
Array of (x, y, w, h) of the detected faces. It is a list; if several faces are detected in one image, the coordinates of all detected faces 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
item (array) – List of face coordinates (x1,y1,x2,y2)
- landmark_face(img, item)[source]¶
Detects facial landmarks.
example:
img = camera.read() face.landmark_face(img)
- Parameters:
img (numpy.ndarray) – Image object
item (array) – Face coordinates (x1,y1,x2,y2)
- Returns:
List of coordinates
- landmark_face_vis(img, coords)[source]¶
Detects facial landmarks.
example:
img = camera.read() face.landmark_face(img)
- Parameters:
img (numpy.ndarray) – Image object
item (array) – Face coordinates (x1,y1,x2,y2)
- Returns:
List of coordinates
- 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 (x, y, w, h)
- Returns:
{age: 0~100, gender: Male or Female, emotions:
neutral,happy,sad,surprise,anger, box:coordinates}
- analyze_face_vis(img, item)[source]¶
Estimates the age, gender and emotion of a face.
- Parameters:
img (numpy.ndarray) – Image object
item (numpy.ndarray) – Face analysis result
- 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 (x, y, w, h)
- Returns:
{"name": name, "score": error}The more similar the faces, the lower the error. Faces with an error of 0.4 or less are judged to be the same person.
- get_db()[source]¶
Checks the face database in use.
example:
face.get_db()
- Returns:
facedb (type
list(list, list)) example:: [ [‘honggildong’], [array([-0.06423206, 0.12474005, 0.0511112 , -0.05676335, -0.07211345, -0.03123881, -0.04119622, -0.12800875, 0.11717855, -0.11079554, 0.22952782, -0.02007426, -0.17457265, -0.13562854, -0.04972655, 0.15810637, -0.12785575, -0.16479518, -0.07002968, -0.00208595, 0.169218 , 0.03144928, -0.01074579, 0.04103286, -0.09245337, … -0.00706697, 0.06025593, -0.0049719 ])] ]
- save_db(filename)[source]¶
Saves the face database to a file.
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.
example:
face.load_db('/home/pi/facedb')
- Parameters:
filename (str) – Path of the
facedbfile 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.