def test_image(image_to_check, known_names, known_face_encodings, tolerance=0.6, show_distance=False): unknown_image = face_recognition.load_image_file(image_to_check) if max(unknown_image.shape) > 1600: pil_img = PIL.Image.fromarray(unknown_image) pil_img.thumbnail((1600, 1600), PIL.Image.LANCZOS) unknown_image = np.array(pil_img) unknown_encodings = face_recognition.face_encodings(unknown_image) for unknown_encoding in unknown_encodings: distances = face_recognition.face_distance(known_face_encodings, unknown_encoding) result = list(distances <= tolerance) if True in result: [ print_result(image_to_check, name, distance, show_distance) for is_match, name, distance in zip(result, known_names, distances) if is_match ] else: print_result(image_to_check, "unknown_person", None, show_distance) if not unknown_encodings: print_result(image_to_check, "no_persons_found", None, show_distance)
def test_image(image_to_check, model): unknown_image = face_recognition.load_image_file(image_to_check) face_locations = face_recognition.face_locations( unknown_image, number_of_times_to_upsample=0, model=model) for face_location in face_locations: print_result(image_to_check, face_location)
def scan_known_people(known_people_folder): known_names = [] known_face_encodings = [] for file in image_files_in_folder(known_people_folder): basename = os.path.splitext(os.path.basename(file))[0] img = face_recognition.load_image_file(file) encodings = face_recognition.face_encodings(img) if len(encodings) > 1: click.echo( "WARNING: More than one face found in {}. Only considering the first face." .format(file)) if len(encodings) == 0: click.echo( "WARNING: No faces found in {}. Ignoring file.".format(file)) else: known_names.append(basename) known_face_encodings.append(encodings[0]) return known_names, known_face_encodings
def sortByFace(p, imageInput, des, reverse, threadID): filesOwn = filesOwnA nowS = datetime.datetime.now() start = nowS.strftime("%H:%M:%S") imageEncoding = loadFaceAndReturn(p, imageInput, des) print(imageEncoding) # print(str(len(filesOwnA))) if reverse == "1": print("reversing") filesOwn = filesOwnA[::-1] for file in filesOwn: if os.path.exists(file): d = os.path.join(file) print( "---------------------------------------------------------------------------" ) print(d) uk = "" unknownEncoding = "" uk = fr.load_image_file(d) print("Load completed") try: unknownEncoding = fr.face_encodings(uk)[0] print("File image encoded") result = fr.compare_faces([imageEncoding], unknownEncoding) print(result[0]) if result[0] == True: newDir = p + "/" + des if os.path.exists(newDir) == False: os.mkdir(newDir) print("Creating the directory: " + newDir) source = d print(os.path.basename(d)) destination = newDir + "/" + os.path.basename(d) print(destination) now = datetime.datetime.now() a = now.strftime("%Y%m%d%H%M%S") + "[dx]" destinationDX = newDir + "/" + a + os.path.basename(d) if os.path.exists(destination): print( "Transfer: " + os.path.basename(source) + " (This is a duplicated, this file will have a [dx] sign to be easily recognized." ) try: if os.path.exists(source): shutil.move(source, destinationDX) except: print("Error during move") else: try: if os.path.exists(source): print("Transfer: " + os.path.basename(source)) shutil.move(source, destination) except: print("Error during move") except Exception as e: print("No faces detected") nowE = datetime.datetime.now() end = nowE.strftime("%H:%M:%S") d = datetime.datetime.strptime( end, "%H:%M:%S") - datetime.datetime.strptime(start, "%H:%M:%S") print(str(d.seconds) + " seconds elapsed.")
def loadFaceAndReturn(p, imageInput, des): ki = fr.load_image_file(imageInput) print("Base image loaded.") imageEncodingg = fr.face_encodings(ki)[0] print("Base image encoded.") return imageEncodingg