Face Recognition sample code in Python

import face_recognition
import PIL.Image
import PIL.ImageDraw
from pathlib import Path

for filename in Path("./").glob("*.jpg"):

    # Load the jpg files into numpy arrays
    image = face_recognition.load_image_file(filename)

    # Generate the face encodings
    face_encodings = face_recognition.face_encodings(image)

    if len(face_encodings) == 0:
        # No faces found in the image.
        print("No faces were found in {}.".format(filename))

    else:
        # Grab the first face encoding
        first_face_encoding = face_encodings[0]

        # Print the results
        print("Face encoding for {}\n".format(filename),first_face_encoding)
        # Find all facial features in all the faces in the image
        face_landmarks_list = face_recognition.face_landmarks(image)

        number_of_faces = len(face_landmarks_list)
        print("I found {} face(s) in this photograph.".format(number_of_faces))

        # Load the image into a Python Image Library object so that we can draw on top of it and display it
        pil_image = PIL.Image.fromarray(image)

        # Create a PIL drawing object to be able to draw lines later
        draw = PIL.ImageDraw.Draw(pil_image)

        # Loop over each face
        for face_landmarks in face_landmarks_list:

            # Loop over each facial feature (eye, nose, mouth, lips, etc)
            for name, list_of_points in face_landmarks.items():
                # Print the location of each facial feature in this image
                print("The {} in this face has the following points: {}".format(name, list_of_points))

                # Let's trace out each facial feature in the image with a line!
                draw.line(list_of_points, fill="red", width=2)

        pil_image.show()
Source: Wikipedia
Face encoding for 少女時代.jpg
 [ 0.00280786  0.04952031  0.026659   -0.10861826 -0.17135668  0.01904625
 -0.11755726 -0.06806283  0.1412486  -0.15165474  0.18089642 -0.16080858
 -0.21458767  0.05857671 -0.06703646  0.22791716 -0.14075357 -0.08185396
 -0.03164606 -0.02500833  0.02726686  0.06292275 -0.02887238  0.05016224
 -0.07993153 -0.29592544 -0.0707741  -0.03700335 -0.04502663 -0.10061727
 -0.09053811  0.04664797 -0.14854443  0.00317795  0.05002183  0.04636773
 -0.03239224 -0.11983427  0.1343635   0.02443757 -0.23627587  0.00571186
  0.10522461  0.18201745  0.17516077  0.05142709 -0.03043179 -0.14990571
  0.09647369 -0.20674658  0.03496456  0.07886875  0.05340976  0.03410742
  0.00878919 -0.16121371  0.00705353  0.21608526 -0.13619703  0.00877173
  0.07228769 -0.03856369 -0.01120605 -0.11059319  0.2701869   0.11101417
 -0.10498714 -0.18699118  0.10967363 -0.11973584 -0.09845779  0.06239281
 -0.11637798 -0.19572417 -0.2599822  -0.01245424  0.33891317  0.10544451
 -0.18954752  0.01627849  0.00198444 -0.02051603  0.13760076  0.11457649
  0.03677165 -0.09055617 -0.04983162 -0.03842438  0.27888617 -0.11553628
  0.0078914   0.1742713  -0.03833457  0.01251387 -0.06437807  0.00966834
 -0.10598457  0.06969929 -0.11915567 -0.04802158 -0.01132921 -0.01886535
 -0.05262694  0.15786843 -0.24415033  0.11756948 -0.01041824 -0.07494211
  0.0261571  -0.01704325 -0.049513   -0.03705477  0.17593864 -0.25121033
  0.14738104  0.16808799  0.11185423  0.13825981  0.06301515  0.08305022
  0.04364685 -0.02754669 -0.25275427 -0.0228281   0.1033156  -0.05074894
  0.03387671  0.01970361]
I found 8 face(s) in this photograph.
The chin in this face has the following points: [(832, 153), (833, 168), (835, 183), (839, 196), (847, 207), (857, 216), (869, 223), (881, 226), (891, 226), (898, 221), (902, 212), (908, 204), (914, 194), (918, 185), (920, 175), (920, 165), (919, 155)]
The left_eyebrow in this face has the following points: [(857, 150), (865, 146), (874, 144), (883, 144), (892, 146)]
The right_eyebrow in this face has the following points: [(903, 145), (907, 143), (913, 141), (917, 141), (920, 144)]
The nose_bridge in this face has the following points: [(898, 156), (899, 164), (901, 171), (902, 179)]
The nose_tip in this face has the following points: [(890, 184), (894, 185), (898, 186), (902, 185), (904, 183)]
The left_eye in this face has the following points: [(868, 156), (875, 153), (881, 153), (885, 158), (880, 159), (874, 158)]
The right_eye in this face has the following points: [(903, 156), (907, 152), (911, 151), (914, 154), (912, 156), (908, 157)]
The top_lip in this face has the following points: [(876, 198), (885, 194), (892, 193), (896, 194), (900, 193), (903, 193), (904, 196), (902, 196), (899, 195), (896, 196), (892, 196), (879, 197)]
The bottom_lip in this face has the following points: [(904, 196), (902, 202), (899, 206), (895, 208), (891, 208), (884, 205), (876, 198), (879, 197), (892, 203), (895, 203), (899, 201), (902, 196)]
The chin in this face has the following points: [(907, 546), (909, 559), (911, 572), (916, 584), (924, 593), (933, 601), (943, 608), (954, 611), (964, 611), (973, 607), (980, 599), (985, 589), (990, 579), (993, 568), (994, 555), (994, 543), (993, 530)]
The left_eyebrow in this face has the following points: [(916, 534), (922, 530), (930, 528), (938, 528), (946, 530)]
The right_eyebrow in this face has the following points: [(960, 528), (967, 523), (974, 520), (982, 519), (988, 523)]
The nose_bridge in this face has the following points: [(954, 541), (956, 550), (957, 558), (959, 567)]
The nose_tip in this face has the following points: [(949, 572), (954, 573), (959, 574), (964, 571), (968, 569)]
The left_eye in this face has the following points: [(926, 543), (931, 540), (937, 539), (942, 543), (937, 544), (931, 544)]
The right_eye in this face has the following points: [(966, 540), (971, 535), (977, 533), (981, 534), (978, 537), (972, 539)]
The top_lip in this face has the following points: [(938, 583), (945, 581), (954, 580), (960, 580), (965, 578), (972, 576), (978, 575), (976, 577), (966, 580), (960, 582), (955, 582), (940, 583)]
The bottom_lip in this face has the following points: [(978, 575), (975, 589), (968, 597), (962, 599), (956, 599), (946, 595), (938, 583), (940, 583), (955, 593), (961, 593), (967, 590), (976, 577)]
The chin in this face has the following points: [(664, 136), (663, 148), (662, 161), (664, 173), (670, 183), (678, 191), (687, 198), (697, 203), (706, 205), (713, 201), (718, 194), (722, 185), (727, 177), (732, 170), (737, 162), (737, 153), (736, 145)]
The left_eyebrow in this face has the following points: [(687, 128), (694, 123), (702, 121), (711, 122), (718, 126)]
The right_eyebrow in this face has the following points: [(727, 128), (731, 126), (734, 126), (737, 127), (739, 130)]
The nose_bridge in this face has the following points: [(722, 138), (723, 145), (724, 153), (725, 161)]
The nose_tip in this face has the following points: [(712, 164), (716, 166), (720, 167), (724, 166), (726, 165)]
The left_eye in this face has the following points: [(696, 136), (701, 133), (706, 134), (709, 138), (705, 138), (700, 137)]
The right_eye in this face has the following points: [(725, 139), (729, 136), (733, 136), (734, 139), (732, 140), (729, 140)]
The top_lip in this face has the following points: [(693, 170), (704, 171), (713, 172), (717, 173), (721, 172), (723, 173), (723, 173), (722, 174), (720, 175), (717, 175), (712, 174), (695, 171)]
The bottom_lip in this face has the following points: [(723, 173), (721, 179), (717, 183), (714, 184), (709, 183), (701, 180), (693, 170), (695, 171), (711, 179), (715, 180), (719, 179), (722, 174)]
The chin in this face has the following points: [(89, 150), (87, 166), (84, 183), (85, 197), (92, 211), (103, 221), (116, 230), (129, 237), (140, 242), (150, 241), (156, 234), (161, 224), (168, 215), (174, 207), (179, 198), (181, 187), (180, 178)]
The left_eyebrow in this face has the following points: [(130, 149), (141, 147), (152, 149), (161, 153), (168, 158)]
The right_eyebrow in this face has the following points: [(175, 163), (178, 163), (181, 164), (184, 165), (185, 167)]
The nose_bridge in this face has the following points: [(170, 170), (171, 178), (173, 186), (175, 194)]
The nose_tip in this face has the following points: [(157, 197), (162, 199), (166, 202), (169, 202), (172, 201)]
The left_eye in this face has the following points: [(137, 158), (145, 158), (150, 160), (153, 166), (148, 165), (143, 162)]
The right_eye in this face has the following points: [(171, 173), (175, 171), (178, 172), (179, 177), (177, 177), (173, 175)]
The top_lip in this face has the following points: [(139, 207), (151, 206), (160, 206), (163, 209), (166, 209), (166, 213), (162, 218), (160, 217), (163, 213), (161, 212), (158, 210), (142, 208)]
The bottom_lip in this face has the following points: [(162, 218), (160, 221), (158, 222), (155, 221), (152, 219), (146, 214), (139, 207), (142, 208), (156, 213), (158, 214), (161, 215), (160, 217)]
The chin in this face has the following points: [(1090, 551), (1093, 562), (1095, 573), (1099, 583), (1105, 592), (1113, 600), (1122, 608), (1132, 615), (1144, 616), (1154, 613), (1163, 604), (1169, 594), (1174, 583), (1178, 572), (1178, 559), (1177, 546), (1176, 534)]
The left_eyebrow in this face has the following points: [(1095, 547), (1099, 541), (1106, 538), (1114, 538), (1122, 540)]
The right_eyebrow in this face has the following points: [(1136, 537), (1144, 533), (1152, 530), (1160, 530), (1166, 534)]
The nose_bridge in this face has the following points: [(1131, 548), (1132, 556), (1133, 564), (1135, 572)]
The nose_tip in this face has the following points: [(1126, 576), (1131, 577), (1136, 577), (1141, 575), (1146, 572)]
The left_eye in this face has the following points: [(1104, 551), (1109, 549), (1114, 548), (1120, 551), (1114, 552), (1109, 552)]
The right_eye in this face has the following points: [(1144, 548), (1148, 543), (1154, 542), (1159, 543), (1155, 545), (1149, 547)]
The top_lip in this face has the following points: [(1120, 586), (1125, 582), (1133, 581), (1138, 581), (1144, 579), (1152, 577), (1159, 578), (1157, 579), (1145, 582), (1139, 584), (1133, 585), (1122, 586)]
The bottom_lip in this face has the following points: [(1159, 578), (1154, 590), (1147, 596), (1141, 598), (1135, 598), (1127, 596), (1120, 586), (1122, 586), (1134, 593), (1140, 593), (1146, 590), (1157, 579)]
The chin in this face has the following points: [(1269, 227), (1272, 237), (1275, 247), (1280, 257), (1287, 265), (1296, 271), (1305, 277), (1314, 282), (1324, 283), (1334, 280), (1342, 273), (1350, 265), (1356, 255), (1359, 243), (1359, 231), (1358, 218), (1357, 205)]
The left_eyebrow in this face has the following points: [(1270, 217), (1273, 210), (1280, 207), (1288, 207), (1295, 209)]
The right_eyebrow in this face has the following points: [(1310, 205), (1317, 201), (1326, 198), (1334, 197), (1342, 200)]
The nose_bridge in this face has the following points: [(1305, 218), (1306, 226), (1308, 234), (1309, 242)]
The nose_tip in this face has the following points: [(1303, 247), (1308, 248), (1312, 248), (1316, 246), (1320, 244)]
The left_eye in this face has the following points: [(1279, 223), (1284, 220), (1290, 219), (1295, 223), (1290, 225), (1284, 225)]
The right_eye in this face has the following points: [(1319, 218), (1323, 212), (1329, 210), (1335, 212), (1331, 215), (1325, 217)]
The top_lip in this face has the following points: [(1299, 257), (1305, 255), (1310, 254), (1314, 254), (1318, 252), (1327, 252), (1337, 250), (1334, 251), (1319, 255), (1315, 256), (1311, 257), (1302, 257)]
The bottom_lip in this face has the following points: [(1337, 250), (1329, 257), (1322, 261), (1318, 262), (1313, 262), (1307, 260), (1299, 257), (1302, 257), (1312, 257), (1316, 257), (1320, 256), (1334, 251)]
The chin in this face has the following points: [(495, 525), (490, 539), (487, 554), (486, 569), (490, 582), (498, 595), (508, 606), (518, 615), (529, 621), (538, 621), (544, 614), (551, 605), (558, 598), (566, 589), (573, 581), (575, 572), (577, 563)]
The left_eyebrow in this face has the following points: [(527, 527), (537, 526), (547, 528), (555, 534), (562, 541)]
The right_eyebrow in this face has the following points: [(572, 547), (576, 547), (580, 548), (583, 549), (584, 553)]
The nose_bridge in this face has the following points: [(565, 555), (565, 562), (565, 568), (565, 576)]
The nose_tip in this face has the following points: [(548, 576), (552, 580), (555, 583), (559, 583), (562, 582)]
The left_eye in this face has the following points: [(534, 540), (542, 540), (547, 542), (550, 549), (544, 547), (539, 545)]
The right_eye in this face has the following points: [(565, 558), (571, 556), (575, 558), (577, 562), (574, 563), (569, 561)]
The top_lip in this face has the following points: [(526, 581), (538, 583), (547, 586), (551, 588), (554, 589), (556, 591), (555, 594), (553, 594), (552, 592), (549, 591), (545, 588), (528, 582)]
The bottom_lip in this face has the following points: [(555, 594), (550, 601), (546, 603), (542, 602), (538, 600), (532, 593), (526, 581), (528, 582), (542, 595), (546, 597), (549, 597), (553, 594)]
The chin in this face has the following points: [(353, 156), (356, 167), (359, 177), (365, 187), (372, 196), (381, 203), (389, 209), (399, 215), (408, 215), (417, 211), (425, 204), (432, 195), (438, 184), (441, 172), (441, 159), (441, 147), (440, 134)]
The left_eyebrow in this face has the following points: [(356, 146), (361, 142), (367, 140), (374, 140), (381, 141)]
The right_eyebrow in this face has the following points: [(393, 137), (401, 133), (409, 129), (418, 128), (426, 131)]
The nose_bridge in this face has the following points: [(390, 149), (391, 157), (392, 166), (394, 174)]
The nose_tip in this face has the following points: [(388, 179), (392, 179), (397, 179), (401, 177), (404, 175)]
The left_eye in this face has the following points: [(366, 155), (370, 153), (376, 152), (380, 153), (376, 155), (371, 156)]
The right_eye in this face has the following points: [(403, 148), (407, 145), (412, 143), (418, 143), (414, 146), (409, 148)]
The top_lip in this face has the following points: [(387, 192), (391, 189), (395, 188), (398, 188), (402, 186), (408, 186), (416, 185), (414, 186), (403, 188), (399, 190), (396, 190), (389, 192)]
The bottom_lip in this face has the following points: [(416, 185), (411, 194), (405, 199), (401, 200), (397, 200), (392, 198), (387, 192), (389, 192), (396, 195), (400, 195), (404, 194), (414, 186)]

Process finished with exit code 0
Facebook Comments