自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

三分鐘帶你入門人臉識別

人工智能 人臉識別
人臉識別是AI研究帶給世界的眾多奇跡之一。對于許多技術(shù)人員來說,這是一個充滿好奇的話題-他們希望對事物的工作方式有基本的了解。讓我們潛入主題,看看事情如何運作。

 人臉識別是AI研究帶給世界的眾多奇跡之一。對于許多技術(shù)人員來說,這是一個充滿好奇的話題-他們希望對事物的工作方式有基本的了解。讓我們潛入主題,看看事情如何運作。

人臉識別

解決此類問題時,最好不要重新發(fā)明輪子-我們不能!最好遵循研究人員提供給我們的模型。開源中也有很多可用的工具。這樣的Python庫之一是face_recognition。它可以通過幾個步驟工作:

  • 識別給定圖像中的人臉
  • 識別面部特征
  • 生成128個值的人臉編碼向量

基于這種編碼,我們可以測量兩個臉部圖像之間的相似度-可以告訴我們它們是否屬于同一個人。

首先安裝face_recognition模塊

  1. pip install face_recognition 

導入模塊
接下來,我們導入所需的模塊

  1. import PIL.Image 
  2. import PIL.ImageDraw 
  3. import requests 
  4. from io import BytesIO 
  5.  
  6. from IPython.display import display 
  7.  
  8. import face_recognition 

載入圖片
接下來,我們加載圖片。我已將圖像存儲在我的Github帳戶中。這樣我們就可以從URL中讀取原始圖像。

  1. response = requests.get("https://raw.githubusercontent.com/solegaonkar/solegaonkar.github.io/master/img/rahul1.jpeg"
  2. fr_image = face_recognition.load_image_file(BytesIO(response.content)) 

識別面孔
加載面部后,讓我們看一下face_recognition模塊的各個部分。它如何識別人臉?

  1. face_locations = face_recognition.face_locations(fr_image) 
  2.  
  3. number_of_faces = len(face_locations) 
  4. print("I found {} face(s) in this photograph.".format(number_of_faces)) 

這給了我們一個輸出:

  1. I found 1 face(s) **in** this photograph.</span> 

這意味著,該算法僅在圖像中找到了一張臉。讓我們看看所識別的圖像和面部。

  1. pil_image = PIL.Image.fromarray(fr_image) 
  2.  
  3. for face_location in face_locations: 
  4.     # Print the location of each face in this image. Each face is a list of co-ordinates in (top, right, bottom, left) order. 
  5.     top, right, bottom, left = face_location 
  6.     print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right)) 
  7.     # Let's draw a box around the face 
  8.     draw = PIL.ImageDraw.Draw(pil_image) 
  9.     draw.rectangle([left, top, right, bottom], outline="black"

這給了我們一個輸出

  1. A face **is** located at pixel location Top: 68, Left: 117, Bottom: 291, Right: 340</span> 

上面的代碼還修改了圖像以在臉部周圍繪制一個矩形。讓我們檢查一下是否運作良好。

已經(jīng)可以了。

人臉編碼
這是我們的面孔。但是,對于我們的算法而言,它只是RGB值的數(shù)組—匹配從我們提供給它的數(shù)據(jù)樣本中學到的模式。

對于面部識別,該算法記錄了面部的某些重要測量值,例如眼睛的顏色和大小和傾斜度,眉毛之間的間隙等。所有這些共同定義了面部編碼-從圖像中獲取的信息-即用于識別特定面孔。

為了了解從面部讀取的內(nèi)容,讓我們看一下讀取的編碼。

  1. face_encodings = face_recognition.face_encodings(fr_image) 
  2. face_encodings[0

打印出一個巨大的數(shù)組:

  1. array([-0.10213576,  0.05088161, -0.03425048, -0.09622347, -0.12966095
  2.         0.04867411, -0.00511892, -0.03418527,  0.2254715 , -0.07892745
  3.         0.21497472, -0.0245543 , -0.2127848 , -0.08542262, -0.00298059
  4.         0.13224372, -0.21870363, -0.09271716, -0.03727289, -0.1250658 , 
  5.         0.09436664,  0.03037129, -0.02634972,  0.02594662, -0.1627259 , 
  6.        -0.29416466, -0.12254384, -0.15237436,  0.14907973, -0.09940194
  7.         0.02000656,  0.04662619, -0.1266906 , -0.11484023,  0.04613583
  8.         0.1228286 , -0.03202137, -0.0715076 ,  0.18478717, -0.01387333
  9.        -0.11409076,  0.07516225,  0.08549548,  0.31538364,  0.1297821 , 
  10.         0.04055009,  0.0346106 , -0.04874525,  0.17533901, -0.22634712
  11.         0.14879328,  0.09331974,  0.17943285,  0.02707857,  0.22914577
  12.        -0.20668915,  0.03964197,  0.17524502, -0.20210043,  0.07155308
  13.         0.04467429,  0.02973968,  0.00257265, -0.00049853,  0.18866715
  14.         0.08767469, -0.06483966, -0.13107982,  0.21610288, -0.04506358
  15.        -0.02243116,  0.05963502, -0.14988004, -0.11296406, -0.30011353
  16.         0.07316103,  0.38660526,  0.07268623, -0.14636359,  0.08436179
  17.         0.01005938, -0.00661338,  0.09306039,  0.03271955, -0.11528577
  18.        -0.0524189 , -0.11697718,  0.07356471,  0.10350288, -0.03610475
  19.         0.00390615,  0.17884226,  0.04291092, -0.02914601,  0.06112404
  20.         0.05315027, -0.14561613, -0.01887275, -0.13125736, -0.0362937 , 
  21.         0.16490118, -0.09027836, -0.00981111,  0.1363602 , -0.23134531
  22.         0.0788044 , -0.00604869, -0.05569676, -0.07010217, -0.0408107 , 
  23.        -0.10358225,  0.08519378,  0.16833456, -0.30366772,  0.17561394
  24.         0.14421709, -0.05016343,  0.13464174,  0.0646335 , -0.0262765 , 
  25.         0.02722404, -0.06028951, -0.19448066, -0.07304715,  0.0204969 , 
  26.        -0.03045784, -0.02818791,  0.06679841]) 

這些數(shù)字中的每一個代表面部編碼的正交分量。

相似
現(xiàn)在讓我們研究下一步-確定面孔之間的相似性。為此,我們需要加載更多圖像。

讓我們首先加載三個圖像。加載圖像時,我們還先找到人臉,再找到人臉編碼。

  1. response = requests.get("https://raw.githubusercontent.com/solegaonkar/solegaonkar.github.io/master/img/rahul1.jpeg"
  2. image_of_person_1 = face_recognition.load_image_file(BytesIO(response.content)) 
  3. face_locations = face_recognition.face_locations(image_of_person_1) 
  4. person_1_face_encoding = face_recognition.face_encodings(image_of_person_1, known_face_locations=face_locations) 
  5.  
  6. response = requests.get("https://raw.githubusercontent.com/solegaonkar/solegaonkar.github.io/master/img/rahul2.jpg"
  7. image_of_person_2 = face_recognition.load_image_file(BytesIO(response.content)) 
  8. face_locations = face_recognition.face_locations(image_of_person_2) 
  9. person_2_face_encoding = face_recognition.face_encodings(image_of_person_2, known_face_locations=face_locations) 
  10.  
  11. response = requests.get("https://raw.githubusercontent.com/solegaonkar/solegaonkar.github.io/master/img/trump.jpg"
  12. image_of_person_3 = face_recognition.load_image_file(BytesIO(response.content)) 
  13. face_locations = face_recognition.face_locations(image_of_person_3) 
  14. person_3_face_encoding = face_recognition.face_encodings(image_of_person_3, known_face_locations=face_locations) 

現(xiàn)在,識別相似性并不困難。face_recognition模塊提供了一個簡單的API。

  1. face_recognition.compare_faces([person_1_face_encoding,person_3_face_encoding], person_2_face_encoding[0], tolerance=0.08

該方法檢查要比較的兩個面的每個分量,并告訴我們當前分量是否在公差范圍內(nèi)變化。上面的命令顯示如下輸出:

  1. [array([ True,  True,  True,  True,  True,  True,  True,  True,  True, 
  2.          True,  True,  True,  True,  True,  True,  True,  True,  True, 
  3.          True,  True,  True,  True,  True,  True,  True,  True,  True, 
  4.          True,  True,  True,  True,  True,  True,  True,  True,  True, 
  5.          True,  True,  True,  True, False,  True,  True,  True,  True, 
  6.          True,  True,  True,  True,  True,  True,  True,  True, False, 
  7.          True,  True,  True,  True,  True,  True,  True,  True,  True, 
  8.          True,  True,  True,  True,  True, False,  True,  True,  True, 
  9.          True,  True,  True,  True,  True,  True,  True,  True, False, 
  10.          True,  True,  True,  True,  True,  True,  True,  True,  True, 
  11.          True,  True,  True, False,  True,  True,  True,  True,  True, 
  12.          True,  True,  True,  True,  True,  True,  True,  True,  True, 
  13.          True,  True,  True,  True,  True,  True,  True,  True,  True, 
  14.          True,  True,  True,  True,  True,  True,  True,  True, False, 
  15.          True,  True]), 
  16.  array([ True,  True,  True,  True,  True,  True, False, False, False, 
  17.          True,  True,  True, False,  True,  True,  True, False,  True, 
  18.         False,  True,  True,  True,  True, False,  True,  True,  True, 
  19.         False,  True,  True,  True, False,  True,  True,  True,  True, 
  20.          True,  True,  True,  True, False,  True, False,  True,  True, 
  21.          True,  True,  True, False,  True, False,  True,  True,  True, 
  22.         False, False,  True,  True,  True,  True,  True, False,  True, 
  23.         False, False, False, False,  True, False,  True, False,  True, 
  24.         False,  True,  True,  True,  True, False,  True,  True,  True, 
  25.          True,  True,  True, False,  True,  True,  True, False,  True, 
  26.          True, False,  True,  True,  True,  True,  True,  True,  True, 
  27.          True,  True,  True,  True,  True, False, False,  True,  True, 
  28.         False, False, False,  True,  True, False,  True,  True,  True, 
  29.          True,  True,  True,  True,  True,  True, False, False,  True, 
  30.          True,  True])] 

這兩個數(shù)組表示給定圖像(在第二個參數(shù)中)與提供的列表(在第一個參數(shù)中)中每個已知面部編碼的相似性。

我們可以看到第一個數(shù)組顯示出更多相似性。可以正確識別該人。

數(shù)碼化妝
如果您喜歡有趣,我們可以使用面部識別庫做更多的事情。我們有一個API,可以幫助我們識別面部的各個特征。

  1. face_landmarks_list = face_recognition.face_landmarks(fr_image) 
  2. print(face_landmarks_list) 

這為我們提供了每個單獨特征曲線的長長列表。

  1. [{ 
  2.     'chin': [(4647), (4554), (4462), (4469), (4477), (4684), (4991), (5495), (6197), (6897), (7695), (8491), (9087), (9481), (9775), (9968), (10160)],  
  3.     'left_eyebrow': [(5142), (5439), (5839), (6340), (6742)],  
  4.     'right_eyebrow': [(7544), (8044), (8644), (9047), (9351)],  
  5.     'nose_bridge': [(7048), (6852), (6756), (6660)],  
  6.     'nose_tip': [(6064), (6265), (6567), (6866), (7166)],  
  7.     'left_eye': [(5547), (5745), (6146), (6348), (6048), (5748)],  
  8.     'right_eye': [(7751), (8050), (8451), (8654), (8354), (7953)],  
  9.     'top_lip': [(5475), (5872), (6172), (6473), (6673), (7075), (7380), (7179), (6675), (6375), (6174), (5675)],  
  10.     'bottom_lip': [(7380), (6881), (6481), (6280), (6080), (5778), (5475), (5675), (6077), (6378), (6578), (7179)] 
  11. }] 

我們可以對此圖像應用數(shù)字化妝。

  1. for face_landmarks in face_landmarks_list: 
  2.     pil_image = PIL.Image.fromarray(fr_image) 
  3.     d = PIL.ImageDraw.Draw(pil_image, 'RGBA'
  4.  
  5.     # Make the eyebrows into a nightmare 
  6.     d.line(face_landmarks['left_eyebrow'], fill=(000255), width=3
  7.     d.line(face_landmarks['right_eyebrow'], fill=(000255), width=3
  8.     d.polygon(face_landmarks['left_eyebrow'], fill=(000255)) 
  9.     d.polygon(face_landmarks['right_eyebrow'], fill=(000255)) 
  10.  
  11.     # Gloss the lips 
  12.     d.line(face_landmarks['top_lip'], fill=(000255), width=10
  13.     d.line(face_landmarks['bottom_lip'], fill=(000255), width=10
  14.  
  15.     d.polygon(face_landmarks['bottom_lip'], fill=(25500255)) 
  16.     d.polygon(face_landmarks['top_lip'], fill=(25500255)) 
  17.     d.line(face_landmarks['top_lip'], fill=(000255), width=2
  18.     d.line(face_landmarks['bottom_lip'], fill=(000255), width=2
  19.  
  20.     # Chin 
  21.     d.polygon(face_landmarks['chin'], fill=(2550016)) 
  22.  
  23.     # Apply some eyeliner 
  24.     d.line(face_landmarks['left_eye'] + [face_landmarks['left_eye'][0]], fill=(1000255), width=6
  25.     d.line(face_landmarks['right_eye'] + [face_landmarks['right_eye'][0]], fill=(1000255), width=6
  26.  
  27.     # Sparkle the eyes 
  28.     d.polygon(face_landmarks['left_eye'], fill=(25500200)) 
  29.     d.polygon(face_landmarks['right_eye'], fill=(25500200)) 
  30.  
  31.     display(pil_image) 

下面是我們得到的。

 

責任編輯:梁菲 來源: Python之眼
相關(guān)推薦

2022-02-17 09:24:11

TypeScript編程語言javaScrip

2024-09-13 08:49:45

2024-05-16 11:13:16

Helm工具release

2021-04-20 13:59:37

云計算

2024-01-16 07:46:14

FutureTask接口用法

2024-08-30 08:50:00

2020-06-30 10:45:28

Web開發(fā)工具

2024-01-12 07:38:38

AQS原理JUC

2024-07-05 09:31:37

2020-03-08 16:45:58

數(shù)據(jù)挖掘學習數(shù)據(jù)量

2017-01-18 15:38:20

語言

2019-12-05 10:00:03

架構(gòu)Redis服務(wù)器

2024-10-15 09:18:30

2024-02-22 07:37:37

對象JVM內(nèi)存

2024-06-06 08:50:43

2009-11-09 12:55:43

WCF事務(wù)

2024-12-18 10:24:59

代理技術(shù)JDK動態(tài)代理

2022-02-21 18:16:38

Go語言枚舉

2023-12-27 08:15:47

Java虛擬線程

2020-11-03 09:20:30

MySQLOracle數(shù)據(jù)庫
點贊
收藏

51CTO技術(shù)棧公眾號