### Example Dockerfile for Face Recognition Application Source: https://github.com/ageitgey/face_recognition/blob/master/docker/README.md This Dockerfile provides a template for building a custom Docker image based on the prebuilt `animcogn/face_recognition:gpu` image. It shows how to copy application dependencies, install Python requirements, and define the entry point for the application. ```Dockerfile FROM animcogn/face_recognition:gpu COPY requirements.txt requirements.txt RUN pip3 install -r ./requirements.txt COPY my_app /my_app CMD [ "python3", "/my_app/my_app.py" ] ``` -------------------------------- ### Install Face Recognition stable release with pip Source: https://github.com/ageitgey/face_recognition/blob/master/docs/installation.rst This command installs the latest stable version of the Face Recognition library using pip3. It is the recommended method for quick and easy setup. ```shell $ pip3 install face_recognition ``` -------------------------------- ### Install Face Recognition from local source Source: https://github.com/ageitgey/face_recognition/blob/master/docs/installation.rst After obtaining the source code, this command installs the Face Recognition library from your local copy. This is typically used after cloning or downloading the source tarball. ```shell $ python setup.py install ``` -------------------------------- ### Set Up Face Recognition for Local Development Source: https://github.com/ageitgey/face_recognition/blob/master/CONTRIBUTING.rst Initializes a virtual environment for the project, navigates into the project directory, and installs the local copy of `face_recognition` in development mode using `setup.py develop`. ```shell $ mkvirtualenv face_recognition $ cd face_recognition/ $ python setup.py develop ``` -------------------------------- ### Install face_recognition on FreeBSD Source: https://github.com/ageitgey/face_recognition/blob/master/README.md This command installs the `face_recognition` library on FreeBSD systems using the `pkg` package manager. It ensures all necessary dependencies are met for the library to function correctly. ```bash pkg install graphics/py-face_recognition ``` -------------------------------- ### Clone Face Recognition source code from GitHub Source: https://github.com/ageitgey/face_recognition/blob/master/docs/installation.rst This command clones the entire public repository of Face Recognition from GitHub. Use this method to get the complete source code for development or custom installations. ```shell $ git clone git://github.com/ageitgey/face_recognition ``` -------------------------------- ### Importing the Face Recognition Library Source: https://github.com/ageitgey/face_recognition/blob/master/docs/usage.rst Demonstrates how to import the `face_recognition` library into a Python project to begin using its functionalities. ```Python import face_recognition ``` -------------------------------- ### Install face_recognition Library via Pip Source: https://github.com/ageitgey/face_recognition/blob/master/README.md This command installs the `face_recognition` Python library from PyPI using pip3. Ensure that dlib with Python bindings and CMake are already installed as prerequisites for a successful installation. ```bash pip3 install face_recognition ``` -------------------------------- ### Clone Face Recognition Repository Source: https://github.com/ageitgey/face_recognition/blob/master/CONTRIBUTING.rst Clones your forked `face_recognition` repository from GitHub to your local machine, initiating your local development setup. ```shell $ git clone git@github.com:your_name_here/face_recognition.git ``` -------------------------------- ### Download Face Recognition source tarball with curl Source: https://github.com/ageitgey/face_recognition/blob/master/docs/installation.rst This command downloads a compressed tarball of the Face Recognition master branch source code. It's an alternative to cloning the repository if you prefer a direct download. ```shell $ curl -OL https://github.com/ageitgey/face_recognition/tarball/master ``` -------------------------------- ### Verify GPU Setup in Docker Container with dlib Source: https://github.com/ageitgey/face_recognition/blob/master/docker/README.md This Python snippet demonstrates how to check if the GPU is correctly recognized and accessible within a Docker container using the `dlib` library. It prints the number of CUDA devices detected. ```python3 import dlib print(dlib.cuda.get_num_devices()) ``` -------------------------------- ### Install CMake Dependency on Mac/Linux Source: https://github.com/ageitgey/face_recognition/blob/master/README.md This command installs CMake using Homebrew on macOS or a similar package manager on Linux. CMake is a crucial build system generator required for compiling dlib, which is a dependency for `face_recognition`. ```bash brew install cmake ``` -------------------------------- ### Identify and Compare Faces in Images using Python Source: https://github.com/ageitgey/face_recognition/blob/master/README.md This example shows how to compare an unknown face against a known face to determine if they belong to the same person. It involves loading images, encoding faces into numerical representations, and then using `compare_faces` for recognition. ```python import face_recognition known_image = face_recognition.load_image_file("biden.jpg") unknown_image = face_recognition.load_image_file("unknown.jpg") biden_encoding = face_recognition.face_encodings(known_image)[0] unknown_encoding = face_recognition.face_encodings(unknown_image)[0] results = face_recognition.compare_faces([biden_encoding], unknown_encoding) ``` -------------------------------- ### Performing Core Face Recognition Operations Source: https://github.com/ageitgey/face_recognition/blob/master/docs/usage.rst Illustrates various operations that can be performed on a loaded image, including finding face locations, identifying facial landmarks, and generating face encodings. Face encodings are crucial for comparing faces, though their generation can be slow, suggesting caching for performance. ```Python # Find all the faces in the image face_locations = face_recognition.face_locations(image) # Or maybe find the facial features in the image face_landmarks_list = face_recognition.face_landmarks(image) # Or you could get face encodings for each face in the image: list_of_face_encodings = face_recognition.face_encodings(image) ``` -------------------------------- ### Comparing Face Encodings for Match Detection Source: https://github.com/ageitgey/face_recognition/blob/master/docs/usage.rst Demonstrates how to compare a single unknown face encoding against a list of known face encodings using `face_recognition.compare_faces()`. The function returns a boolean array indicating whether the unknown face matches any of the known faces. ```Python # results is an array of True/False telling if the unknown face matched anyone in the known_faces array results = face_recognition.compare_faces(known_face_encodings, a_single_unknown_face_encoding) ``` -------------------------------- ### Loading an Image for Face Recognition Source: https://github.com/ageitgey/face_recognition/blob/master/docs/usage.rst Shows how to load an image file using `face_recognition.load_image_file()`. This function converts the image into a NumPy array, which is the required format for subsequent operations. Users can skip this step if their image is already in a NumPy array. ```Python import face_recognition image = face_recognition.load_image_file("your_file.jpg") ``` -------------------------------- ### Speed Up Face Recognition with Parallel Processing Source: https://github.com/ageitgey/face_recognition/blob/master/README.md This command utilizes multiple CPU cores for faster face recognition by specifying the number of cores with the `--cpus` parameter. For example, `--cpus 4` uses 4 cores, and `--cpus -1` uses all available CPU cores, significantly speeding up processing on multi-core systems. ```bash $ face_recognition --cpus 4 ./pictures_of_people_i_know/ ./unknown_pictures/ ``` -------------------------------- ### Run Code Linting and Tests Source: https://github.com/ageitgey/face_recognition/blob/master/CONTRIBUTING.rst Executes `flake8` for code style checks, runs unit tests using `setup.py test` or `py.test`, and performs comprehensive testing across multiple Python versions with `tox` to ensure code quality and compatibility. ```shell $ flake8 face_recognition tests $ python setup.py test or py.test $ tox ``` -------------------------------- ### Import Libraries for Face Detection Source: https://github.com/ageitgey/face_recognition/blob/master/examples/ipynb_examples/track_faces_on_video_realtime.ipynb This snippet imports necessary libraries for real-time face detection, including `face_recognition` for face processing, `cv2` (OpenCV) for video handling, and `matplotlib` for displaying frames and results. `IPython.display` is used for clearing output in interactive environments. ```python %pylab inline import face_recognition import cv2 import matplotlib.patches as patches from IPython.display import clear_output from matplotlib.pyplot import imshow import matplotlib.pylab as plt ``` -------------------------------- ### Run Face Recognition Docker Image Locally Source: https://github.com/ageitgey/face_recognition/blob/master/README.md This command builds and runs the face recognition application's Docker image locally, allowing for testing and development before deployment to cloud hosting providers. It utilizes the `docker-compose` tool. ```Shell docker-compose up --build ``` -------------------------------- ### Run Specific Python Unit Tests Source: https://github.com/ageitgey/face_recognition/blob/master/CONTRIBUTING.rst Executes a specific subset of unit tests using Python's `unittest` module, targeting a particular test file or module within the project. ```shell $ python -m unittest tests.test_face_recognition ``` -------------------------------- ### Find Faces in Image (Default Model) Source: https://github.com/ageitgey/face_recognition/blob/master/README.md This snippet demonstrates how to detect all faces in a given image using the default face detection model. It loads an image file and returns an array of bounding box coordinates for each detected face. ```python import face_recognition image = face_recognition.load_image_file("my_picture.jpg") face_locations = face_recognition.face_locations(image) # face_locations is now an array listing the co-ordinates of each face! ``` -------------------------------- ### Commit and Push Changes to GitHub Source: https://github.com/ageitgey/face_recognition/blob/master/CONTRIBUTING.rst Stages all modified files, commits them with a descriptive message, and pushes the changes to your remote branch on GitHub, preparing them for a pull request. ```shell $ git add . $ git commit -m "Your detailed description of your changes." $ git push origin name-of-your-bugfix-or-feature ``` -------------------------------- ### Python Package Dependencies for face_recognition Source: https://github.com/ageitgey/face_recognition/blob/master/requirements.txt Lists the Python packages and their minimum version requirements needed to run the face_recognition project. These dependencies are typically found in a requirements.txt file. ```Python face_recognition_models Click>=6.0 dlib>=19.3.0 numpy Pillow scipy>=0.17.0 ``` -------------------------------- ### Create New Git Branch for Development Source: https://github.com/ageitgey/face_recognition/blob/master/CONTRIBUTING.rst Creates a new Git branch for your bugfix or feature, allowing you to make isolated changes locally without affecting the main branch. ```shell $ git checkout -b name-of-your-bugfix-or-feature ``` -------------------------------- ### Generate API Documentation for face_recognition.api Module Source: https://github.com/ageitgey/face_recognition/blob/master/docs/face_recognition.rst This reStructuredText directive instructs Sphinx to automatically generate comprehensive API documentation for the `face_recognition.api` module. It includes all public members, undocumented members, and details about class inheritance, providing a complete reference for the module's functionalities. ```reStructuredText .. automodule:: face_recognition.api :members: :undoc-members: :show-inheritance: ``` -------------------------------- ### Detect Faces in Video Stream and Display Real-time Source: https://github.com/ageitgey/face_recognition/blob/master/examples/ipynb_examples/track_faces_on_video_realtime.ipynb This code processes a video file frame by frame to detect faces in real-time. It uses OpenCV to read video frames and `face_recognition` to find face locations. For performance, it processes every 15th frame. Detected faces are marked with blue dots on the displayed video frame using `matplotlib`. ```python # Loading video for face detection video_capture = cv2.VideoCapture("../hamilton_clip.mp4") frame_count = 0 while video_capture.isOpened(): # Grab a single frame of video ret, frame = video_capture.read() # Bail out when the video file ends if not ret: video_capture.release() break # We will search face in every 15 frames to speed up process. frame_count += 1 if frame_count % 15 == 0: frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Display video frame title("Input Stream") plt.imshow(frame) # Find all the faces and face encodings in the current frame of video rgb_frame = frame[:, :, ::-1] face_locations = face_recognition.face_locations(rgb_frame) # If faces were found, we will mark it on frame with blue dots for face_location in face_locations: plt.plot(face_location[1], face_location[0], 'bo') plt.plot(face_location[1], face_location[2], 'bo') plt.plot(face_location[3], face_location[2], 'bo') plt.plot(face_location[3], face_location[0], 'bo') # Show frame... plt.show() # ... and hold it until a new frame appears clear_output(wait=True) ``` -------------------------------- ### Recognize Faces with face_recognition CLI Source: https://github.com/ageitgey/face_recognition/blob/master/README.md This command uses the `face_recognition` tool to identify people in a folder of unknown pictures by comparing them against a folder of known individuals. The output provides comma-separated values: filename and the recognized person's name, or 'unknown_person' if no match is found. ```bash $ face_recognition ./pictures_of_people_i_know/ ./unknown_pictures/ /unknown_pictures/unknown.jpg,Barack Obama /face_recognition_test/unknown_pictures/unknown.jpg,unknown_person ``` -------------------------------- ### Find Faces in Image (Deep Learning CNN Model) Source: https://github.com/ageitgey/face_recognition/blob/master/README.md This snippet shows how to detect faces using a more accurate deep-learning-based Convolutional Neural Network (CNN) model. This model requires GPU acceleration (via NVidia's CUDA) for good performance and CUDA support enabled during dlib compilation. It returns an array of bounding box coordinates for each detected face. ```python import face_recognition image = face_recognition.load_image_file("my_picture.jpg") face_locations = face_recognition.face_locations(image, model="cnn") # face_locations is now an array listing the co-ordinates of each face! ``` -------------------------------- ### Find Faces in an Image using Python Source: https://github.com/ageitgey/face_recognition/blob/master/README.md This snippet demonstrates how to load an image file and detect the locations of all faces present within it using the `face_recognition` library. The `face_locations` function returns a list of bounding box coordinates for each detected face. ```python import face_recognition image = face_recognition.load_image_file("your_file.jpg") face_locations = face_recognition.face_locations(image) ``` -------------------------------- ### Show Face Recognition Distance Source: https://github.com/ageitgey/face_recognition/blob/master/README.md This command uses the `--show-distance true` parameter to display the calculated face distance for each match. This output helps in fine-tuning the `--tolerance` setting by providing insight into the similarity score between faces. ```bash $ face_recognition --show-distance true ./pictures_of_people_i_know/ ./unknown_pictures/ /unknown_pictures/unknown.jpg,Barack Obama,0.378542298956785 /face_recognition_test/unknown_pictures/unknown.jpg,unknown_person,None ``` -------------------------------- ### Detect Faces with face_detection CLI Source: https://github.com/ageitgey/face_recognition/blob/master/README.md This command uses the `face_detection` tool to find faces in images within a specified folder or a single image. It outputs the filename and the pixel coordinates (top, right, bottom, left) for each detected face, allowing for precise location identification. ```bash $ face_detection ./folder_with_pictures/ examples/image1.jpg,65,215,169,112 examples/image2.jpg,62,394,211,244 examples/image2.jpg,95,941,244,792 ``` -------------------------------- ### Adjust Face Recognition Tolerance Source: https://github.com/ageitgey/face_recognition/blob/master/README.md This command demonstrates how to adjust the sensitivity of face comparisons using the `--tolerance` parameter. The default tolerance is 0.6; lower values make face comparisons stricter, which is useful when people in photos look very similar or to reduce false positives. ```bash $ face_recognition --tolerance 0.54 ./pictures_of_people_i_know/ ./unknown_pictures/ /unknown_pictures/unknown.jpg,Barack Obama /face_recognition_test/unknown_pictures/unknown.jpg,unknown_person ``` -------------------------------- ### Locate Facial Features in Image Source: https://github.com/ageitgey/face_recognition/blob/master/README.md This snippet demonstrates how to identify and locate specific facial features (e.g., eyes, nose, mouth) in an image. It returns a list of dictionaries, where each dictionary contains the coordinates for various facial landmarks for each detected face. ```python import face_recognition image = face_recognition.load_image_file("my_picture.jpg") face_landmarks_list = face_recognition.face_landmarks(image) # face_landmarks_list is now an array with the locations of each facial feature in each face. # face_landmarks_list[0]['left_eye'] would be the location and outline of the first person's left eye. ``` -------------------------------- ### Recognize and Identify Faces in Images Source: https://github.com/ageitgey/face_recognition/blob/master/README.md This snippet illustrates how to recognize and identify individuals by comparing face encodings. It first generates a unique encoding for a known face and then compares it against an unknown face's encoding to determine if they belong to the same person. The `compare_faces` function returns a boolean result. ```python import face_recognition picture_of_me = face_recognition.load_image_file("me.jpg") my_face_encoding = face_recognition.face_encodings(picture_of_me)[0] # my_face_encoding now contains a universal 'encoding' of my facial features that can be compared to any other picture of a face! unknown_picture = face_recognition.load_image_file("unknown.jpg") unknown_face_encoding = face_recognition.face_encodings(unknown_picture)[0] # Now we can see the two face encodings are of the same person with `compare_faces`! results = face_recognition.compare_faces([my_face_encoding], unknown_face_encoding) if results[0] == True: print("It's a picture of me!") else: print("It's not a picture of me!") ``` -------------------------------- ### Detect Facial Landmarks in an Image using Python Source: https://github.com/ageitgey/face_recognition/blob/master/README.md This code identifies key facial features such as eyes, nose, mouth, and chin in an image. The `face_landmarks` function returns a list of dictionaries, where each dictionary contains the coordinates for different facial parts. ```python import face_recognition image = face_recognition.load_image_file("your_file.jpg") face_landmarks_list = face_recognition.face_landmarks(image) ``` -------------------------------- ### Extract Only Names from Face Recognition Output Source: https://github.com/ageitgey/face_recognition/blob/master/README.md This command pipes the output of `face_recognition` to the `cut` utility to extract only the names of the recognized individuals, ignoring the filenames. This is useful for generating simpler output when only the names are required. ```bash $ face_recognition ./pictures_of_people_i_know/ ./unknown_pictures/ | cut -d ',' -f2 Barack Obama unknown_person ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.