MTCNN
MTCNN
import cv2
from os import path
from mtcnn import MTCNN
from PIL.Image import Image
detector = MTCNN()
current_datetime = get_current_datetime()
if save_path:
# total_datasets = get_total_files(preprocessed_images_dir)
# if total_datasets > 0:
# rmtree(preprocessed_images_dir)
# preprocessed_images_dir =
get_dir(path.join(get_dir(settings.ML_PREPROCESSED_IMAGES_FOLDER), save_path))
username = path.basename(path.normpath(save_path))
prefix_name = generate_file_name(save_path, username, extension="")
else:
prefix_name = current_datetime
if len(detections) == 0:
logger.info("TOTAL DETECTIONS = " + str(len(detections)))
logger.info("RETRYING WITH ORIGINAL IMAGE SIZE")
logger.info(f"PREV SIZE : {img.shape}")
img = image
logger.info(f"CURRENT SIZE : {img.shape}")
detecting_time_start = time.perf_counter()
detections = detector.detect_faces(image)
detecting_time_finish = time.perf_counter()
detection_time = detecting_time_finish - detecting_time_start
if len(detections) == 0:
with open('confidences.csv', 'a') as fd:
fd.write(f"{current_datetime},{prefix_name}.0,{str(0)},FAILED\n")
img_failed_path = path.join(preprocessed_images_dir,
f"FAILED_{prefix_name}.jpg")
cv2.imwrite(img_failed_path, img)
detected_faces = []
highest_conf = 0
total_rejected = 0
for (i, detection) in enumerate(detections):
counter = i + 1
file_name_prefix = f"{prefix_name}.{counter}"
if return_box:
capture_path = path.join(preprocessed_images_dir,
f"{file_name_prefix}.0_input.jpg")
cv2.imwrite(capture_path, img)
score = detection["confidence"]
db = SessionLocal()
with_masked_datasets =
crud_site_setting.site_setting.datasets_with_mask(db)
if custom_threshold:
threshold = custom_threshold
elif with_masked_datasets:
threshold = settings.ML_THRESHOLD_FACE_DETECTION_MASKED
else:
threshold = settings.ML_THRESHOLD_FACE_DETECTION
if multiple_faces:
is_detection_accepted = score >= threshold
else:
is_detection_accepted = score >= threshold and score >= highest_conf
box = detection["box"]
keypoints = detection["keypoints"]
if is_detection_accepted:
highest_conf = score
left_eye = keypoints["left_eye"]
right_eye = keypoints["right_eye"]
# Crop face
cropped_face = crop_face(img, bounding_box, keypoints)
if save_preprocessing:
face_path = path.join(preprocessed_images_dir,
f"{file_name_prefix}.2_face.jpg")
cv2.imwrite(face_path, cropped_face)
# Face alignment
angle_in_radian, direction, point_3rd = align_eyes(left_eye, right_eye)
angle = radian_to_degree(angle_in_radian)
angle = angle_with_direction(angle, direction)
logger.info(f"box: {str(detection['box'])}, conf:
{str(detection['confidence'])}, rotated: {angle}, "
f"time: {detection_time} s")
if recognize_face:
label = recognize(db, detected_face, semester_code, course_code,
save_preprocessing=save_preprocessing)
if multiple_faces:
detected_faces.append(output)
else:
detected_faces = [output]
else:
total_rejected = total_rejected + 1
logger.info("REJECTED -- " + str(detection))
# Put bounding box and face landmarks and save
img_bounding_box = np.copy(img)
img_bounding_box =
put_bounding_box_and_face_landmarks(img_bounding_box, box, keypoints)
img_box_path = path.join(preprocessed_images_dir,
f"REJECTED_{file_name_prefix}.jpg")
cv2.imwrite(img_box_path, img_bounding_box)
total_detected = len(detections)
total_saved = len(detected_faces)
is_different = total_saved != total_detected
logger.info(f"SAVED -- {str(total_saved)}/{str(total_detected)}" + ("
(DIFFERENT)" if is_different else ""))
result = {
"detected_faces": detected_faces,
"total_rejected": total_rejected,
"total_saved": total_saved
}
return result