{"id":22039411,"url":"https://github.com/mpolinowski/simple-face-recognition","last_synced_at":"2026-05-15T13:35:51.105Z","repository":{"id":234831443,"uuid":"698978992","full_name":"mpolinowski/simple-face-recognition","owner":"mpolinowski","description":"DLIB Face Recognition","archived":false,"fork":false,"pushed_at":"2023-10-01T15:11:13.000Z","size":37122,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-23T13:14:22.363Z","etag":null,"topics":["dlib-face-detection","dlib-face-recognition"],"latest_commit_sha":null,"homepage":"https://mpolinowski.github.io/docs/IoT-and-Machine-Learning/ML/2023-10-01--delib-face-detection/2023-10-01","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mpolinowski.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-10-01T15:10:06.000Z","updated_at":"2023-10-01T15:40:08.000Z","dependencies_parsed_at":null,"dependency_job_id":"e5f54f28-5e82-4c82-b744-46222451aff2","html_url":"https://github.com/mpolinowski/simple-face-recognition","commit_stats":null,"previous_names":["mpolinowski/simple-face-recognition"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/mpolinowski/simple-face-recognition","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpolinowski%2Fsimple-face-recognition","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpolinowski%2Fsimple-face-recognition/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpolinowski%2Fsimple-face-recognition/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpolinowski%2Fsimple-face-recognition/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mpolinowski","download_url":"https://codeload.github.com/mpolinowski/simple-face-recognition/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpolinowski%2Fsimple-face-recognition/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271079227,"owners_count":24695596,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-18T02:00:08.743Z","response_time":89,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["dlib-face-detection","dlib-face-recognition"],"created_at":"2024-11-30T11:10:40.876Z","updated_at":"2026-05-15T13:35:46.078Z","avatar_url":"https://github.com/mpolinowski.png","language":"Jupyter Notebook","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DLIB Face Recognition\n\n* [Face Recognition](https://github.com/ageitgey/face_recognition)\n\nRecognize and manipulate faces from Python or from the command line with the world's simplest face recognition library. Built using [dlib](http://dlib.net/)'s state-of-the-art face recognition built with deep learning. The model has an accuracy of 99.38% on the [Labeled Faces in the Wild](http://vis-www.cs.umass.edu/lfw/) benchmark.\n\n\n* [PyTorch Jupyter](https://github.com/mpolinowski/pytorch-jupyter)\n\n```bash\ndocker run --ipc=host --gpus all -ti --rm \\\n    -v $(pwd):/opt/app -p 8888:8888 \\\n    --name pytorch-jupyter \\\n    pytorch-jupyter:latest\n```\n\n\n\n\n\n```python\n!pip install face_recognition\n```\n\n\n```python\nimport face_recognition\nimport numpy as np\n```\n\n\n```python\nimg_bobby =\"faces/bobbie_w_draper.jpg\"\nimg_jim =\"faces/jim_holden.jpg\"\nimg_amos =\"faces/amos_burton.jpg\"\nimg_camina =\"faces/camina_drummer.jpg\"\nimg_naomi =\"faces/naomi_nagata.jpg\"\nimg_chrisjen =\"faces/chrisjen_avasarala.jpg\"\n\nimage_path = img_bobby\n```\n\n## Detect Face Location (CPU)\n\nRe-run the following steps for all training images above:\n\n\n```python\nimage = face_recognition.load_image_file(image_path)\nface_locations = face_recognition.face_locations(image)\n```\n\n### Crop Location\n\n\n```python\nimport cv2 as cv\nimport matplotlib.pyplot as plt\n```\n\n\n```python\nimg = cv.imread(image_path)\nimg = cv.cvtColor(img, cv.COLOR_BGR2RGB)\n```\n\n\n```python\nplt.imshow(img)\nplt.axis('off')\n\nfor face_location in face_locations:  \n    plt.plot(face_location[3], face_location[0], 'ro') \n    plt.plot(face_location[1], face_location[0], 'r+')     \n    plt.plot(face_location[3], face_location[2], 'bo')\n    plt.plot(face_location[1], face_location[2], 'b+')\n\nplt.show()\n```\n\n\n    \n![DLIB Face Recognition](assets/output_10_0.png)\n    \n\n\n\n```python\nfor face_location in face_locations: \n    x1, y1 = face_location[3], face_location[2]\n    x2, y2 = face_location[1], face_location[2]\n    x3, y3 = face_location[1], face_location[0]\n    x4, y4 = face_location[3], face_location[0]\n\ntop_left_x = min([x1,x2,x3,x4])\ntop_left_y = min([y1,y2,y3,y4])\nbot_right_x = max([x1,x2,x3,x4])\nbot_right_y = max([y1,y2,y3,y4])\n\ncropped_image = img[top_left_y:bot_right_y, top_left_x:bot_right_x]\n```\n\n\n```python\nplt.imshow(cropped_image)\nplt.axis('off')\n```\n\n\n\n    \n![DLIB Face Recognition](assets/output_12_1.png)\n    \n\n\n\n```python\ncv.imwrite('faces/cut/bobbie_w_draper.jpg', cv.cvtColor(cropped_image, cv.COLOR_RGB2BGR))\n```\n\n\n\n### Get all the Training Images\n\n\n```python\nbobby_train = face_recognition.load_image_file(img_bobby)\njim_train = face_recognition.load_image_file(img_jim)\namos_train = face_recognition.load_image_file(img_amos)\ncamina_train = face_recognition.load_image_file(img_camina)\nnaomi_train = face_recognition.load_image_file(img_naomi)\nchrisjen_train = face_recognition.load_image_file(img_chrisjen)\n\nbobby_encoding = face_recognition.face_encodings(bobby_train)[0]\njim_encoding = face_recognition.face_encodings(jim_train)[0]\namos_encoding = face_recognition.face_encodings(amos_train)[0]\ncamina_encoding = face_recognition.face_encodings(camina_train)[0]\nnaomi_encoding = face_recognition.face_encodings(naomi_train)[0]\nchrisjen_encoding = face_recognition.face_encodings(chrisjen_train)[0]\n```\n\n\n```python\nfrom glob import glob\n\ncropped_images = glob('./faces/cut/*.jpg')\n```\n\n\n```python\nplt.figure(figsize=(12, 8))\nplt.suptitle('Training Images')\n\nax = plt.subplot(2, 3, 1)\nimg_path = cropped_images[0]\nimg_title = 'face: ' + cropped_images[0][12:-4]\nplt.title(img_title, fontsize='medium')\nimage = plt.imread(img_path)\nplt.imshow(image, cmap=plt.cm.binary)\n\nax = plt.subplot(2, 3, 2)\nimg_path = cropped_images[1]\nimg_title = 'face: ' + cropped_images[1][12:-4]\nplt.title(img_title, fontsize='medium')\nimage = plt.imread(img_path)\nplt.imshow(image, cmap=plt.cm.binary)\n\nax = plt.subplot(2, 3, 3)\nimg_path = cropped_images[2]\nimg_title = 'face: ' + cropped_images[2][12:-4]\nplt.title(img_title, fontsize='medium')\nimage = plt.imread(img_path)\nplt.imshow(image, cmap=plt.cm.binary)\n\nax = plt.subplot(2, 3, 4)\nimg_path = cropped_images[3]\nimg_title = 'face: ' + cropped_images[3][12:-4]\nplt.title(img_title, fontsize='medium')\nimage = plt.imread(img_path)\nplt.imshow(image, cmap=plt.cm.binary)\n\nax = plt.subplot(2, 3, 5)\nimg_path = cropped_images[4]\nimg_title = 'face: ' + cropped_images[4][12:-4]\nplt.title(img_title, fontsize='medium')\nimage = plt.imread(img_path)\nplt.imshow(image, cmap=plt.cm.binary)\n\nax = plt.subplot(2, 3, 6)\nimg_path = cropped_images[5]\nimg_title = 'face: ' + cropped_images[5][12:-4]\nplt.title(img_title, fontsize='medium')\nimage = plt.imread(img_path)\nplt.imshow(image, cmap=plt.cm.binary)\n```\n\n\n    \n![DLIB Face Recognition](assets/output_17_1.png)\n    \n\n\n## Face Recognition\n\nLoading a bunch of test images with \"unknown\" faces:\n\n\n```python\ntest_image1 = face_recognition.load_image_file(\"faces/test/unknown_01.jpg\")\ntest_image2 = face_recognition.load_image_file(\"faces/test/unknown_02.jpg\")\ntest_image3 = face_recognition.load_image_file(\"faces/test/unknown_03.jpg\")\ntest_image4 = face_recognition.load_image_file(\"faces/test/unknown_04.jpg\")\ntest_image5 = face_recognition.load_image_file(\"faces/test/unknown_05.jpg\")\ntest_image6 = face_recognition.load_image_file(\"faces/test/unknown_06.jpg\")\n\ntest1_encoding = face_recognition.face_encodings(test_image1)\ntest2_encoding = face_recognition.face_encodings(test_image2)\ntest3_encoding = face_recognition.face_encodings(test_image3)\ntest4_encoding = face_recognition.face_encodings(test_image4)\ntest5_encoding = face_recognition.face_encodings(test_image5)\ntest6_encoding = face_recognition.face_encodings(test_image6)\n```\n\n### Compare Faces\n\nCompare all detected images in the test dataset to the training images:\n\n\n```python\ntrained_images = [bobby_encoding, jim_encoding, amos_encoding, camina_encoding, naomi_encoding, chrisjen_encoding]\ntrained_faces = np.array([\"bobbie_w_draper\", \"jim_holden\", \"amos_burton\", \"camina_drummer\", \"naomi_nagata\", \"chrisjen_avasarala\"])\n```\n\n#### Test Image 1\n\n\n```python\ntest1_results = []\n\nfor detection in test1_encoding:\n    result = face_recognition.compare_faces(trained_images, detection)\n    test1_results.append(trained_faces[result])\n```\n\n\n```python\ntest_img1 = plt.imread('faces/test/unknown_01.jpg')\nplt.title('detected faces: \\n' + str(test1_results), fontsize='small')\nplt.axis('off')\nplt.imshow(test_img1)\n```\n\n\n    \n![DLIB Face Recognition](assets/output_24_1.png)\n    \n\n\n#### Test Image 2\n\n\n```python\ntest2_results = []\n\nfor detection in test2_encoding:\n    result = face_recognition.compare_faces(trained_images, detection)\n    test1_results.append(trained_faces[result])\n```\n\n\n```python\ntest_img2 = plt.imread('faces/test/unknown_02.jpg')\nplt.title('detected faces: \\n' + str(test2_results), fontsize='small')\nplt.axis('off')\nplt.imshow(test_img2)\n```\n\n\n\n    \n![DLIB Face Recognition](assets/output_27_1.png)\n    \n\n\n#### Test Image 3\n\n\n```python\ntest3_results = []\n\nfor detection in test3_encoding:\n    result = face_recognition.compare_faces(trained_images, detection)\n    test3_results.append(trained_faces[result])\n\ntest3_results\n```\n\n\n\n\n    [array(['naomi_nagata'], dtype='\u003cU18'), array(['amos_burton'], dtype='\u003cU18')]\n\n\n\n\n```python\ntest_img3 = plt.imread('faces/test/unknown_03.jpg')\nplt.title('detected faces: \\n' + str(test3_results), fontsize='small')\nplt.axis('off')\nplt.imshow(test_img3)\n```\n\n    \n![DLIB Face Recognition](assets/output_30_1.png)\n    \n\n\n#### Test Image 4\n\n\n```python\ntest4_results = []\n\nfor detection in test4_encoding:\n    result = face_recognition.compare_faces(trained_images, detection)\n    test4_results.append(trained_faces[result])\n```\n\n\n```python\ntest_img4 = plt.imread('faces/test/unknown_04.jpg')\nplt.title('detected faces: \\n' + str(test4_results), fontsize='small')\nplt.axis('off')\nplt.imshow(test_img4)\n```\n\n    \n![DLIB Face Recognition](assets/output_33_1.png)\n    \n\n\n#### Test Image 5\n\n\n```python\ntest5_results = []\n\nfor detection in test5_encoding:\n    result = face_recognition.compare_faces(trained_images, detection)\n    test5_results.append(trained_faces[result])\n```\n\n\n```python\ntest_img5 = plt.imread('faces/test/unknown_05.jpg')\nplt.title('detected faces: \\n' + str(test5_results), fontsize='small')\nplt.axis('off')\nplt.imshow(test_img5)\n```\n\n    \n![DLIB Face Recognition](assets/output_36_1.png)\n    \n\n\n#### Test Image 6\n\n\n```python\ntest6_results = []\n\nfor detection in test6_encoding:\n    result = face_recognition.compare_faces(trained_images, detection)\n    test6_results.append(trained_faces[result])\n```\n\n\n```python\ntest_img6 = plt.imread('faces/test/unknown_06.jpg')\nplt.title('detected faces: \\n' + str(test6_results), fontsize='small')\nplt.axis('off')\nplt.imshow(test_img6)\n```\n\n    \n![DLIB Face Recognition](assets/output_39_1.png)\n    \n\n\n## Detect Face Location (GPU + Batch Processing)\n\n\n```python\nall_training_images = glob('./faces/*.jpg')\nlen(all_training_images)\n```\n\n\n\n\n    60\n\n\n\n\n```python\nran_gen = np.random.default_rng()\n\nplt.figure(figsize=(14, 12))\nplt.suptitle('Training Images')\n\nfor i in range(16):\n    ax = plt.subplot(4, 4, i+1)\n    random_index = ran_gen.integers(low=0, high=59, size=1)\n    i = random_index[0]\n    img_loc = all_training_images[i]\n    img_title = 'label: ' + all_training_images[i][8:-4]\n    image = plt.imread(img_loc)\n    plt.imshow(image)\n    plt.title(img_title, fontsize='small')\n    plt.axis(False)\n```\n\n\n    \n![DLIB Face Recognition](assets/output_42_0.png)\n    \n\n\nFor this experiment I collected 10 images from all faces that I used before. All training images only contain one face - so I expect only getting one location that I can map to the image label:\n\n\n```python\nimage_labels = []\nface_locations = []\n\nfor image_path in all_training_images:\n    image_labels.append(image_path[8:-5])\n    image = face_recognition.load_image_file(image_path)\n    location = face_recognition.face_locations(image, model=\"cnn\")\n    face_locations.append(location)\n```\n\n\n```python\nprint(len(image_labels), len(face_locations))\n```\n\n    60 60\n\n\nNow I can get the feature vector for every detected face by it's bounding box:\n\n\n```python\nface_encodings = []\ni = 0\n\nfor location in face_locations:\n    image = face_recognition.load_image_file(all_training_images[i])\n    encoding = face_recognition.face_encodings(image, location)[0]\n\n    face_encodings.append(encoding)\n    i+=1\n```\n\n\n```python\nlen(face_encodings)\n```\n\n\n\n\n    60\n\n\n\n\n```python\ntestcnn1_results = []\n\nfor detection in test1_encoding:\n    result = face_recognition.compare_faces(face_encodings, detection)\n    testcnn1_results.append(np.array(image_labels)[result])\n```\n\n\n```python\nclasses = []\n\ntest_img1 = plt.imread('faces/test/unknown_01.jpg')\n\nfor result in testcnn1_results:\n    label, count = np.unique(result, return_counts=True)\n    classes.append(\n        (\n            # check for unlabeled faces\n            count[0] if 0 \u003c len(count) else None, \n            label[0] if 0 \u003c len(label) else None\n        )\n    )\n    \nplt.title(str(classes), fontsize='small')\nplt.axis('off')\nplt.imshow(test_img1)\n```\n\n    \n![DLIB Face Recognition](assets/output_50_1.png)\n    \n\n\n\n```python\ntestcnn2_results = []\n\nfor detection in test2_encoding:\n    result = face_recognition.compare_faces(face_encodings, detection)\n    testcnn2_results.append(np.array(image_labels)[result])\n```\n\n\n```python\nclasses = []\n\ntest_img2 = plt.imread('faces/test/unknown_02.jpg')\n\nfor result in testcnn2_results:\n    label, count = np.unique(result, return_counts=True)\n    classes.append(\n        (\n            # check for unlabeled faces\n            count[0] if 0 \u003c len(count) else None, \n            label[0] if 0 \u003c len(label) else None\n        )\n    )\n    \nplt.title(str(classes), fontsize='small')\nplt.axis('off')\nplt.imshow(test_img2)\n```\n\n    \n![DLIB Face Recognition](assets/output_52_1.png)\n    \n\n\n\n```python\ntestcnn3_results = []\n\nfor detection in test3_encoding:\n    result = face_recognition.compare_faces(face_encodings, detection)\n    testcnn3_results.append(np.array(image_labels)[result])\n```\n\n\n```python\nclasses = []\ntest_img3 = plt.imread('faces/test/unknown_03.jpg')\n\nfor result in testcnn3_results:\n    label, count = np.unique(result, return_counts=True)\n    classes.append(\n        (\n            # check for unlabeled faces\n            count[0] if 0 \u003c len(count) else None, \n            label[0] if 0 \u003c len(label) else None\n        )\n    )\n    \nplt.title(str(classes), fontsize='small')\nplt.axis('off')\nplt.imshow(test_img3)\n```\n    \n![DLIB Face Recognition](assets/output_54_1.png)\n    \n\n\n\n```python\ntestcnn4_results = []\n\nfor detection in test4_encoding:\n    result = face_recognition.compare_faces(face_encodings, detection)\n    testcnn4_results.append(np.array(image_labels)[result])\n```\n\n\n```python\nclasses = []\ntest_img4 = plt.imread('faces/test/unknown_04.jpg')\n\nfor result in testcnn4_results:\n    label, count = np.unique(result, return_counts=True)\n    classes.append(\n        (\n            # check for unlabeled faces\n            count[0] if 0 \u003c len(count) else None, \n            label[0] if 0 \u003c len(label) else None\n        )\n    )\n    \nplt.title(str(classes), fontsize='small')\nplt.axis('off')\nplt.imshow(test_img4)\n```\n\n    \n![DLIB Face Recognition](assets/output_56_1.png)\n    \n\n\n\n```python\ntestcnn5_results = []\n\nfor detection in test5_encoding:\n    result = face_recognition.compare_faces(face_encodings, detection)\n    testcnn5_results.append(np.array(image_labels)[result])\n```\n\n\n```python\nclasses = []\ntest_img5 = plt.imread('faces/test/unknown_05.jpg')\n\nfor result in testcnn5_results:\n    label, count = np.unique(result, return_counts=True)\n    classes.append(\n        (\n            # check for unlabeled faces\n            count[0] if 0 \u003c len(count) else None, \n            label[0] if 0 \u003c len(label) else None\n        )\n    )\n    \nplt.title(str(classes), fontsize='small')\nplt.axis('off')\nplt.imshow(test_img5)\n```\n\n    \n![DLIB Face Recognition](assets/output_58_1.png)\n    \n\n\n\n```python\ntestcnn6_results = []\n\nfor detection in test6_encoding:\n    result = face_recognition.compare_faces(face_encodings, detection)\n    testcnn6_results.append(np.array(image_labels)[result])\n```\n\n\n```python\nclasses = []\ntest_img6 = plt.imread('faces/test/unknown_06.jpg')\n\nfor result in testcnn6_results:\n    label, count = np.unique(result, return_counts=True)\n    classes.append(\n        (\n            # check for unlabeled faces\n            count[0] if 0 \u003c len(count) else None, \n            label[0] if 0 \u003c len(label) else None\n        )\n    )\n    \nplt.title(str(classes), fontsize='small')\nplt.axis('off')\nplt.imshow(test_img6)\n```\n\n\n    \n![DLIB Face Recognition](assets/output_60_1.png)\n    \n\n\n## Draw Bounding Boxes\n\n\n```python\nfrom PIL import Image, ImageDraw\n```\n\nI'm using the encodings/labels for the 60 training images generated above:\n\n\n```python\nprint(len(face_encodings), len(image_labels))\n```\n\n    60 60\n\n\nLoad a test image that contains one of the faces above:\n\n\n```python\nunknown_image = face_recognition.load_image_file('faces/test/unknown_01.jpg')\n\n# Find all the faces and face encodings in the unknown image\nfound_faces = face_recognition.face_locations(unknown_image)\nfound_face_encodings = face_recognition.face_encodings(unknown_image, found_faces)\n\n# Convert the image to a PIL-format image so that we can draw on top of it with the Pillow library\npil_image = Image.fromarray(unknown_image)\n```\n\n\n```python\n# Create a Pillow ImageDraw Draw instance to draw with\ndraw = ImageDraw.Draw(pil_image)\n\n# Loop through each face found in the unknown image\nfor (top, right, bottom, left), face_encoding in zip(found_faces, found_face_encodings):\n    # See if the face is a match for the known face(s)\n    matches = face_recognition.compare_faces(face_encodings, face_encoding)\n\n    name = \"Unknown\"\n\n    # If a match was found in face_encodings, just use the first one.\n    # if True in matches:\n    #     first_match_index = matches.index(True)\n    #     name = image_labels[first_match_index]\n\n    # Or instead, use the known face with the smallest distance to the new face\n    face_distances = face_recognition.face_distance(face_encodings, face_encoding)\n    best_match_index = np.argmin(face_distances)\n    if matches[best_match_index]:\n        name = image_labels[best_match_index]\n    # Draw a box around the face using the Pillow module\n    draw.rectangle(((left, top), (right, bottom)), outline=(0, 0, 255))\n\n    # Draw a label with a name below the face\n    text_width, text_height = draw.textsize(name)\n    draw.rectangle(((left, bottom - text_height - 10), (right, bottom)), fill=(0, 0, 255), outline=(0, 0, 255))\n    draw.text((left + 6, bottom - text_height - 5), name, fill=(255, 255, 255, 255))\n\n\n# Remove the drawing library from memory as per the Pillow docs\ndel draw\n\n# Display the resulting image\npil_image.show()\n```\n\n    \n![DLIB Face Recognition](assets/output_67_1.png)\n    \n\n\n\n```python\nunknown_image2 = face_recognition.load_image_file('faces/test/unknown_02.jpg')\n\n# Find all the faces and face encodings in the unknown image\nfound_faces2 = face_recognition.face_locations(unknown_image2)\nfound_face_encodings2 = face_recognition.face_encodings(unknown_image2, found_faces2)\n\n# Convert the image to a PIL-format image so that we can draw on top of it with the Pillow library\npil_image2 = Image.fromarray(unknown_image2)\n```\n\n\n```python\n# Create a Pillow ImageDraw Draw instance to draw with\ndraw = ImageDraw.Draw(pil_image2)\n\n# Loop through each face found in the unknown image\nfor (top, right, bottom, left), face_encoding in zip(found_faces2, found_face_encodings2):\n    # See if the face is a match for the known face(s)\n    matches = face_recognition.compare_faces(face_encodings, face_encoding)\n\n    name = \"Unknown\"\n\n    # If a match was found in face_encodings, just use the first one.\n    # if True in matches:\n    #     first_match_index = matches.index(True)\n    #     name = image_labels[first_match_index]\n\n    # Or instead, use the known face with the smallest distance to the new face\n    face_distances = face_recognition.face_distance(face_encodings, face_encoding)\n    best_match_index = np.argmin(face_distances)\n    if matches[best_match_index]:\n        name = image_labels[best_match_index]\n\n    # Draw a box around the face using the Pillow module\n    draw.rectangle(((left, top), (right, bottom)), outline=(0, 0, 255))\n\n    # Draw a label with a name below the face\n    text_width, text_height = draw.textsize(name)\n    draw.rectangle(((left, bottom - text_height - 10), (right, bottom)), fill=(0, 0, 255), outline=(0, 0, 255))\n    draw.text((left + 6, bottom - text_height - 5), name, fill=(255, 255, 255, 255))\n\n\n# Remove the drawing library from memory as per the Pillow docs\ndel draw\n\n# Display the resulting image\npil_image2.show()\n```\n\n    \n![DLIB Face Recognition](assets/output_69_1.png)\n    \n\n\n\n```python\nunknown_image3 = face_recognition.load_image_file('faces/test/unknown_03.jpg')\n\n# Find all the faces and face encodings in the unknown image\nfound_faces3 = face_recognition.face_locations(unknown_image3)\nfound_face_encodings3 = face_recognition.face_encodings(unknown_image3, found_faces3)\n\n# Convert the image to a PIL-format image so that we can draw on top of it with the Pillow library\npil_image3 = Image.fromarray(unknown_image3)\n```\n\n\n```python\n# Create a Pillow ImageDraw Draw instance to draw with\ndraw = ImageDraw.Draw(pil_image3)\n\n# Loop through each face found in the unknown image\nfor (top, right, bottom, left), face_encoding in zip(found_faces3, found_face_encodings3):\n    # See if the face is a match for the known face(s)\n    matches = face_recognition.compare_faces(face_encodings, face_encoding)\n\n    name = \"Unknown\"\n\n    # If a match was found in face_encodings, just use the first one.\n    # if True in matches:\n    #     first_match_index = matches.index(True)\n    #     name = image_labels[first_match_index]\n\n    # Or instead, use the known face with the smallest distance to the new face\n    face_distances = face_recognition.face_distance(face_encodings, face_encoding)\n    best_match_index = np.argmin(face_distances)\n    if matches[best_match_index]:\n        name = image_labels[best_match_index]\n\n    # Draw a box around the face using the Pillow module\n    draw.rectangle(((left, top), (right, bottom)), outline=(0, 0, 255))\n\n    # Draw a label with a name below the face\n    text_width, text_height = draw.textsize(name)\n    draw.rectangle(((left, bottom - text_height - 10), (right, bottom)), fill=(0, 0, 255), outline=(0, 0, 255))\n    draw.text((left + 6, bottom - text_height - 5), name, fill=(255, 255, 255, 255))\n\n\n# Remove the drawing library from memory as per the Pillow docs\ndel draw\n\n# Display the resulting image\npil_image3.show()\n```\n\n    \n![DLIB Face Recognition](assets/output_71_1.png)\n    \n\n\n\n```python\nunknown_image4 = face_recognition.load_image_file('faces/test/unknown_04.jpg')\n\n# Find all the faces and face encodings in the unknown image\nfound_faces4 = face_recognition.face_locations(unknown_image4)\nfound_face_encodings4 = face_recognition.face_encodings(unknown_image4, found_faces4)\n\n# Convert the image to a PIL-format image so that we can draw on top of it with the Pillow library\npil_image4 = Image.fromarray(unknown_image4)\n```\n\n\n```python\n# Create a Pillow ImageDraw Draw instance to draw with\ndraw = ImageDraw.Draw(pil_image4)\n\n# Loop through each face found in the unknown image\nfor (top, right, bottom, left), face_encoding in zip(found_faces4, found_face_encodings4):\n    # See if the face is a match for the known face(s)\n    matches = face_recognition.compare_faces(face_encodings, face_encoding)\n\n    name = \"Unknown\"\n\n    # If a match was found in face_encodings, just use the first one.\n    # if True in matches:\n    #     first_match_index = matches.index(True)\n    #     name = image_labels[first_match_index]\n\n    # Or instead, use the known face with the smallest distance to the new face\n    face_distances = face_recognition.face_distance(face_encodings, face_encoding)\n    best_match_index = np.argmin(face_distances)\n    if matches[best_match_index]:\n        name = image_labels[best_match_index]\n\n    # Draw a box around the face using the Pillow module\n    draw.rectangle(((left, top), (right, bottom)), outline=(0, 0, 255))\n\n    # Draw a label with a name below the face\n    text_width, text_height = draw.textsize(name)\n    draw.rectangle(((left, bottom - text_height - 10), (right, bottom)), fill=(0, 0, 255), outline=(0, 0, 255))\n    draw.text((left + 6, bottom - text_height - 5), name, fill=(255, 255, 255, 255))\n\n\n# Remove the drawing library from memory as per the Pillow docs\ndel draw\n\n# Display the resulting image\npil_image4.show()\n```\n\n    \n![DLIB Face Recognition](assets/output_73_1.png)\n    \n\n\n\n```python\nunknown_image5 = face_recognition.load_image_file('faces/test/unknown_05.jpg')\n\n# Find all the faces and face encodings in the unknown image\nfound_faces5 = face_recognition.face_locations(unknown_image5)\nfound_face_encodings5 = face_recognition.face_encodings(unknown_image5, found_faces5)\n\n# Convert the image to a PIL-format image so that we can draw on top of it with the Pillow library\npil_image5 = Image.fromarray(unknown_image5)\n```\n\n\n```python\n# Create a Pillow ImageDraw Draw instance to draw with\ndraw = ImageDraw.Draw(pil_image5)\n\n# Loop through each face found in the unknown image\nfor (top, right, bottom, left), face_encoding in zip(found_faces5, found_face_encodings5):\n    # See if the face is a match for the known face(s)\n    matches = face_recognition.compare_faces(face_encodings, face_encoding)\n\n    name = \"Unknown\"\n\n    # If a match was found in face_encodings, just use the first one.\n    # if True in matches:\n    #     first_match_index = matches.index(True)\n    #     name = image_labels[first_match_index]\n\n    # Or instead, use the known face with the smallest distance to the new face\n    face_distances = face_recognition.face_distance(face_encodings, face_encoding)\n    best_match_index = np.argmin(face_distances)\n    if matches[best_match_index]:\n        name = image_labels[best_match_index]\n\n    # Draw a box around the face using the Pillow module\n    draw.rectangle(((left, top), (right, bottom)), outline=(0, 0, 255))\n\n    # Draw a label with a name below the face\n    text_width, text_height = draw.textsize(name)\n    draw.rectangle(((left, bottom - text_height - 10), (right, bottom)), fill=(0, 0, 255), outline=(0, 0, 255))\n    draw.text((left + 6, bottom - text_height - 5), name, fill=(255, 255, 255, 255))\n\n\n# Remove the drawing library from memory as per the Pillow docs\ndel draw\n\n# Display the resulting image\npil_image5.show()\n```\n\n\n    \n![DLIB Face Recognition](assets/output_75_1.png)\n    \n\n\n\n```python\nunknown_image6 = face_recognition.load_image_file('faces/test/unknown_06.jpg')\n\n# Find all the faces and face encodings in the unknown image\nfound_faces6 = face_recognition.face_locations(unknown_image6)\nfound_face_encodings6 = face_recognition.face_encodings(unknown_image6, found_faces6)\n\n# Convert the image to a PIL-format image so that we can draw on top of it with the Pillow library\npil_image6 = Image.fromarray(unknown_image6)\n```\n\n\n```python\n# Create a Pillow ImageDraw Draw instance to draw with\ndraw = ImageDraw.Draw(pil_image6)\n\n# Loop through each face found in the unknown image\nfor (top, right, bottom, left), face_encoding in zip(found_faces6, found_face_encodings6):\n    # See if the face is a match for the known face(s)\n    matches = face_recognition.compare_faces(face_encodings, face_encoding)\n\n    name = \"Unknown\"\n\n    # If a match was found in face_encodings, just use the first one.\n    # if True in matches:\n    #     first_match_index = matches.index(True)\n    #     name = image_labels[first_match_index]\n\n    # Or instead, use the known face with the smallest distance to the new face\n    face_distances = face_recognition.face_distance(face_encodings, face_encoding)\n    best_match_index = np.argmin(face_distances)\n    if matches[best_match_index]:\n        name = image_labels[best_match_index]\n\n    # Draw a box around the face using the Pillow module\n    draw.rectangle(((left, top), (right, bottom)), outline=(0, 0, 255))\n\n    # Draw a label with a name below the face\n    text_width, text_height = draw.textsize(name)\n    draw.rectangle(((left, bottom - text_height - 10), (right, bottom)), fill=(0, 0, 255), outline=(0, 0, 255))\n    draw.text((left + 6, bottom - text_height - 5), name, fill=(255, 255, 255, 255))\n\n\n# Remove the drawing library from memory as per the Pillow docs\ndel draw\n\n# Display the resulting image\npil_image6.show()\n```\n\n    \n![DLIB Face Recognition](assets/output_77_1.png)\n    \n\n\n## Save Feature Vector\n\nExport encodings and labels to use them in a Flask App -\u003e see `./main.py`\n\n\n```python\nwith open('features/face_encodings.npy', 'wb') as f:\n    np.save(f, face_encodings)\n\nwith open('features/image_labels.npy', 'wb') as f:\n    np.save(f, image_labels)\n```\n\n\n```python\nwith open('features/face_encodings.npy', 'rb') as f:\n    feature_vectors = np.load(f)\n\nwith open('features/image_labels.npy', 'rb') as f:\n    feature_labels = np.load(f)\n```\n\n\n```python\nnp.unique(feature_labels, return_counts=True)\n```\n\n\n\n\n    (array(['amos_burton', 'bobbie_w_draper', 'camina_drummer',\n            'chrisjen_avasarala', 'jim_holden', 'naomi_nagata'], dtype='\u003cU18'),\n     array([10, 10, 10, 10, 10, 10]))\n\n\n\n\n```python\nfeature_vectors.shape\n```\n\n\n\n\n    (60, 128)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmpolinowski%2Fsimple-face-recognition","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmpolinowski%2Fsimple-face-recognition","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmpolinowski%2Fsimple-face-recognition/lists"}