!pip3 install face_recognition
Collecting face_recognition
Downloading face_recognition-1.3.0-py2.py3-none-any.whl (15 kB)
Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from face_recognition) (1.19.5)
Requirement already satisfied: Pillow in /usr/local/lib/python3.7/dist-packages (from face_recognition) (7.1.2)
Requirement already satisfied: dlib>=19.7 in /usr/local/lib/python3.7/dist-packages (from face_recognition) (19.18.0)
Collecting face-recognition-models>=0.3.0
Downloading face_recognition_models-0.3.0.tar.gz (100.1 MB)
|████████████████████████████████| 100.1 MB 9.0 kB/s
Requirement already satisfied: Click>=6.0 in /usr/local/lib/python3.7/dist-packages (from face_recognition) (7.1.2)
Building wheels for collected packages: face-recognition-models
Building wheel for face-recognition-models (setup.py) ... done
Created wheel for face-recognition-models: filename=face_recognition_models-0.3.0-py2.py3-none-any.whl size=100566184 sha256=2d16298f573e0ec3410447487d116cbae65405f1d7a1e0e0d965197fafc9e1f5
Stored in directory: /root/.cache/pip/wheels/d6/81/3c/884bcd5e1c120ff548d57c2ecc9ebf3281c9a6f7c0e7e7947a
Successfully built face-recognition-models
Installing collected packages: face-recognition-models, face-recognition
Successfully installed face-recognition-1.3.0 face-recognition-models-0.3.0
import cv2
import numpy as np
import face_recognition
import matplotlib.pyplot as plt
luis = cv2.cvtColor(cv2.imread('pp.jpeg'), cv2.COLOR_BGR2RGB)
luis2 = cv2.cvtColor(cv2.imread('luis2.jpeg'), cv2.COLOR_BGR2RGB)
luis3 = cv2.cvtColor(cv2.imread('luis3.jpg'), cv2.COLOR_BGR2RGB)
maria = cv2.cvtColor(cv2.imread('maria.jpeg'), cv2.COLOR_BGR2RGB)
jaci = cv2.cvtColor(cv2.imread('saci.jpeg'), cv2.COLOR_BGR2RGB)
def show_img(img):
if type(img) is list:
tam = len(img)
if tam % 3 == 0:
lin, col = tam//3, 3
elif tam < 3:
lin, col = 1, tam
else:
lin, col = (tam//3) + 1, 3
fig, axes = plt.subplots(lin, col)
for i in range(lin):
for j in range(col):
if lin != 1:
axes[i, j].imshow(img[tam-1])
axes[i, j].axis('off')
else:
axes[j].imshow(img[tam-1])
axes[j].axis('off')
tam -= 1
else:
plt.imshow(img)
plt.axis('off')
plt.show()
show_img(luis)
show_img(maria)
show_img(jaci)
luis
array([[[54, 55, 76],
[54, 55, 76],
[54, 55, 75],
...,
[29, 33, 44],
[29, 33, 44],
[29, 33, 44]],
[[54, 55, 76],
[54, 55, 76],
[54, 55, 75],
...,
[29, 33, 44],
[29, 33, 44],
[29, 33, 44]],
[[54, 55, 76],
[54, 55, 76],
[54, 55, 75],
...,
[29, 33, 44],
[29, 33, 44],
[29, 33, 44]],
...,
[[31, 31, 81],
[31, 31, 81],
[31, 31, 81],
...,
[22, 24, 62],
[22, 24, 62],
[22, 24, 62]],
[[31, 31, 81],
[31, 31, 81],
[31, 31, 81],
...,
[22, 24, 62],
[22, 24, 62],
[22, 24, 62]],
[[31, 31, 81],
[31, 31, 81],
[31, 31, 81],
...,
[22, 24, 62],
[22, 24, 62],
[22, 24, 62]]], dtype=uint8)
top_luis, right_luis, botton_luis, left_luis = face_recognition.face_locations(luis)[0]
luis_face = luis[top_luis:botton_luis, left_luis:right_luis]
show_img(luis_face)
jaci_faces = [jaci[pessoa[0]:pessoa[2], pessoa[3]:pessoa[1]] for pessoa in face_recognition.face_locations(jaci,model='cnn')]
jaci_faces
[array([[[ 54, 45, 40],
[ 48, 40, 37],
[ 48, 40, 37],
...,
[ 66, 52, 41],
[103, 93, 81],
[181, 173, 160]],
[[ 49, 41, 38],
[ 40, 35, 32],
[ 41, 36, 33],
...,
[ 61, 45, 32],
[ 89, 77, 63],
[166, 157, 142]],
[[ 45, 37, 34],
[ 39, 35, 34],
[ 39, 35, 34],
...,
[ 61, 43, 29],
[ 74, 61, 45],
[145, 133, 117]],
...,
[[198, 190, 171],
[199, 189, 164],
[196, 182, 155],
...,
[199, 186, 169],
[197, 185, 169],
[197, 188, 171]],
[[198, 190, 171],
[196, 185, 165],
[198, 183, 160],
...,
[197, 186, 168],
[198, 186, 170],
[198, 189, 172]],
[[199, 187, 171],
[194, 181, 164],
[195, 178, 160],
...,
[194, 183, 165],
[195, 186, 169],
[199, 190, 173]]], dtype=uint8), array([[[ 40, 31, 22],
[ 43, 33, 24],
[ 49, 36, 28],
...,
[ 69, 50, 43],
[ 66, 49, 42],
[ 52, 37, 32]],
[[ 42, 33, 24],
[ 46, 36, 27],
[ 52, 39, 31],
...,
[ 69, 49, 40],
[ 70, 53, 46],
[ 54, 39, 34]],
[[ 44, 35, 28],
[ 49, 38, 32],
[ 56, 41, 34],
...,
[ 72, 48, 36],
[ 74, 54, 43],
[ 60, 41, 34]],
...,
[[180, 175, 153],
[181, 175, 153],
[184, 173, 155],
...,
[125, 98, 79],
[138, 115, 97],
[161, 142, 125]],
[[180, 175, 153],
[181, 175, 153],
[184, 173, 155],
...,
[133, 105, 84],
[144, 117, 98],
[164, 141, 123]],
[[180, 175, 153],
[181, 175, 153],
[184, 173, 155],
...,
[140, 107, 88],
[147, 118, 100],
[165, 138, 121]]], dtype=uint8), array([[[ 55, 41, 41],
[ 60, 45, 42],
[ 81, 61, 52],
...,
[ 47, 38, 33],
[ 88, 79, 74],
[136, 127, 120]],
[[ 61, 46, 43],
[ 69, 50, 44],
[ 95, 71, 59],
...,
[ 45, 34, 28],
[ 81, 70, 64],
[132, 123, 114]],
[[ 73, 52, 47],
[ 91, 64, 55],
[105, 71, 59],
...,
[ 51, 34, 24],
[ 59, 47, 35],
[103, 93, 81]],
...,
[[161, 147, 121],
[155, 135, 110],
[146, 115, 87],
...,
[173, 152, 131],
[178, 161, 141],
[177, 164, 145]],
[[162, 148, 122],
[158, 138, 113],
[146, 116, 88],
...,
[174, 152, 129],
[178, 162, 139],
[176, 164, 142]],
[[163, 149, 123],
[158, 141, 115],
[146, 119, 90],
...,
[173, 151, 128],
[178, 160, 138],
[176, 164, 142]]], dtype=uint8)]
show_img(jaci_faces)
luis_encoding = face_recognition.face_encodings(luis)
maria_encoding = face_recognition.face_encodings(maria)
jaci_encoding = face_recognition.face_encodings(jaci)
jaci_encoding
[array([-0.08789816, -0.03008584, 0.05577884, -0.08479237, -0.09826313,
-0.01392301, -0.04553 , -0.04432406, 0.16222697, -0.16831239,
0.21675977, -0.06455759, -0.26409975, -0.06895885, -0.02413547,
0.08129043, -0.09776334, -0.13081568, -0.02729555, -0.06535068,
0.10309048, 0.00427369, 0.01869318, 0.03152769, -0.15030384,
-0.37315124, -0.11969478, -0.08970489, -0.10858388, -0.04745936,
0.01135334, 0.12085398, -0.13257562, -0.05890212, 0.0155109 ,
0.11249921, -0.0502039 , 0.01817897, 0.15492237, -0.01191158,
-0.17339507, -0.0520844 , -0.01500951, 0.19553053, 0.11095989,
0.09744856, -0.02944122, -0.01291144, 0.09520975, -0.3043811 ,
0.0262799 , 0.17502514, 0.0781436 , 0.01329285, 0.11845125,
-0.06061999, 0.03111755, 0.17218225, -0.22580026, 0.06154964,
-0.03090296, -0.13117163, -0.03710995, -0.07222022, 0.17140238,
0.14212659, -0.10942058, -0.13587227, 0.18133111, -0.12978035,
-0.0773065 , 0.13342172, -0.12608851, -0.25879592, -0.19692501,
0.03599323, 0.41323006, 0.13033827, -0.11024022, 0.02821863,
-0.03062518, -0.0295788 , 0.08164817, -0.01851358, -0.03959596,
-0.00372549, -0.07835146, 0.03129553, 0.15963867, -0.03614672,
-0.08372416, 0.23265146, -0.06060647, 0.13707572, 0.07050374,
0.0205676 , -0.00797683, 0.0601842 , -0.15809734, 0.03113601,
0.10352223, -0.09715842, -0.01743142, 0.01078988, -0.09679279,
0.08262731, -0.06886124, -0.00655837, -0.03791344, -0.01277207,
-0.20241719, 0.0107207 , 0.18024632, -0.31785047, 0.17959107,
0.18171906, 0.01604836, 0.14581639, 0.09626773, -0.03268584,
0.04483093, -0.13065627, -0.22195184, -0.09247123, 0.12773517,
-0.01245836, 0.03142244, -0.01746618]),
array([-0.17248318, 0.01910097, 0.07620397, -0.04722053, -0.10697562,
-0.06971901, -0.11840478, -0.00927445, 0.11978191, -0.08741194,
0.21459866, -0.03439255, -0.15668957, -0.12199581, -0.01262255,
0.11328019, -0.14846395, -0.08782988, -0.01097975, -0.05628571,
0.04696027, -0.00611556, 0.03307581, 0.04094208, -0.10302443,
-0.44459221, -0.11878264, -0.12957731, 0.01512598, -0.11207368,
0.00602638, 0.09480824, -0.14865533, -0.05008914, -0.00215437,
0.15426557, -0.01102754, -0.05104135, 0.20004106, 0.02231458,
-0.12063141, -0.06819689, -0.01268589, 0.27205175, 0.1545516 ,
0.02556258, 0.10967417, -0.04490918, 0.14368869, -0.17554897,
0.01621157, 0.17563945, 0.10262311, 0.03665211, 0.08580529,
-0.07804353, 0.08685544, 0.15521318, -0.20761815, 0.0784117 ,
-0.00789883, -0.0910659 , -0.02588584, 0.00682246, 0.26119995,
0.10531088, -0.07927739, -0.12727517, 0.19802259, -0.1282862 ,
-0.02160498, 0.07916938, -0.1451994 , -0.21507898, -0.25979921,
0.01867936, 0.30835816, 0.1095635 , -0.20604113, 0.04454224,
-0.08031599, -0.07249617, 0.09265293, 0.13416588, -0.06788277,
0.03992527, -0.1224382 , 0.01769789, 0.21235727, -0.00256675,
0.00958643, 0.19979134, 0.04521942, 0.04563854, 0.07755884,
-0.00848406, -0.07454862, -0.03239892, -0.11251722, -0.02759354,
0.06053174, -0.10706446, -0.05366674, 0.03573869, -0.16556555,
0.09730703, -0.02664138, -0.0342816 , -0.08065936, -0.08468435,
-0.0933824 , -0.04194911, 0.17880546, -0.22630593, 0.14753538,
0.20304613, 0.03736237, 0.20604199, 0.0794629 , 0.05855465,
-0.02274376, -0.10693853, -0.10988022, -0.01225821, 0.02534753,
0.00899873, 0.00305905, -0.01070225]),
array([-1.33697242e-01, 5.69382161e-02, 6.16797321e-02, -3.95115912e-02,
-6.88795298e-02, -3.85505185e-02, -5.22825643e-02, -1.34167045e-01,
1.40570775e-01, -1.42648727e-01, 3.10363859e-01, -5.71532585e-02,
-2.22622097e-01, -1.38205603e-01, -5.68449683e-02, 1.35639027e-01,
-2.06858575e-01, -1.17772281e-01, -3.01488359e-02, 5.22938650e-03,
4.73824181e-02, -7.84698501e-02, 6.89284801e-02, 4.00961637e-02,
-1.09398857e-01, -4.20470715e-01, -1.31024778e-01, -1.49145722e-01,
9.41003934e-02, -4.04903032e-02, -4.85925041e-02, 5.98356202e-02,
-1.32113099e-01, -7.33796321e-03, -2.06326246e-02, 7.45286047e-02,
-6.01836629e-02, -3.26915123e-02, 2.53326595e-01, -3.35257836e-02,
-1.80469096e-01, -4.38587703e-02, 3.60717252e-03, 2.12387368e-01,
2.07693830e-01, 5.46448529e-02, 4.27515544e-02, -1.45225972e-02,
7.50094950e-02, -2.57929265e-01, 5.99716380e-02, 1.47969902e-01,
1.49257511e-01, 4.37504239e-02, 1.16590172e-01, -1.41455501e-01,
-4.36283089e-03, 1.20123208e-01, -1.90207005e-01, 5.74368499e-02,
-2.95000449e-02, -7.65466094e-02, 2.16772873e-03, 2.90200114e-04,
2.40884155e-01, 1.69049025e-01, -1.06633320e-01, -8.83657038e-02,
2.20678359e-01, -1.72088206e-01, -2.48931721e-03, 7.08062649e-02,
-1.39050618e-01, -2.04667315e-01, -2.40567803e-01, 1.03026978e-03,
3.79309058e-01, 1.39059588e-01, -1.11794412e-01, 1.36926454e-02,
-2.95514837e-02, -9.33710635e-02, 6.89372718e-02, 3.46344784e-02,
-1.23024710e-01, -3.03623900e-02, -8.26134458e-02, 1.29846148e-02,
1.81944042e-01, -3.79813612e-02, -2.62947865e-02, 2.17180699e-01,
-6.30481392e-02, 6.36897087e-02, 3.88563052e-02, -3.79628688e-02,
-4.32349183e-03, 8.00616667e-03, -9.92226675e-02, 3.09158303e-02,
3.82405296e-02, -1.27473921e-01, -1.33198909e-02, 2.79244222e-02,
-1.62297472e-01, 2.18025185e-02, -2.59035323e-02, -1.55940913e-02,
-2.14551166e-02, -1.47703830e-02, -1.72131732e-01, 5.23836166e-03,
1.87583864e-01, -2.68002003e-01, 2.69288301e-01, 2.09022194e-01,
5.07867187e-02, 1.31319076e-01, 1.07708253e-01, -2.32751481e-02,
3.71268727e-02, -9.27271843e-02, -1.34754315e-01, -5.23514524e-02,
8.06027800e-02, -7.55971596e-02, 6.07303604e-02, -1.26052834e-03])]
is_luis = face_recognition.compare_faces(maria_encoding, luis_encoding[0])
is_luis
[False]
luis_and_maria_distance = face_recognition.face_distance(maria_encoding, luis_encoding[0])
luis_and_maria_distance
array([0.78939632])
luis_esta = face_recognition.compare_faces(jaci_encoding, luis_encoding[0])
luis_esta
[False, True, False]
for i, comp in enumerate(luis_esta):
if comp:
show_img([luis_face, jaci_faces[i]])
face_recognition.face_distance(luis_encoding, jaci_encoding[1])
array([0.56834275])
luis2_encoding = face_recognition.face_encodings(luis2)
is_luis = face_recognition.compare_faces(luis_encoding, luis2_encoding[0])
is_luis
[True]
top_luis2, right_luis2, botton_luis2, left_luis2 = face_recognition.face_locations(luis2, model='cnn')[0]
luis2_face = luis2[top_luis2:botton_luis2, left_luis2:right_luis2]
for i, comp in enumerate(is_luis):
if comp:
show_img([luis_face, luis2_face])
luis3_encoding = face_recognition.face_encodings(luis3)
luis_denovo = face_recognition.compare_faces(luis_encoding, luis3_encoding[0])
luis_denovo
[True]
top_luis3, right_luis3, botton_luis3, left_luis3 = face_recognition.face_locations(luis3, model='cnn')[0]
luis3_face = luis3[top_luis3:botton_luis3, left_luis3:right_luis3]
for i, comp in enumerate(is_luis):
if comp:
show_img([luis_face, luis3_face])
landmark_luis = face_recognition.face_landmarks(luis_face)[0]
landmark_luis
{'bottom_lip': [(116, 146),
(103, 149),
(90, 147),
(80, 145),
(72, 143),
(58, 140),
(42, 131),
(49, 132),
(75, 134),
(83, 135),
(92, 137),
(111, 144)],
'chin': [(1, 56),
(-1, 78),
(-1, 100),
(-2, 123),
(-1, 145),
(7, 165),
(23, 178),
(44, 185),
(71, 188),
(97, 188),
(119, 184),
(138, 176),
(151, 165),
(160, 148),
(166, 130),
(171, 111),
(176, 92)],
'left_eye': [(38, 46), (49, 40), (60, 41), (70, 49), (59, 50), (48, 49)],
'left_eyebrow': [(20, 29), (34, 16), (54, 12), (74, 15), (91, 25)],
'nose_bridge': [(102, 44), (102, 55), (101, 66), (100, 77)],
'nose_tip': [(73, 94), (83, 98), (93, 102), (103, 102), (111, 100)],
'right_eye': [(122, 56),
(133, 51),
(145, 54),
(152, 63),
(143, 64),
(132, 61)],
'right_eyebrow': [(120, 29), (137, 26), (153, 28), (167, 37), (172, 53)],
'top_lip': [(42, 131),
(62, 127),
(78, 125),
(86, 129),
(95, 129),
(106, 136),
(116, 146),
(111, 144),
(93, 138),
(84, 137),
(76, 134),
(49, 132)]}
for mark in landmark_luis.keys():
for point in landmark_luis[mark]:
cv2.circle(luis_face, point, 1, (0, 255, 0), -1)
show_img(luis_face)