face detection python

import face_recognition
import os
import cv2


KNOWN_FACES_DIR = 'known_faces'
UNKNOWN_FACES_DIR = 'unknown_faces'
TOLERANCE = 0.6
FRAME_THICKNESS = 3
FONT_THICKNESS = 2
MODEL = 'cnn'  # default: 'hog', other one can be 'cnn' - CUDA accelerated (if available) deep-learning pretrained model


# Returns (R, G, B) from name
def name_to_color(name):
    # Take 3 first letters, tolower()
    # lowercased character ord() value rage is 97 to 122, substract 97, multiply by 8
    color = [(ord(c.lower())-97)*8 for c in name[:3]]
    return color


print('Loading known faces...')
known_faces = []
known_names = []

# We oranize known faces as subfolders of KNOWN_FACES_DIR
# Each subfolder's name becomes our label (name)
for name in os.listdir(KNOWN_FACES_DIR):

    # Next we load every file of faces of known person
    for filename in os.listdir(f'{KNOWN_FACES_DIR}/{name}'):

        # Load an image
        image = face_recognition.load_image_file(f'{KNOWN_FACES_DIR}/{name}/{filename}')

        # Get 128-dimension face encoding
        # Always returns a list of found faces, for this purpose we take first face only (assuming one face per image as you can't be twice on one image)
        encoding = face_recognition.face_encodings(image)[0]

        # Append encodings and name
        known_faces.append(encoding)
        known_names.append(name)


print('Processing unknown faces...')
# Now let's loop over a folder of faces we want to label
for filename in os.listdir(UNKNOWN_FACES_DIR):

    # Load image
    print(f'Filename {filename}', end='')
    image = face_recognition.load_image_file(f'{UNKNOWN_FACES_DIR}/{filename}')

    # This time we first grab face locations - we'll need them to draw boxes
    locations = face_recognition.face_locations(image, model=MODEL)

    # Now since we know loctions, we can pass them to face_encodings as second argument
    # Without that it will search for faces once again slowing down whole process
    encodings = face_recognition.face_encodings(image, locations)

    # We passed our image through face_locations and face_encodings, so we can modify it
    # First we need to convert it from RGB to BGR as we are going to work with cv2
    image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)

    # But this time we assume that there might be more faces in an image - we can find faces of dirrerent people
    print(f', found {len(encodings)} face(s)')
    for face_encoding, face_location in zip(encodings, locations):

        # We use compare_faces (but might use face_distance as well)
        # Returns array of True/False values in order of passed known_faces
        results = face_recognition.compare_faces(known_faces, face_encoding, TOLERANCE)

        # Since order is being preserved, we check if any face was found then grab index
        # then label (name) of first matching known face withing a tolerance
        match = None
        if True in results:  # If at least one is true, get a name of first of found labels
            match = known_names[results.index(True)]
            print(f' - {match} from {results}')

            # Each location contains positions in order: top, right, bottom, left
            top_left = (face_location[3], face_location[0])
            bottom_right = (face_location[1], face_location[2])

            # Get color by name using our fancy function
            color = name_to_color(match)

            # Paint frame
            cv2.rectangle(image, top_left, bottom_right, color, FRAME_THICKNESS)

            # Now we need smaller, filled grame below for a name
            # This time we use bottom in both corners - to start from bottom and move 50 pixels down
            top_left = (face_location[3], face_location[2])
            bottom_right = (face_location[1], face_location[2] + 22)

            # Paint frame
            cv2.rectangle(image, top_left, bottom_right, color, cv2.FILLED)

            # Wite a name
            cv2.putText(image, match, (face_location[3] + 10, face_location[2] + 15), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (200, 200, 200), FONT_THICKNESS)

    # Show image
    cv2.imshow(filename, image)
    cv2.waitKey(0)
    cv2.destroyWindow(filename)

4.33
6

                                    # Good face detection library to use: OpenCV

4.33 (6 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
facerecognittion python python facerecognition facerecognizer python us? facerecognizer python use facenet? facerecognizer python constrat image face recognition python python face tracker codfe how to create a facial recognition system for office in python dcs face tracking python cofde detect faces using opencv how to create a facial recognition system for office in pyhton open cv face tracker code how can detect { in python facerecognizer python python photo face recognition facedetection python face detection python user face recognition detect only front face python py face recognition python code face identification face api py face recognition algo python face unlock using python face detection algorithm python how to make a decision tree in python that detect faces python image detection opencv face recognition using face recognition library python best face recognition face frecognition python opencv python face detection using opencv face recognition python open cv code to recognize laptops image verification python code face detection.py faceial tracking in python3 how to make my own face recognition python no imports python face identification opencv face tracking example python face recogiton face matching python face detection in opencv python ace detection in opencv python face detection in image face detection project build a face recognition model using python face recognition using camera python imaage detection using opencv python face detection using opencv python face recognition pyton facerec-webcam and emotion with python python real time face detection python face-recognition library python face recognition app with live database face detection using python using camera die faces jupyter notebook face capture pytohn face capture on pytohn face detection opencv teach me face detection code for face recognition. code for face recognition face detection python webcam Tutorial on recognizing faces python how to do live face following python python face detection camera face recognition python opencv python face detection python python face detextion face comparison in python face recognition python yt opencv face examples python face recnition exampel pyton face detection camera face detection and show percentage with cv python simple face detection opencv face detection tracking python face recognition code in python using opencv live facial detection python face detection and recognition python detect face python face recognition project using python facerecognizer opencv python project facial recognition sample face detection using python project opencv python face detection from webcam python easy face recognition face recognition PYHON how to make face recognition software in python face recognition scripts executable file for facial recognition application using python python face recognition match photos database face encoding detailing python face recognition detecting more faces face recognition coede face recognition detecting two faces at once python detect face from image and camera using cv2 python check detected human face from images face recognizer python can't detect faces face recognition python webcam python face rec facial recognition using python face recognition in python using webcam how to use face recongition with python how to use opencv to detect faces face detection in python using a webcam cv2 facial detection python3 opencv recognize face best facial recognition api for python ai ml face unlocker python face recognition opecv face recognition tutorial use deepface with cv2 dlib face recognition script dlib face recognition facial recogmiton person detection tutotierla face detection system in django python live body recognition Python project on human detection using various classifier max lowest tolerance level for facial recongnition using python tolerance level for facial recognition python accuracxy face recognition in opencv python using a webcam how to publish a face recognition python as an application software python skin detection library live face identification python best face recognition live stream python face detection python code python search face on internet live face rec python live face rec python# how to make a face recognition system in python facial detection python code person recognition python face shifter using python face shifter using python python face recognition detect face how to print face detection in python python face recognition and search python face search face tracker in python python webcam live face recognition tutorial python live face recognition tutorial python live facerecognition tutorial opencv face recognition example pyrthon face detection full code in python Detect face movement pyton code for computer face detection library face identification python machine learning face recognition python project face detection frameworks for python face recognition using python and opencv for website detect face moving python python face detection cv2 machine learning python facial detectiom python facia detecton library facial recog library in python gpu face detection pyimagesearch video recognition python face ditiction python hog face recognition python implementation python 3 face recognition face recognition - python throught laptop python face recognation face detection python step by step python facial recognition OpenCV face detector python types of facial detection algorithms in python face recognition script in python python face recognition my face face recognition code python pycharm face recognition with camera face recognition in pycharm multiple cameras facial recognition python face recognition code in python machine learning facial recognition python python facial reconicion face detection alogorithms in python python opencv face detection python facial recognition models list python face tracking face tracking python how to show a recognised face using face recognition library how to face recognition in python facialrecognitiobn python face recognition project facial features recognition library python face recognition through python python script for face detection how to make a face recognition software that makes a square around face with webcam in pytho how to make a program that detection face in python how to make a face recognition sotware tht makes a square around face in py code face detection opencv python how to use face recognition in python how to make a face detection in python latest face detection code python identify face python api face recognition python liv face verification python python code detect faces webcam facial recognition python How to make a webcam facial recognician ai in python face classification python pyimage search facial recognition face detection code python face detection opencv print modi face using python face detection pythn make a face detection app with python face recognition projects facial recongnition python code face recognition project using deep learning full tutorial image recognition opencv python image recognition opencv how to get python track faces face detection code in python face detetction benchmarks c++ vs python face recognition python with opencv code python learn new face how to make real-time face recognition opencv python python opencv face recognition pyton face recognition face extraction python how to create face recognition software in python detect face and blue face python face encoding facial recogintion code Face Recognition using pytho how to do facial recognition in python face movent python moving face detection using python how to face recognition program in python Face matching ai python face tracking code facetracking code webcam authentication in python faceRecognition python add face recognition lock in windows using python face recognition python# face detection ml code in python python face recognition tutorial python webcam face recognition face recognition machine learning python create face detection algorithm python face detection visualization python code face recognition opencv python create a bar graph of faces in camera python how to create facial recognition using python ai face recognition python face detection python opencv opencv face detection python create simple face recognition python without libray face detection code python opencv detect face python face-recognition python live face recognition python opencv python face Authentication opencv face identification python generating csv file of faces in video stream python best face recognition for python python face detector face recognition good example python facial regonition python how to detect images using python without opencv Face Recognition with Python Darwin Windows 10 python face recognition on screen facial detection python python face detection code is it possible to draw a person's face in python face detection using python code Showing results for face recognition and motion detection with python vc Search instead for face recognition and motion dedaction with python vc Ads Face Detection with Python using OpenCV github python detect any face face recognitio python image recognition library python facial recognition in python how to detect face with python python code for face detection for (x, y, w, h) in faces: error opencv face matching python realtime face detection in python python face reconitoin how to face detection in python face recognition using python code opencv facial recognition python script opencv image recognition python python face-recognition face recognition tridimentional python face recognition fast accurate python face recogniition with python face recognition example python face recognition module python face recognition webcam opencv python python person identification image face authentication python code face-recognition python code python detect face simple face recohnition python script face detection with python detection users face recognition python3 user facial comparison python3 make facial comparison python3 python facial detection face detection ai app python face comparison python human face detection python create python3 face recognition password python camera face picture tagging python camera face tagging python camera face detection and tagging python camera scan people opencv python face recognition face recognition using python simple real time face recognition python detect face in image python face recognition using python python face detection library python facial login image compare facial recognition software python face recognition online python face-detection using python how to do face detection in python person reconition python facial verification libraries python python FAC tracking youtube.com how to make face recognition python how to make face recognition python compare 2 faces python python face detection face detection in python face recognition in python real-time face recognition opencv python how to make perfect face recognition in opencv-python only face detection example Face detection OpenCV-Python code how to do real time face recognition in python3 how to make face recognition in python face recognition python code facial recognotion in python python facial detection machine learning face recognition project using opencv-python and deep learning python identify person by face ml face recognition python facial recognition facial recognition project in python facial recognition with python python face recognition from live data live facial recognition python how to make face detection in python face recognition module in python image detection in python python code to detect face face detection program python face recognition webcam hu,man face detection python webcam face identification python webcam how to use face recognition for authentication python webcam best face id python face recognition python deep learning python source code for the similar faces detection make face tracking python face recognize python facial recognition python face detection using python How to make a reliable realtime face recognition python How to make a reliable face recognition python best face recognition code python python face recognition advanced facial recognition python python module for face recognition how to get face recognition in python face recognition python 3 python face detection minscale python side face detection face recognition python face detection algorithm in python how ca i run the code for face detection face detection tutorial with jupyter notebook how to detect face in python best face recognition python face detection python
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source