def show_landmarks(img): sp = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat') detected_face = DeepFace.detectFace(img, detector_backend='mtcnn') detector = dlib.get_frontal_face_detector() img = io.imread(img) win1 = dlib.image_window() win1.clear_overlay() win1.set_image(img) dets = detector(img, 1) print(dets) for k, d in enumerate(dets): print("Detection {}: Left: {} Top: {} Right: {} Bottom: {}".format( k, d.left(), d.top(), d.right(), d.bottom())) shape = sp(img, d) win1.clear_overlay() win1.add_overlay(d) win1.add_overlay(shape) win1.wait_until_closed() face_desc = facerec.compute_face_descriptor(img, shape)
def face_alignment(imgname): '''Rotates the image as much necessary that you could draw a straight line between the two eyes input: path to image return: 224 x 224 x 3 image ''' detected_face = DeepFace.detectFace(imgname) return detected_face
def detect_face(image: Image.Image): # image = image.resize(INPUT_SHAPE_1) arr_image = np.asarray(image) try: s = time.time() detected_face = DeepFace.detectFace(arr_image, detector_backend='mtcnn') e = time.time() print(f'Detection time by DeepFace : {e-s} seconds') except Exception as err: e = time.time() print(f'Detection time by DeepFace : {e-s} seconds - No Face') s = time.time() image = image.resize(INPUT_SHAPE_2) arr_image = np.asarray(image) faces = face_locations(arr_image, model='cnn') e = time.time() print(f'Detection time by Face recognition Library: {e-s} seconds') sorted_faces = sorted(faces, key=lambda x: (x[1] - x[3]) * (x[2] - x[0]), reverse=True) if len(sorted_faces) > 0: top, right, bottom, left = sorted_faces[0] arr_face = arr_image[top:bottom, left:right].copy() arr_face = cv2.resize(arr_face, INPUT_SHAPE_2) return arr_face, False else: return None, False return detected_face, True
def detect_face_deepface(image_path): try: detected_face = DeepFace.detectFace(image_path, detector_backend='opencv') plt.imshow(detected_face) except ValueError: print('Face could not be detected.')
def photo_recognition(path): # детектированиетолько с ипользованием DeepFace img = cv2.imread(path) img = cv2.resize(img, (500, 500)) cv2.imshow('Photo', img) # распознавание лица detected_face = DeepFace.detectFace(img, detector_backend='opencv') cv2.imshow('Detected', detected_face) cv2.waitKey()
def sandbox(img_n): img_abspath = os.path.join(BASE_DIR_PATH, 'prettyface', 'prettyface_jpgs', f'prettyface_{img_n}.jpg') detected_face = DeepFace.detectFace(img_abspath) plt.imshow(detected_face) plt.show() output_abspath = os.path.join(BASE_DIR_PATH, f'sandbox15.jpg') output_abspath_plt = os.path.join(BASE_DIR_PATH, f'sandbox{img_n}_plt.jpg') cv2.imwrite(output_abspath, detected_face) plt.imsave(output_abspath_plt, detected_face)
def photo_recognition(use_dlib=False): if use_dlib: # распознавание с использованием dlib print("Loading dlib...") # создание детекторов face_detector = dlib.get_frontal_face_detector() shape_predictor = dlib.shape_predictor( 'shape_predictor_68_face_landmarks.dat') img = cv2.imread(photo_path) img = cv2.resize(img, (500, 500)) cv2.imshow('Image to process', img) # распознавание лица # ВНИМАНИЕ! Функция detectFace была изменена мной вручную. Сейчас она возвращает фото с наложенным на него # боксом распознанного лица. По умолчанию detectFace не рисует бокс! detected_face = DeepFace.detectFace(img, detector_backend='ssd') # --- работа dlib (распознавание)--- dlib_img = img.copy() dets = face_detector(dlib_img, 1) # рисование лендмарок for det in dets: shape = shape_predictor(dlib_img, det) for i in range(68): point = (shape.part(i).x, shape.part(i).y) # рисуется на фото с боксом! cv2.circle(detected_face, point, 1, (255, 255, 0), 1) # вывод результата cv2.imshow('Detected face', detected_face) key = cv2.waitKey(0) & 0xFF if key == ord("q"): print("[INFO] process finished by user") else: # детектированиетолько с ипользованием DeepFace img = cv2.imread(photo_path) img = cv2.resize(img, (500, 500)) cv2.imshow('Image to process', img) # распознавание лица # ВНИМАНИЕ! Функция detectFace была изменена мной вручную. Сейчас она возвращает фото с наложенным на него # боксом распознанного лица. По умолчанию detectFace не рисует бокс! detected_face = DeepFace.detectFace(img, detector_backend='ssd') cv2.imshow('Detected face', detected_face) key = cv2.waitKey(0) & 0xFF if key == ord("q"): print("[INFO] process finished by user")
def detect_face(img): orig = cv2.imread(img) sp = dlib.shape_predictor('shape_predictor_68_face_landmarks.dat') detected_face = DeepFace.detectFace(img, detector_backend='mtcnn') detector = dlib.get_frontal_face_detector() cv2.imshow("Original image", orig) cv2.imshow("Detected face", detected_face) cv2.waitKey(0) cv2.destroyAllWindows()
def photo_recognition(): photo_path = 'C://Users//sawer//PycharmProjects//lab3//obama.jpg' img = cv2.imread(photo_path) img_copy = cv2.resize(img.copy(), (500, 500)) cv2.imshow('Original', img_copy) img_bgr = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) detected_face = DeepFace.detectFace(img_bgr, detector_backend='ssd') cv2.imshow('Face', detected_face) key = cv2.waitKey(0) & 0xFF if key == ord("q"): print("process finished by user")
def video_recognition(): cap = cv2.VideoCapture(0) while True: grab, original_img = cap.read() ssd_img = original_img.copy() ssd_bgr = cv2.cvtColor(ssd_img, cv2.COLOR_RGB2BGR) try: detected_face = DeepFace.detectFace(ssd_bgr, detector_backend='ssd') cv2.imshow("Face", detected_face) key = cv2.waitKey(1) & 0xFF if key == ord("q"): print("Конец") break except ValueError as v: print(v) cap.release() cv2.destroyAllWindows()
import time import random import warnings warnings.filterwarnings("ignore") from pathlib import Path from deepface import DeepFace backends = ['opencv', 'ssd', 'dlib', 'mtcnn'] frames_directory = "./test_frames" subdirs = [x for x in Path(frames_directory).iterdir() if x.is_dir()] #subdirs = random.sample(subdirs,20) for backend in backends: print("Probando {}".format(backend)) processed = 0 tic = time.perf_counter() for idx,subdir in enumerate(subdirs): for image_path in subdir.glob('*.jpg'): try: detected_aligned_face = DeepFace.detectFace(img_path = str(image_path) , detector_backend = backend) processed += 1 except: continue toc = time.perf_counter() print("Se procesaron {} imágenes.".format(processed)) print("Se tardó {:0.4f} segundos.".format(toc - tic))
from deepface import DeepFace from PIL import Image import numpy as np import pandas as pd pd.set_option('display.max_colwidth', -1) db_path = "/home/collin/Web/piwigo/plugins/MugShot/training" target_img = "/home/collin/Downloads/IMG_8069.jpg" backends = ['opencv', 'ssd', 'dlib', 'mtcnn', 'retinaface'] #face detection and alignment detected_face = DeepFace.detectFace(target_img, detector_backend=backends[4]) #face recognition #df = DeepFace.find(target_img, db_path, detector_backend = backends[4]) #Convert array to image images = Image.fromarray(detected_face) # Display image for image in images: image.show() print(detected_face)
previousTime = 0 while True: frame = vs.read() currentTime = time.time() fps = 1 // (currentTime - previousTime) previousTime = currentTime cv2.putText(frame, f"fps: {fps}", (30, 40), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 0, 50), 2) tic = time.time() try: detected_face = DeepFace.detectFace(frame, detector_backend="ssd") except ValueError: detected_face = cv2.cvtColor(detected_face, cv2.COLOR_RGB2BGR) print("nu face") toc = time.time() print(toc - tic) detected_face = cv2.cvtColor(detected_face, cv2.COLOR_RGB2BGR) cv2.imshow('siodoai', detected_face) if cv2.waitKey(1) == ord("q"): break cv2.destroyAllWindows()
from deepface import DeepFace img1 = DeepFace.detectFace( "C:\\development\\surveilance\\code\\deepface\\img\\image_registered.jpg") #import matplotlib.pyplot as plt #plt.imshow(img1) #plt.show() #plt.imsave("C:\\development\\surveilance\\code\\deepface\\img\\image_aligned.jpg", img1) #import cv2 #cv2.imwrite("C:\\development\\surveilance\\code\\deepface\\img\\image_aligned.jpg", img1)
def test_cases(): print("DeepFace.detectFace test") for detector in detectors: img = DeepFace.detectFace("dataset/img11.jpg", detector_backend=detector) evaluate(img.shape[0] > 0 and img.shape[1] > 0) print(detector, " test is done") print("-----------------------------------------") img_path = "dataset/img1.jpg" embedding = DeepFace.represent(img_path) print("Function returned ", len(embedding), "dimensional vector") evaluate(len(embedding) > 0) print("-----------------------------------------") print("Face detectors test") for detector in detectors: print(detector + " detector") res = DeepFace.verify(dataset[0][0], dataset[0][1], detector_backend=detector) print(res) assert res["verified"] == dataset[0][2] print("-----------------------------------------") print("Find function test") df = DeepFace.find(img_path="dataset/img1.jpg", db_path="dataset") print(df.head()) evaluate(df.shape[0] > 0) print("-----------------------------------------") print("Facial analysis test. Passing nothing as an action") img = "dataset/img4.jpg" demography = DeepFace.analyze(img) print(demography) evaluate(demography["age"] > 20 and demography["age"] < 40) evaluate(demography["dominant_gender"] == "Woman") print("-----------------------------------------") print("Facial analysis test. Passing all to the action") demography = DeepFace.analyze(img, ['age', 'gender', 'race', 'emotion']) print("Demography:") print(demography) #check response is a valid json print("Age: ", demography["age"]) print("Gender: ", demography["dominant_gender"]) print("Race: ", demography["dominant_race"]) print("Emotion: ", demography["dominant_emotion"]) evaluate(demography.get("age") is not None) evaluate(demography.get("dominant_gender") is not None) evaluate(demography.get("dominant_race") is not None) evaluate(demography.get("dominant_emotion") is not None) print("-----------------------------------------") print( "Facial analysis test 2. Remove some actions and check they are not computed" ) demography = DeepFace.analyze(img, ['age', 'gender']) print("Age: ", demography.get("age")) print("Gender: ", demography.get("dominant_gender")) print("Race: ", demography.get("dominant_race")) print("Emotion: ", demography.get("dominant_emotion")) evaluate(demography.get("age") is not None) evaluate(demography.get("dominant_gender") is not None) evaluate(demography.get("dominant_race") is None) evaluate(demography.get("dominant_emotion") is None) print("-----------------------------------------") print("Facial recognition tests") for model in models: for metric in metrics: for instance in dataset: img1 = instance[0] img2 = instance[1] result = instance[2] resp_obj = DeepFace.verify(img1, img2, model_name=model, distance_metric=metric) prediction = resp_obj["verified"] distance = round(resp_obj["distance"], 2) threshold = resp_obj["threshold"] passed = prediction == result evaluate(passed) if passed: test_result_label = "passed" else: test_result_label = "failed" if prediction == True: classified_label = "verified" else: classified_label = "unverified" print( img1.split("/")[-1], "-", img2.split("/")[-1], classified_label, "as same person based on", model, "and", metric, ". Distance:", distance, ", Threshold:", threshold, "(", test_result_label, ")") print("--------------------------") # ----------------------------------------- print("Passing numpy array to analyze function") img = cv2.imread("dataset/img1.jpg") resp_obj = DeepFace.analyze(img) print(resp_obj) evaluate(resp_obj["age"] > 20 and resp_obj["age"] < 40) evaluate(resp_obj["gender"] == "Woman") print("--------------------------") print("Passing numpy array to verify function") img1 = cv2.imread("dataset/img1.jpg") img2 = cv2.imread("dataset/img2.jpg") res = DeepFace.verify(img1, img2) print(res) evaluate(res["verified"] == True) print("--------------------------") print("Passing numpy array to find function") img1 = cv2.imread("dataset/img1.jpg") df = DeepFace.find(img1, db_path="dataset") print(df.head()) evaluate(df.shape[0] > 0) print("--------------------------") print("non-binary gender tests") #interface validation - no need to call evaluate here for img1_path, img2_path, verified in dataset: for detector in detectors: result = DeepFace.analyze(img1_path, actions=('gender', ), detector_backend=detector, enforce_detection=False) print(result) assert 'gender' in result.keys() assert 'dominant_gender' in result.keys( ) and result["dominant_gender"] in ["Man", "Woman"] if result["dominant_gender"] == "Man": assert result["gender"]["Man"] > result["gender"]["Woman"] else: assert result["gender"]["Man"] < result["gender"]["Woman"]
if tf_version == 2: import logging tf.get_logger().setLevel(logging.ERROR) print("Running unit tests for TF ", tf.__version__) from deepface.basemodels import VGGFace, OpenFace, Facenet, FbDeepFace from deepface.extendedmodels import Age, Gender, Race, Emotion print("-----------------------------------------") #----------------------------------------- print("DeepFace.detectFace test") detectors = ['opencv', 'ssd', 'dlib', 'mtcnn', 'retinaface'] for detector in detectors: img = DeepFace.detectFace("dataset/img11.jpg", detector_backend=detector) print(detector, " test is done") #import matplotlib.pyplot as plt #plt.imshow(img) #plt.show() #----------------------------------------- print("-----------------------------------------") img_path = "dataset/img1.jpg" embedding = DeepFace.represent(img_path) print("Function returned ", len(embedding), "dimensional vector") model_name = "VGG-Face" model = DeepFace.build_model(model_name) print(model_name, " is built")
import cv2 from deepface import DeepFace from google.colab import files uploaded = files.upload() from google.colab import files uploaded = files.upload() img1 = cv2.imread('Tom Hanks.jpg') img2 = cv2.imread('johnny-depp 2.jpg') image_face = DeepFace.detectFace(img1) print(img2) def verification(img1, img2, model_name = "VGG-Face", distance_metric = "cosine"): start = time.time() result = DeepFace.verify(img1, img2, model_name=model_name, distance_metric=distance_metric) print(result) demography = DeepFace.analyze(img1, ['age', 'gender', 'emotion']) print(demography) end = time.time()
def take_attendance_by_photo(request): """ Working example for encoding and format: import base64 import requests import json with open("photo.jpg", "rb") as f: s_raw = f.read() s_b64 = base64.b64encode(s_raw) r = requests.post( url='http://localhost:8000/teacher-api/take-attendance/', data=json.dumps({'session_id': 1, 'raw_picture': str(s_b64, 'ascii')}) ) """ serializer = TakeAttendanceSerializer(json.loads(request.body)) photo = base64.b64decode(serializer.data['raw_picture']) session_id = serializer.data['session_id'] """ Store the submitted in temporary folder """ file_name = file_utils.store_temp_file(photo, '.jpg') if imghdr.what(file_name) is None: return HttpResponse(json.dumps( {'error_msg': 'raw_picture is not a valid image data.'}), status=400) """ Initialize folder of this class if not exists """ course_schedule = get_object_or_404(CourseSchedule, id=session_id) is_created_now = file_utils.create_directory(session_id) if is_created_now: # copy out student images and pre-process it print("COPY STUDENT PHOTO") students = [ coursestudent.students for coursestudent in CourseStudent.objects.filter( course_class=course_schedule.course_class) ] print(students) for student in students: src = os.path.join(MEDIA_ROOT, student.image.path) dst = os.path.join(STORAGE_DIR, str(session_id), student.student_id + '.jpg') print(src, dst) shutil.copyfile(src, dst) """ Use deepface to detect face, return if face not detected """ from deepface import DeepFace try: DeepFace.detectFace(file_name, detector_backend='dlib') except ValueError: return HttpResponse(json.dumps({'face_is_detected': False}), status=200) """ Use deepface to check photo against folder return if no face match """ try: df = DeepFace.find(file_name, db_path=os.path.join(file_utils.STORAGE_DIR, str(session_id)), distance_metric='euclidean_l2', detector_backend='dlib') except ValueError: return HttpResponse(json.dumps({ 'error_msg': 'Server failed to detect face existing database images' }), status=500) if len(df) == 0: return HttpResponse(json.dumps({ 'face_is_detected': True, 'matched_student_id': None, }), status=200) result = [(row['distance'], row['identity']) for idx, row in df.iterrows()] result.sort() best_match_file_name = result[0][1] student_id, __ = os.path.basename(best_match_file_name).split('.') """ Take attendance of user in the database, return matched student id """ student_obj = StudentProfile.objects.get(student_id=student_id) if Attendance.objects.filter(attendee=student_obj.user, course_schedule=course_schedule).count() == 0: Attendance.objects.create( attendee=student_obj.user, course_schedule=course_schedule, late=timezone.now() > (course_schedule.open_time + timedelta(minutes=LATE_ATTENDANCE_CUTOFF_MINUTES))) attendance_taken_before = False else: attendance_taken_before = True return HttpResponse(json.dumps({ 'face_is_detected': True, 'matched_student_id': student_id, 'attendance_taken_before': attendance_taken_before, }), status=200)
def video_recognition(use_dlib=False): if use_dlib: # распознавание с использованием dlib print("Loading dlib...") # создание детекторов face_detector = dlib.get_frontal_face_detector() shape_predictor = dlib.shape_predictor( 'shape_predictor_68_face_landmarks.dat') cap = cv2.VideoCapture(0) while True: grab, original_img = cap.read() # --- работа SSD (детектирование)--- ssd_img = original_img.copy() # распознавание лица # ВНИМАНИЕ! Функция detectFace была изменена мной вручную. Сейчас она возвращает фото с наложенным на него # боксом распознанного лица. По умолчанию detectFace не рисует бокс! detected_face = DeepFace.detectFace(ssd_img, detector_backend='ssd') # --- работа dlib (распознавание)--- dlib_img = original_img.copy() dets = face_detector(dlib_img, 1) # рисование лендмарок for det in dets: shape = shape_predictor(dlib_img, det) for i in range(68): point = (shape.part(i).x, shape.part(i).y) # рисуется на фото с боксом! cv2.circle(detected_face, point, 1, (255, 255, 0), 1) # вывод результата cv2.imshow("Output", detected_face) key = cv2.waitKey(1) & 0xFF if key == ord("q"): print("[INFO] process finished by user") break cap.release() cv2.destroyAllWindows() else: # детектирование только с ипользованием DeepFace cap = cv2.VideoCapture(0) while True: grab, original_img = cap.read() # --- работа SSD (детекция)--- ssd_img = original_img.copy() # распознавание лица # ВНИМАНИЕ! Функция detectFace была изменена мной вручную. Сейчас она возвращает фото с наложенным на него # боксом распознанного лица. По умолчанию detectFace не рисует бокс! detected_face = DeepFace.detectFace(ssd_img, detector_backend='ssd') cv2.imshow("Output", detected_face) # для прекращения работы необходимо нажать клавишу "q" key = cv2.waitKey(1) & 0xFF if key == ord("q"): print("[INFO] process finished by user") break cap.release() cv2.destroyAllWindows()
def detect_face(img): detected_face = DeepFace.detectFace(img, detector_backend = 'ssd') plt.imshow(cv2.cvtColor(detected_face, cv2.COLOR_BGR2RGB))
from deepface import DeepFace result = DeepFace.detectFace("face_images/Gates/1.jpg") print(result)
def extract_align_face(img): # it return align face result = DeepFace.detectFace(img) # print(result) return result
backends = ['opencv', 'ssd', 'dlib', 'mtcnn'] backends = backends[1] print("[INFO] starting video stream...") vs = VideoStream(src=0).start() # vs = VideoStream(usePiCamera=True).start() time.sleep(2.0) total = 0 while True: frame = vs.read() base_frame = frame.copy() frame = imutils.resize(frame, width=480) original_size = base_frame.shape detected_aligned_face = DeepFace.detectFace(img_path=frame, detector_backend=backends) print(len(detected_aligned_face)) #print(detected_aligned_face) # show the output frame cv2.imshow("Frame", base_frame) key = cv2.waitKey(1) & 0xFF # if the 'k' key was pressed, write the *orignial* frame to disk # so we can later process it, and use it for regconition if key == ord("k"): p = 'dataset/lvan/' + str(total) + '.png' print(p) cv2.imwrite(p, frame) total += 1