mirror of
https://github.com/serengil/deepface.git
synced 2025-06-07 20:15:21 +00:00
implement multi-faces detections
This commit is contained in:
parent
f36af9ffe7
commit
47733dc7a6
@ -103,8 +103,8 @@ def verify(img1_path, img2_path = '', model_name ='VGG-Face', distance_metric =
|
|||||||
input_shape = input_shape[1:3]
|
input_shape = input_shape[1:3]
|
||||||
|
|
||||||
|
|
||||||
img1 = functions.preprocess_face(img = img1_path, target_size = input_shape, enforce_detection = enforce_detection, detector_backend = detector_backend)
|
img1 = functions.preprocess_face(img = img1_path, target_size = input_shape, enforce_detection = enforce_detection, detector_backend = detector_backend)['processed']
|
||||||
img2 = functions.preprocess_face(img = img2_path, target_size = input_shape, enforce_detection = enforce_detection, detector_backend = detector_backend)
|
img2 = functions.preprocess_face(img = img2_path, target_size = input_shape, enforce_detection = enforce_detection, detector_backend = detector_backend)['processed']
|
||||||
|
|
||||||
img1_representation = custom_model.predict(img1)[0,:]
|
img1_representation = custom_model.predict(img1)[0,:]
|
||||||
img2_representation = custom_model.predict(img2)[0,:]
|
img2_representation = custom_model.predict(img2)[0,:]
|
||||||
@ -271,8 +271,8 @@ def verify(img1_path, img2_path = '', model_name ='VGG-Face', distance_metric =
|
|||||||
#----------------------
|
#----------------------
|
||||||
#crop and align faces
|
#crop and align faces
|
||||||
|
|
||||||
img1 = functions.preprocess_face(img=img1_path, target_size=(input_shape_y, input_shape_x), enforce_detection = enforce_detection, detector_backend = detector_backend)
|
img1 = functions.preprocess_face(img=img1_path, target_size=(input_shape_y, input_shape_x), enforce_detection = enforce_detection, detector_backend = detector_backend)['processed']
|
||||||
img2 = functions.preprocess_face(img=img2_path, target_size=(input_shape_y, input_shape_x), enforce_detection = enforce_detection, detector_backend = detector_backend)
|
img2 = functions.preprocess_face(img=img2_path, target_size=(input_shape_y, input_shape_x), enforce_detection = enforce_detection, detector_backend = detector_backend)['processed']
|
||||||
|
|
||||||
#----------------------
|
#----------------------
|
||||||
#find embeddings
|
#find embeddings
|
||||||
@ -358,7 +358,8 @@ def analyze(img_path, actions = [], models = {}, enforce_detection = True, detec
|
|||||||
|
|
||||||
#if a specific target is not passed, then find them all
|
#if a specific target is not passed, then find them all
|
||||||
if len(actions) == 0:
|
if len(actions) == 0:
|
||||||
actions= ['emotion', 'age', 'gender', 'race']
|
# actions= ['emotion', 'age', 'gender', 'race']
|
||||||
|
actions = ['emotion', 'age', 'gender']
|
||||||
|
|
||||||
#print("Actions to do: ", actions)
|
#print("Actions to do: ", actions)
|
||||||
|
|
||||||
@ -403,14 +404,21 @@ def analyze(img_path, actions = [], models = {}, enforce_detection = True, detec
|
|||||||
for j in global_pbar:
|
for j in global_pbar:
|
||||||
img_path = img_paths[j]
|
img_path = img_paths[j]
|
||||||
|
|
||||||
resp_obj = "{"
|
|
||||||
|
|
||||||
disable_option = False if len(actions) > 1 else True
|
disable_option = False if len(actions) > 1 else True
|
||||||
|
|
||||||
pbar = tqdm(range(0,len(actions)), desc='Finding actions', disable = disable_option)
|
pbar = tqdm(range(0,len(actions)), desc='Finding actions', disable = disable_option)
|
||||||
|
|
||||||
|
# preprocess images
|
||||||
|
emotion_imgs = functions.preprocess_face(img=img_path, target_size=(48, 48), grayscale=True, enforce_detection=enforce_detection, detector_backend=detector_backend)['processed']
|
||||||
|
imgs_224 = functions.preprocess_face(img_path, target_size=(224, 224), grayscale=False, enforce_detection=enforce_detection) # just emotion model expects grayscale images
|
||||||
|
orig_faces = imgs_224['original']
|
||||||
|
imgs_224 = imgs_224['processed']
|
||||||
|
|
||||||
|
for i in range(len(imgs_224)):
|
||||||
|
|
||||||
|
resp_obj = "{"
|
||||||
action_idx = 0
|
action_idx = 0
|
||||||
img_224 = None # Set to prevent re-detection
|
|
||||||
#for action in actions:
|
#for action in actions:
|
||||||
for index in pbar:
|
for index in pbar:
|
||||||
action = actions[index]
|
action = actions[index]
|
||||||
@ -421,9 +429,8 @@ def analyze(img_path, actions = [], models = {}, enforce_detection = True, detec
|
|||||||
|
|
||||||
if action == 'emotion':
|
if action == 'emotion':
|
||||||
emotion_labels = ['angry', 'disgust', 'fear', 'happy', 'sad', 'surprise', 'neutral']
|
emotion_labels = ['angry', 'disgust', 'fear', 'happy', 'sad', 'surprise', 'neutral']
|
||||||
img = functions.preprocess_face(img = img_path, target_size = (48, 48), grayscale = True, enforce_detection = enforce_detection, detector_backend = detector_backend)
|
|
||||||
|
|
||||||
emotion_predictions = emotion_model.predict(img)[0,:]
|
emotion_predictions = emotion_model.predict(emotion_imgs[i])[0,:]
|
||||||
|
|
||||||
sum_of_predictions = emotion_predictions.sum()
|
sum_of_predictions = emotion_predictions.sum()
|
||||||
|
|
||||||
@ -443,20 +450,16 @@ def analyze(img_path, actions = [], models = {}, enforce_detection = True, detec
|
|||||||
resp_obj += emotion_obj
|
resp_obj += emotion_obj
|
||||||
|
|
||||||
elif action == 'age':
|
elif action == 'age':
|
||||||
if img_224 is None:
|
|
||||||
img_224 = functions.preprocess_face(img_path, target_size = (224, 224), grayscale = False, enforce_detection = enforce_detection) #just emotion model expects grayscale images
|
|
||||||
#print("age prediction")
|
#print("age prediction")
|
||||||
age_predictions = age_model.predict(img_224)[0,:]
|
age_predictions = age_model.predict(imgs_224[i])[0,:]
|
||||||
apparent_age = Age.findApparentAge(age_predictions)
|
apparent_age = Age.findApparentAge(age_predictions)
|
||||||
|
|
||||||
resp_obj += "\"age\": %s" % (apparent_age)
|
resp_obj += "\"age\": %s" % (apparent_age)
|
||||||
|
|
||||||
elif action == 'gender':
|
elif action == 'gender':
|
||||||
if img_224 is None:
|
|
||||||
img_224 = functions.preprocess_face(img = img_path, target_size = (224, 224), grayscale = False, enforce_detection = enforce_detection, detector_backend = detector_backend) #just emotion model expects grayscale images
|
|
||||||
#print("gender prediction")
|
#print("gender prediction")
|
||||||
|
|
||||||
gender_prediction = gender_model.predict(img_224)[0,:]
|
gender_prediction = gender_model.predict(imgs_224[i])[0,:]
|
||||||
|
|
||||||
if np.argmax(gender_prediction) == 0:
|
if np.argmax(gender_prediction) == 0:
|
||||||
gender = "Woman"
|
gender = "Woman"
|
||||||
@ -466,9 +469,7 @@ def analyze(img_path, actions = [], models = {}, enforce_detection = True, detec
|
|||||||
resp_obj += "\"gender\": \"%s\"" % (gender)
|
resp_obj += "\"gender\": \"%s\"" % (gender)
|
||||||
|
|
||||||
elif action == 'race':
|
elif action == 'race':
|
||||||
if img_224 is None:
|
race_predictions = race_model.predict(imgs_224[i])[0,:]
|
||||||
img_224 = functions.preprocess_face(img = img_path, target_size = (224, 224), grayscale = False, enforce_detection = enforce_detection, detector_backend = detector_backend) #just emotion model expects grayscale images
|
|
||||||
race_predictions = race_model.predict(img_224)[0,:]
|
|
||||||
race_labels = ['asian', 'indian', 'black', 'white', 'middle eastern', 'latino hispanic']
|
race_labels = ['asian', 'indian', 'black', 'white', 'middle eastern', 'latino hispanic']
|
||||||
|
|
||||||
sum_of_predictions = race_predictions.sum()
|
sum_of_predictions = race_predictions.sum()
|
||||||
@ -493,30 +494,27 @@ def analyze(img_path, actions = [], models = {}, enforce_detection = True, detec
|
|||||||
|
|
||||||
resp_obj = json.loads(resp_obj)
|
resp_obj = json.loads(resp_obj)
|
||||||
|
|
||||||
if bulkProcess == True:
|
|
||||||
resp_objects.append(resp_obj)
|
resp_objects.append(resp_obj)
|
||||||
else:
|
|
||||||
return resp_obj
|
|
||||||
|
|
||||||
if bulkProcess == True:
|
|
||||||
resp_obj = "{"
|
|
||||||
|
|
||||||
for i in range(0, len(resp_objects)):
|
|
||||||
resp_item = json.dumps(resp_objects[i])
|
|
||||||
|
|
||||||
if i > 0:
|
|
||||||
resp_obj += ", "
|
|
||||||
|
|
||||||
resp_obj += "\"instance_"+str(i+1)+"\": "+resp_item
|
|
||||||
resp_obj += "}"
|
|
||||||
resp_obj = json.loads(resp_obj)
|
|
||||||
return resp_obj
|
|
||||||
#return resp_objects
|
|
||||||
|
|
||||||
|
# resp_obj = "{"
|
||||||
|
#
|
||||||
|
# for i in range(0, len(resp_objects)):
|
||||||
|
# resp_item = json.dumps(resp_objects[i])
|
||||||
|
#
|
||||||
|
# if i > 0:
|
||||||
|
# resp_obj += ", "
|
||||||
|
#
|
||||||
|
# resp_obj += "\"instance_"+str(i+1)+"\": "+resp_item
|
||||||
|
# resp_obj += "}"
|
||||||
|
# resp_obj = json.loads(resp_obj)
|
||||||
|
# return resp_obj
|
||||||
|
return resp_objects, orig_faces
|
||||||
|
|
||||||
def detectFace(img_path, detector_backend='opencv'):
|
def detectFace(img_path, detector_backend='opencv'):
|
||||||
img = functions.preprocess_face(img = img_path, detector_backend = detector_backend)[0] #preprocess_face returns (1, 224, 224, 3)
|
imgs = functions.preprocess_face(img=img_path, detector_backend=detector_backend)['processed'] #preprocess_face returns (1, 224, 224, 3)
|
||||||
return img[:, :, ::-1] #bgr to rgb
|
for i in range(len(imgs)):
|
||||||
|
imgs[i] = imgs[i][0][:, :, ::-1] #bgr to rgb
|
||||||
|
return imgs
|
||||||
|
|
||||||
def find(img_path, db_path, model_name ='VGG-Face', distance_metric = 'cosine', model = None, enforce_detection = True, detector_backend = 'opencv'):
|
def find(img_path, db_path, model_name ='VGG-Face', distance_metric = 'cosine', model = None, enforce_detection = True, detector_backend = 'opencv'):
|
||||||
|
|
||||||
@ -659,7 +657,7 @@ def find(img_path, db_path, model_name ='VGG-Face', distance_metric = 'cosine',
|
|||||||
|
|
||||||
input_shape_x = input_shape[0]; input_shape_y = input_shape[1]
|
input_shape_x = input_shape[0]; input_shape_y = input_shape[1]
|
||||||
|
|
||||||
img = functions.preprocess_face(img = employee, target_size = (input_shape_y, input_shape_x), enforce_detection = enforce_detection, detector_backend = detector_backend)
|
img = functions.preprocess_face(img = employee, target_size = (input_shape_y, input_shape_x), enforce_detection = enforce_detection, detector_backend = detector_backend)['processed']
|
||||||
representation = model.predict(img)[0,:]
|
representation = model.predict(img)[0,:]
|
||||||
|
|
||||||
instance = []
|
instance = []
|
||||||
@ -685,7 +683,7 @@ def find(img_path, db_path, model_name ='VGG-Face', distance_metric = 'cosine',
|
|||||||
|
|
||||||
input_shape_x = input_shape[0]; input_shape_y = input_shape[1]
|
input_shape_x = input_shape[0]; input_shape_y = input_shape[1]
|
||||||
|
|
||||||
img = functions.preprocess_face(img = employee, target_size = (input_shape_y, input_shape_x), enforce_detection = enforce_detection, detector_backend = detector_backend)
|
img = functions.preprocess_face(img = employee, target_size = (input_shape_y, input_shape_x), enforce_detection = enforce_detection, detector_backend = detector_backend)['processed']
|
||||||
representation = ensemble_model.predict(img)[0,:]
|
representation = ensemble_model.predict(img)[0,:]
|
||||||
instance.append(representation)
|
instance.append(representation)
|
||||||
|
|
||||||
@ -730,7 +728,7 @@ def find(img_path, db_path, model_name ='VGG-Face', distance_metric = 'cosine',
|
|||||||
else:
|
else:
|
||||||
input_shape = input_shape[1:3]
|
input_shape = input_shape[1:3]
|
||||||
|
|
||||||
img = functions.preprocess_face(img = img_path, target_size = input_shape, enforce_detection = enforce_detection, detector_backend = detector_backend)
|
img = functions.preprocess_face(img = img_path, target_size = input_shape, enforce_detection = enforce_detection, detector_backend = detector_backend)['processed']
|
||||||
target_representation = ensemble_model.predict(img)[0,:]
|
target_representation = ensemble_model.predict(img)[0,:]
|
||||||
|
|
||||||
for k in metric_names:
|
for k in metric_names:
|
||||||
@ -822,7 +820,7 @@ def find(img_path, db_path, model_name ='VGG-Face', distance_metric = 'cosine',
|
|||||||
|
|
||||||
input_shape_x = input_shape[0]; input_shape_y = input_shape[1]
|
input_shape_x = input_shape[0]; input_shape_y = input_shape[1]
|
||||||
|
|
||||||
img = functions.preprocess_face(img = img_path, target_size = (input_shape_y, input_shape_x), enforce_detection = enforce_detection, detector_backend = detector_backend)
|
img = functions.preprocess_face(img = img_path, target_size = (input_shape_y, input_shape_x), enforce_detection = enforce_detection, detector_backend = detector_backend)['processed']
|
||||||
target_representation = model.predict(img)[0,:]
|
target_representation = model.predict(img)[0,:]
|
||||||
|
|
||||||
distances = []
|
distances = []
|
||||||
|
@ -20,14 +20,15 @@ import bz2
|
|||||||
from deepface.commons import distance
|
from deepface.commons import distance
|
||||||
from mtcnn import MTCNN # 0.1.0
|
from mtcnn import MTCNN # 0.1.0
|
||||||
|
|
||||||
|
|
||||||
def loadBase64Img(uri):
|
def loadBase64Img(uri):
|
||||||
encoded_data = uri.split(',')[1]
|
encoded_data = uri.split(',')[1]
|
||||||
nparr = np.fromstring(base64.b64decode(encoded_data), np.uint8)
|
nparr = np.fromstring(base64.b64decode(encoded_data), np.uint8)
|
||||||
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
|
||||||
return img
|
return img
|
||||||
|
|
||||||
def initializeFolder():
|
|
||||||
|
|
||||||
|
def initializeFolder():
|
||||||
home = str(Path.home())
|
home = str(Path.home())
|
||||||
|
|
||||||
if not os.path.exists(home + "/.deepface"):
|
if not os.path.exists(home + "/.deepface"):
|
||||||
@ -38,8 +39,8 @@ def initializeFolder():
|
|||||||
os.mkdir(home + "/.deepface/weights")
|
os.mkdir(home + "/.deepface/weights")
|
||||||
print("Directory ", home, "/.deepface/weights created")
|
print("Directory ", home, "/.deepface/weights created")
|
||||||
|
|
||||||
def findThreshold(model_name, distance_metric):
|
|
||||||
|
|
||||||
|
def findThreshold(model_name, distance_metric):
|
||||||
threshold = 0.40
|
threshold = 0.40
|
||||||
|
|
||||||
if model_name == 'VGG-Face':
|
if model_name == 'VGG-Face':
|
||||||
@ -92,6 +93,7 @@ def findThreshold(model_name, distance_metric):
|
|||||||
|
|
||||||
return threshold
|
return threshold
|
||||||
|
|
||||||
|
|
||||||
def get_opencv_path():
|
def get_opencv_path():
|
||||||
opencv_home = cv2.__file__
|
opencv_home = cv2.__file__
|
||||||
folders = opencv_home.split(os.path.sep)[0:-1]
|
folders = opencv_home.split(os.path.sep)[0:-1]
|
||||||
@ -102,8 +104,8 @@ def get_opencv_path():
|
|||||||
|
|
||||||
return path + "/data/"
|
return path + "/data/"
|
||||||
|
|
||||||
def load_image(img):
|
|
||||||
|
|
||||||
|
def load_image(img):
|
||||||
exact_image = False
|
exact_image = False
|
||||||
if type(img).__module__ == np.__name__:
|
if type(img).__module__ == np.__name__:
|
||||||
exact_image = True
|
exact_image = True
|
||||||
@ -125,8 +127,8 @@ def load_image(img):
|
|||||||
|
|
||||||
return img
|
return img
|
||||||
|
|
||||||
def detect_face(img, detector_backend = 'opencv', grayscale = False, enforce_detection = True):
|
|
||||||
|
|
||||||
|
def detect_face(img, detector_backend='opencv', grayscale=False, enforce_detection=True):
|
||||||
home = str(Path.home())
|
home = str(Path.home())
|
||||||
|
|
||||||
if detector_backend == 'opencv':
|
if detector_backend == 'opencv':
|
||||||
@ -136,7 +138,8 @@ def detect_face(img, detector_backend = 'opencv', grayscale = False, enforce_det
|
|||||||
face_detector_path = opencv_path + "haarcascade_frontalface_default.xml"
|
face_detector_path = opencv_path + "haarcascade_frontalface_default.xml"
|
||||||
|
|
||||||
if os.path.isfile(face_detector_path) != True:
|
if os.path.isfile(face_detector_path) != True:
|
||||||
raise ValueError("Confirm that opencv is installed on your environment! Expected path ",face_detector_path," violated.")
|
raise ValueError("Confirm that opencv is installed on your environment! Expected path ", face_detector_path,
|
||||||
|
" violated.")
|
||||||
|
|
||||||
face_detector = cv2.CascadeClassifier(face_detector_path)
|
face_detector = cv2.CascadeClassifier(face_detector_path)
|
||||||
|
|
||||||
@ -150,9 +153,13 @@ def detect_face(img, detector_backend = 'opencv', grayscale = False, enforce_det
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
if len(faces) > 0:
|
if len(faces) > 0:
|
||||||
x,y,w,h = faces[0] #focus on the 1st face found in the image
|
detected_faces = []
|
||||||
|
for face in faces:
|
||||||
|
print(face)
|
||||||
|
x, y, w, h = face
|
||||||
detected_face = img[int(y):int(y + h), int(x):int(x + w)]
|
detected_face = img[int(y):int(y + h), int(x):int(x + w)]
|
||||||
return detected_face
|
detected_faces.append(detected_face)
|
||||||
|
return detected_faces
|
||||||
|
|
||||||
else: # if no face detected
|
else: # if no face detected
|
||||||
|
|
||||||
@ -160,7 +167,8 @@ def detect_face(img, detector_backend = 'opencv', grayscale = False, enforce_det
|
|||||||
return img
|
return img
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise ValueError("Face could not be detected. Please confirm that the picture is a face photo or consider to set enforce_detection param to False.")
|
raise ValueError(
|
||||||
|
"Face could not be detected. Please confirm that the picture is a face photo or consider to set enforce_detection param to False.")
|
||||||
|
|
||||||
elif detector_backend == 'ssd':
|
elif detector_backend == 'ssd':
|
||||||
|
|
||||||
@ -169,7 +177,6 @@ def detect_face(img, detector_backend = 'opencv', grayscale = False, enforce_det
|
|||||||
|
|
||||||
# model structure
|
# model structure
|
||||||
if os.path.isfile(home + '/.deepface/weights/deploy.prototxt') != True:
|
if os.path.isfile(home + '/.deepface/weights/deploy.prototxt') != True:
|
||||||
|
|
||||||
print("deploy.prototxt will be downloaded...")
|
print("deploy.prototxt will be downloaded...")
|
||||||
|
|
||||||
url = "https://github.com/opencv/opencv/raw/3.4.0/samples/dnn/face_detector/deploy.prototxt"
|
url = "https://github.com/opencv/opencv/raw/3.4.0/samples/dnn/face_detector/deploy.prototxt"
|
||||||
@ -178,10 +185,8 @@ def detect_face(img, detector_backend = 'opencv', grayscale = False, enforce_det
|
|||||||
|
|
||||||
gdown.download(url, output, quiet=False)
|
gdown.download(url, output, quiet=False)
|
||||||
|
|
||||||
|
|
||||||
# pre-trained weights
|
# pre-trained weights
|
||||||
if os.path.isfile(home + '/.deepface/weights/res10_300x300_ssd_iter_140000.caffemodel') != True:
|
if os.path.isfile(home + '/.deepface/weights/res10_300x300_ssd_iter_140000.caffemodel') != True:
|
||||||
|
|
||||||
print("res10_300x300_ssd_iter_140000.caffemodel will be downloaded...")
|
print("res10_300x300_ssd_iter_140000.caffemodel will be downloaded...")
|
||||||
|
|
||||||
url = "https://github.com/opencv/opencv_3rdparty/raw/dnn_samples_face_detector_20170830/res10_300x300_ssd_iter_140000.caffemodel"
|
url = "https://github.com/opencv/opencv_3rdparty/raw/dnn_samples_face_detector_20170830/res10_300x300_ssd_iter_140000.caffemodel"
|
||||||
@ -237,7 +242,8 @@ def detect_face(img, detector_backend = 'opencv', grayscale = False, enforce_det
|
|||||||
bottom = instance["bottom"]
|
bottom = instance["bottom"]
|
||||||
top = instance["top"]
|
top = instance["top"]
|
||||||
|
|
||||||
detected_face = base_img[int(top*aspect_ratio_y):int(bottom*aspect_ratio_y), int(left*aspect_ratio_x):int(right*aspect_ratio_x)]
|
detected_face = base_img[int(top * aspect_ratio_y):int(bottom * aspect_ratio_y),
|
||||||
|
int(left * aspect_ratio_x):int(right * aspect_ratio_x)]
|
||||||
|
|
||||||
return detected_face
|
return detected_face
|
||||||
|
|
||||||
@ -248,10 +254,12 @@ def detect_face(img, detector_backend = 'opencv', grayscale = False, enforce_det
|
|||||||
return img
|
return img
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise ValueError("Face could not be detected. Please confirm that the picture is a face photo or consider to set enforce_detection param to False.")
|
raise ValueError(
|
||||||
|
"Face could not be detected. Please confirm that the picture is a face photo or consider to set enforce_detection param to False.")
|
||||||
|
|
||||||
elif detector_backend == 'dlib':
|
elif detector_backend == 'dlib':
|
||||||
import dlib #this is not a must library within deepface. that's why, I didn't put this import to a global level. version: 19.20.0
|
import \
|
||||||
|
dlib # this is not a must library within deepface. that's why, I didn't put this import to a global level. version: 19.20.0
|
||||||
|
|
||||||
detector = dlib.get_frontal_face_detector()
|
detector = dlib.get_frontal_face_detector()
|
||||||
|
|
||||||
@ -260,8 +268,10 @@ def detect_face(img, detector_backend = 'opencv', grayscale = False, enforce_det
|
|||||||
if len(detections) > 0:
|
if len(detections) > 0:
|
||||||
|
|
||||||
for idx, d in enumerate(detections):
|
for idx, d in enumerate(detections):
|
||||||
left = d.left(); right = d.right()
|
left = d.left();
|
||||||
top = d.top(); bottom = d.bottom()
|
right = d.right()
|
||||||
|
top = d.top();
|
||||||
|
bottom = d.bottom()
|
||||||
|
|
||||||
detected_face = img[top:bottom, left:right]
|
detected_face = img[top:bottom, left:right]
|
||||||
|
|
||||||
@ -273,7 +283,8 @@ def detect_face(img, detector_backend = 'opencv', grayscale = False, enforce_det
|
|||||||
return img
|
return img
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise ValueError("Face could not be detected. Please confirm that the picture is a face photo or consider to set enforce_detection param to False.")
|
raise ValueError(
|
||||||
|
"Face could not be detected. Please confirm that the picture is a face photo or consider to set enforce_detection param to False.")
|
||||||
|
|
||||||
elif detector_backend == 'mtcnn':
|
elif detector_backend == 'mtcnn':
|
||||||
|
|
||||||
@ -282,17 +293,20 @@ def detect_face(img, detector_backend = 'opencv', grayscale = False, enforce_det
|
|||||||
detections = mtcnn_detector.detect_faces(img)
|
detections = mtcnn_detector.detect_faces(img)
|
||||||
|
|
||||||
if len(detections) > 0:
|
if len(detections) > 0:
|
||||||
detection = detections[0]
|
detected_faces = []
|
||||||
|
for detection in detections:
|
||||||
x, y, w, h = detection["box"]
|
x, y, w, h = detection["box"]
|
||||||
detected_face = img[int(y):int(y + h), int(x):int(x + w)]
|
detected_face = img[int(y):int(y + h), int(x):int(x + w)]
|
||||||
return detected_face
|
detected_faces.append(detected_face)
|
||||||
|
return detected_faces
|
||||||
|
|
||||||
else: # if no face detected
|
else: # if no face detected
|
||||||
if enforce_detection != True:
|
if enforce_detection != True:
|
||||||
return img
|
return img
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise ValueError("Face could not be detected. Please confirm that the picture is a face photo or consider to set enforce_detection param to False.")
|
raise ValueError(
|
||||||
|
"Face could not be detected. Please confirm that the picture is a face photo or consider to set enforce_detection param to False.")
|
||||||
|
|
||||||
else:
|
else:
|
||||||
detectors = ['opencv', 'ssd', 'dlib', 'mtcnn']
|
detectors = ['opencv', 'ssd', 'dlib', 'mtcnn']
|
||||||
@ -300,8 +314,8 @@ def detect_face(img, detector_backend = 'opencv', grayscale = False, enforce_det
|
|||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
def alignment_procedure(img, left_eye, right_eye):
|
|
||||||
|
|
||||||
|
def alignment_procedure(img, left_eye, right_eye):
|
||||||
# this function aligns given face in img based on left and right eye coordinates
|
# this function aligns given face in img based on left and right eye coordinates
|
||||||
|
|
||||||
left_eye_x, left_eye_y = left_eye
|
left_eye_x, left_eye_y = left_eye
|
||||||
@ -347,8 +361,8 @@ def alignment_procedure(img, left_eye, right_eye):
|
|||||||
|
|
||||||
return img # return img anyway
|
return img # return img anyway
|
||||||
|
|
||||||
def align_face(img, detector_backend = 'opencv'):
|
|
||||||
|
|
||||||
|
def align_face(img, detector_backend='opencv'):
|
||||||
home = str(Path.home())
|
home = str(Path.home())
|
||||||
|
|
||||||
if (detector_backend == 'opencv') or (detector_backend == 'ssd'):
|
if (detector_backend == 'opencv') or (detector_backend == 'ssd'):
|
||||||
@ -379,12 +393,15 @@ def align_face(img, detector_backend = 'opencv'):
|
|||||||
# -----------------------
|
# -----------------------
|
||||||
# decide left and right eye
|
# decide left and right eye
|
||||||
|
|
||||||
eye_1 = eyes[0]; eye_2 = eyes[1]
|
eye_1 = eyes[0];
|
||||||
|
eye_2 = eyes[1]
|
||||||
|
|
||||||
if eye_1[0] < eye_2[0]:
|
if eye_1[0] < eye_2[0]:
|
||||||
left_eye = eye_1; right_eye = eye_2
|
left_eye = eye_1;
|
||||||
|
right_eye = eye_2
|
||||||
else:
|
else:
|
||||||
left_eye = eye_2; right_eye = eye_1
|
left_eye = eye_2;
|
||||||
|
right_eye = eye_1
|
||||||
|
|
||||||
# -----------------------
|
# -----------------------
|
||||||
# find center of eyes
|
# find center of eyes
|
||||||
@ -401,7 +418,6 @@ def align_face(img, detector_backend = 'opencv'):
|
|||||||
# check required file exists in the home/.deepface/weights folder
|
# check required file exists in the home/.deepface/weights folder
|
||||||
|
|
||||||
if os.path.isfile(home + '/.deepface/weights/shape_predictor_5_face_landmarks.dat') != True:
|
if os.path.isfile(home + '/.deepface/weights/shape_predictor_5_face_landmarks.dat') != True:
|
||||||
|
|
||||||
print("shape_predictor_5_face_landmarks.dat.bz2 is going to be downloaded")
|
print("shape_predictor_5_face_landmarks.dat.bz2 is going to be downloaded")
|
||||||
|
|
||||||
url = "http://dlib.net/files/shape_predictor_5_face_landmarks.dat.bz2"
|
url = "http://dlib.net/files/shape_predictor_5_face_landmarks.dat.bz2"
|
||||||
@ -446,28 +462,39 @@ def align_face(img, detector_backend = 'opencv'):
|
|||||||
|
|
||||||
return img # return img anyway
|
return img # return img anyway
|
||||||
|
|
||||||
def preprocess_face(img, target_size=(224, 224), grayscale = False, enforce_detection = True, detector_backend = 'opencv'):
|
|
||||||
|
|
||||||
|
def preprocess_face(img, target_size=(224, 224), grayscale=False, enforce_detection=True, detector_backend='opencv'):
|
||||||
# img might be path, base64 or numpy array. Convert it to numpy whatever it is.
|
# img might be path, base64 or numpy array. Convert it to numpy whatever it is.
|
||||||
img = load_image(img)
|
img = load_image(img)
|
||||||
base_img = img.copy()
|
base_img = img.copy()
|
||||||
|
|
||||||
img = detect_face(img = img, detector_backend = detector_backend, grayscale = grayscale, enforce_detection = enforce_detection)
|
imgs = detect_face(img=img, detector_backend=detector_backend, grayscale=grayscale,
|
||||||
|
enforce_detection=enforce_detection)
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
|
|
||||||
|
for i in range(len(imgs)):
|
||||||
|
|
||||||
|
img = imgs[i]
|
||||||
|
|
||||||
if img.shape[0] > 0 and img.shape[1] > 0:
|
if img.shape[0] > 0 and img.shape[1] > 0:
|
||||||
img = align_face(img = img, detector_backend = detector_backend)
|
imgs[i] = align_face(img=img, detector_backend=detector_backend)
|
||||||
else:
|
else:
|
||||||
|
|
||||||
if enforce_detection == True:
|
if enforce_detection == True:
|
||||||
raise ValueError("Detected face shape is ", img.shape,". Consider to set enforce_detection argument to False.")
|
raise ValueError("Detected face shape is ", img.shape,
|
||||||
|
". Consider to set enforce_detection argument to False.")
|
||||||
else: # restore base image
|
else: # restore base image
|
||||||
img = base_img.copy()
|
imgs[i] = base_img.copy()
|
||||||
|
|
||||||
# --------------------------
|
# --------------------------
|
||||||
|
|
||||||
# post-processing
|
# post-processing
|
||||||
|
|
||||||
|
pixels = []
|
||||||
|
|
||||||
|
for img in imgs:
|
||||||
|
|
||||||
if grayscale == True:
|
if grayscale == True:
|
||||||
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
||||||
|
|
||||||
@ -476,14 +503,20 @@ def preprocess_face(img, target_size=(224, 224), grayscale = False, enforce_dete
|
|||||||
img_pixels = np.expand_dims(img_pixels, axis=0)
|
img_pixels = np.expand_dims(img_pixels, axis=0)
|
||||||
img_pixels /= 255 # normalize input in [0, 1]
|
img_pixels /= 255 # normalize input in [0, 1]
|
||||||
|
|
||||||
return img_pixels
|
pixels.append(img_pixels)
|
||||||
|
|
||||||
|
return {'processed': pixels, 'original': imgs}
|
||||||
|
|
||||||
|
|
||||||
def allocateMemory():
|
def allocateMemory():
|
||||||
|
|
||||||
# find allocated memories
|
# find allocated memories
|
||||||
gpu_indexes = []
|
gpu_indexes = []
|
||||||
memory_usage_percentages = []; available_memories = []; total_memories = []; utilizations = []
|
memory_usage_percentages = [];
|
||||||
power_usages = []; power_capacities = []
|
available_memories = [];
|
||||||
|
total_memories = [];
|
||||||
|
utilizations = []
|
||||||
|
power_usages = [];
|
||||||
|
power_capacities = []
|
||||||
|
|
||||||
try:
|
try:
|
||||||
result = subprocess.check_output(['nvidia-smi'])
|
result = subprocess.check_output(['nvidia-smi'])
|
||||||
|
441
my_deepface.ipynb
Normal file
441
my_deepface.ipynb
Normal file
File diff suppressed because one or more lines are too long
BIN
test_imgs/.DS_Store
vendored
Normal file
BIN
test_imgs/.DS_Store
vendored
Normal file
Binary file not shown.
BIN
test_imgs/test1.jpg
Normal file
BIN
test_imgs/test1.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 285 KiB |
BIN
test_imgs/test2.jpeg
Normal file
BIN
test_imgs/test2.jpeg
Normal file
Binary file not shown.
After Width: | Height: | Size: 3.8 KiB |
BIN
test_imgs/test3.jpg
Normal file
BIN
test_imgs/test3.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 29 KiB |
Loading…
x
Reference in New Issue
Block a user