1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
import os
from os.path import join
from pathlib import Path
import cv2 as cv
import numpy as np
import dlib
import imutils
from app.utils import im_utils, logger_utils
from app.models.bbox import BBox
from app.settings import app_cfg as cfg
from app.settings import types
class RecognitionDLIB:
# https://github.com/davisking/dlib/blob/master/python_examples/face_recognition.py
# facerec.compute_face_descriptor(img, shape, 100, 0.25)
def __init__(self, gpu=0):
self.log = logger_utils.Logger.getLogger()
if gpu > -1:
cuda_visible_devices = os.getenv('CUDA_VISIBLE_DEVICES', '')
os.environ['CUDA_VISIBLE_DEVICES'] = str(gpu)
self.predictor = dlib.shape_predictor(cfg.DIR_MODELS_DLIB_5PT)
self.facerec = dlib.face_recognition_model_v1(cfg.DIR_MODELS_DLIB_FACEREC_RESNET)
if gpu > -1:
os.environ['CUDA_VISIBLE_DEVICES'] = cuda_visible_devices # reset GPU env
def vec(self, im, bbox, width=100,
jitters=cfg.DLIB_FACEREC_JITTERS, padding=cfg.DLIB_FACEREC_PADDING):
'''Converts image and bbox into 128d vector
:param im: (numpy.ndarray) BGR image
:param bbox: (BBox)
'''
# scale the image so the face is always 100x100 pixels
#self.log.debug('compute scale')
scale = width / bbox.width
#im = cv.resize(im, (scale, scale), cv.INTER_LANCZOS4)
#self.log.debug('resize')
cv.resize(im, None, fx=scale, fy=scale, interpolation=cv.INTER_LANCZOS4)
#self.log.debug('to dlib')
bbox_dlib = bbox.to_dlib()
#self.log.debug('precitor')
face_shape = self.predictor(im, bbox_dlib)
# vec = self.facerec.compute_face_descriptor(im, face_shape, jitters, padding)
#self.log.debug('vec')
vec = self.facerec.compute_face_descriptor(im, face_shape, jitters)
#vec = self.facerec.compute_face_descriptor(im, face_shape)
return vec
def similarity(self, query_enc, known_enc):
return np.linalg.norm(query_enc - known_enc, axis=1)
|