最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python3 利用face_recognition實(shí)現(xiàn)人臉識(shí)別的方法

 更新時(shí)間:2020年03月13日 14:33:45   作者:1、、  
這篇文章主要介紹了Python3 利用face_recognition實(shí)現(xiàn)人臉識(shí)別的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

前言

之前實(shí)踐了下face++在線人臉識(shí)別版本,這回做一下離線版本。github 上面有關(guān)于face_recognition的相關(guān)資料,本人只是做個(gè)搬運(yùn)工,對其中的一些內(nèi)容進(jìn)行搬運(yùn),對其中一些例子進(jìn)行實(shí)現(xiàn)。

官方描述:

face_recognition是一個(gè)強(qiáng)大、簡單、易上手的人臉識(shí)別開源項(xiàng)目,并且配備了完整的開發(fā)文檔和應(yīng)用案例,特別是兼容樹莓派系統(tǒng)。本項(xiàng)目是世界上最簡潔的人臉識(shí)別庫,你可以使用Python和命令行工具提取、識(shí)別、操作人臉。本項(xiàng)目的人臉識(shí)別是基于業(yè)內(nèi)領(lǐng)先的C++開源庫 dlib中的深度學(xué)習(xí)模型,用Labeled Faces in the Wild人臉數(shù)據(jù)集進(jìn)行測試,有高達(dá)99.38%的準(zhǔn)確率。但對小孩和亞洲人臉的識(shí)別準(zhǔn)確率尚待提升。

(關(guān)于兼容樹莓派,以后有板子了再做一下)

下面兩個(gè)鏈接劃重點(diǎn)

https://github.com/ageitgey/face_recognition/blob/master/README_Simplified_Chinese.md
https://face-recognition.readthedocs.io/en/latest/face_recognition.html

環(huán)境配置

  • ubuntu16.04(其他環(huán)境的安裝可以參考第一個(gè)鏈接,官方有說明)
  • pycharm(可忽略,怎么舒服怎么來)
  • python3
  • opencv(我的是4.1.2,三點(diǎn)幾的版本應(yīng)該也一樣)

實(shí)際上只需要安裝face_recognition,當(dāng)然,沒有opencv的也需要安裝一下opencv

pip3 install face_recognition

圖片準(zhǔn)備

由于需要做一些圖片的比對,因此需要準(zhǔn)備一些圖片,本文圖片取自以下鏈接

https://www.zhihu.com/question/314169580/answer/872770507

接下來開始操作

官方還有提供命令行的操作(這個(gè)沒去做),本文不做這個(gè),我們只要是要在python中用face_recognition,因此定位到這一塊。


這個(gè)api文檔地址就是上面的第二個(gè)鏈接。進(jìn)去之后可以看到:

part1.識(shí)別圖片中的人是誰

代碼

# part1
# 識(shí)別圖片中的人是誰
import face_recognition
known_image = face_recognition.load_image_file("lyf1.jpg")
unknown_image = face_recognition.load_image_file("lyf2.jpg")

lyf_encoding = face_recognition.face_encodings(known_image)[0]
unknown_encoding = face_recognition.face_encodings(unknown_image)[0]

results = face_recognition.compare_faces([lyf_encoding], unknown_encoding)	
# A list of True/False values indicating which known_face_encodings match the face encoding to check

print(type(results))
print(results)

if results[0] == True:
  print("yes")
else:
  print("no")

結(jié)果

<class 'list'>
[True]
yes

part2.從圖片中找到人臉

代碼

# part2
# 從圖片中找到人臉(定位人臉位置)

import face_recognition
import cv2

image = face_recognition.load_image_file("lyf1.jpg")

face_locations_useCNN = face_recognition.face_locations(image,model='cnn')
# model – Which face detection model to use. “hog” is less accurate but faster on CPUs.
# “cnn” is a more accurate deep-learning model which is GPU/CUDA accelerated (if available). The default is “hog”.

face_locations_noCNN=face_recognition.face_locations(image)
# A list of tuples of found face locations in css (top, right, bottom, left) order
# 因?yàn)榉祷刂档捻樞蚴沁@樣子的,因此在后面的for循環(huán)里面賦值要注意按這個(gè)順序來

print("face_location_useCNN:")
print(face_locations_useCNN)
face_num1=len(face_locations_useCNN)
print(face_num1)    # The number of faces


print("face_location_noCNN:")
print(face_locations_noCNN)
face_num2=len(face_locations_noCNN)
print(face_num2)    # The number of faces
# 到這里為止,可以觀察兩種情況的坐標(biāo)和人臉數(shù),一般來說,坐標(biāo)會(huì)不一樣,但是檢測出來的人臉數(shù)應(yīng)該是一樣的
# 也就是說face_num1?。健ace_num2; face_locations_useCNN 和 face_locations_noCNN 不一樣


org = cv2.imread("lyf1.jpg")
img = cv2.imread("lyf1.jpg")
cv2.imshow("lyf1.jpg",img) # 原始圖片

# Go to get the data and draw the rectangle
# use CNN
for i in range(0,face_num1):
  top = face_locations_useCNN[i][0]
  right = face_locations_useCNN[i][1]
  bottom = face_locations_useCNN[i][2]
  left = face_locations_useCNN[i][3]

  start = (left, top)
  end = (right, bottom)

  color = (0,255,255)
  thickness = 2
  cv2.rectangle(img, start, end, color, thickness)  # opencv 里面畫矩形的函數(shù)

# Show the result
cv2.imshow("useCNN",img)


# for face_location in face_locations_noCNN:
#
#   # Print the location of each face in this image
#   top, right, bottom, left = face_location
# # 等價(jià)于下面的這種寫法

for i in range(0,face_num2):
  top = face_locations_noCNN[i][0]
  right = face_locations_noCNN[i][1]
  bottom = face_locations_noCNN[i][2]
  left = face_locations_noCNN[i][3]

  start = (left, top)
  end = (right, bottom)

  color = (0,255,255)
  thickness = 2
  cv2.rectangle(org, start, end, color, thickness)

cv2.imshow("no cnn ",org)

cv2.waitKey(0)
cv2.destroyAllWindows()

結(jié)果

face_location_useCNN:
[(223, 470, 427, 266)]
1
face_location_noCNN:
[(242, 489, 464, 266)]
1

圖片效果大致是這樣

part3.找到人臉并將其裁剪打印出來(使用cnn定位人臉)

代碼

# part3
# 找到人臉并將其裁剪打印出來(使用cnn定位人臉)

from PIL import Image
import face_recognition

# Load the jpg file into a numpy array
image = face_recognition.load_image_file("lyf1.jpg")

face_locations = face_recognition.face_locations(image, number_of_times_to_upsample=0, model="cnn")

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

for face_location in face_locations:
  top, right, bottom, left = face_location
  print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom, right))

  face_image = image[top:bottom, left:right]
  pil_image = Image.fromarray(face_image)
  pil_image.show()

結(jié)果

I found 1 face(s) in this photograph.
A face is located at pixel location Top: 205, Left: 276, Bottom: 440, Right: 512

圖片效果大致是這樣

part4.識(shí)別單張圖片中人臉的關(guān)鍵點(diǎn)

代碼

# part4 識(shí)別單張圖片中人臉的關(guān)鍵點(diǎn)

from PIL import Image, ImageDraw
import face_recognition

# Load the jpg file into a numpy array
image = face_recognition.load_image_file("lyf1.jpg")

# Find all facial features in all the faces in the image
face_landmarks_list = face_recognition.face_landmarks(image)
# print(face_landmarks_list)

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

# Create a PIL imagedraw object so we can draw on the picture
pil_image = Image.fromarray(image)
d = ImageDraw.Draw(pil_image)

for face_landmarks in face_landmarks_list:

  # Print the location of each facial feature in this image
  for facial_feature in face_landmarks.keys():
    print("The {} in this face has the following points: {}".format(facial_feature, face_landmarks[facial_feature]))

  # Let's trace out each facial feature in the image with a line!
  for facial_feature in face_landmarks.keys():
    d.line(face_landmarks[facial_feature], width=5)

# Show the picture
pil_image.show()

結(jié)果

I found 1 face(s) in this photograph.
The left_eyebrow in this face has the following points: [(305, 285), (321, 276), (340, 277), (360, 281), (377, 288)]
The right_eye in this face has the following points: [(422, 313), (432, 303), (446, 302), (459, 305), (449, 312), (435, 314)]
The nose_bridge in this face has the following points: [(394, 309), (394, 331), (395, 354), (396, 375)]
The right_eyebrow in this face has the following points: [(407, 287), (424, 278), (442, 273), (461, 272), (478, 279)]
The bottom_lip in this face has the following points: [(429, 409), (419, 421), (408, 428), (398, 430), (389, 429), (377, 424), (364, 412), (370, 413), (389, 414), (398, 415), (407, 413), (423, 411)]
The chin in this face has the following points: [(289, 295), (291, 323), (296, 351), (303, 378), (315, 403), (332, 428), (353, 448), (376, 464), (400, 467), (422, 461), (441, 444), (459, 425), (473, 403), (484, 377), (490, 351), (493, 323), (493, 296)]
The top_lip in this face has the following points: [(364, 412), (377, 407), (389, 403), (397, 406), (406, 402), (417, 405), (429, 409), (423, 411), (406, 412), (397, 414), (389, 413), (370, 413)]
The left_eye in this face has the following points: [(327, 308), (339, 304), (353, 306), (364, 314), (352, 317), (338, 316)]
The nose_tip in this face has the following points: [(375, 383), (386, 387), (396, 390), (407, 385), (416, 381)]

圖片效果

到此這篇關(guān)于Python3 利用face_recognition實(shí)現(xiàn)人臉識(shí)別的方法的文章就介紹到這了,更多相關(guān)Python3 人臉識(shí)別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python如何實(shí)現(xiàn)TF-IDF算法

    python如何實(shí)現(xiàn)TF-IDF算法

    這篇文章主要介紹了python如何實(shí)現(xiàn)TF-IDF算法問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • python re的findall和finditer的區(qū)別詳解

    python re的findall和finditer的區(qū)別詳解

    這篇文章主要介紹了python re的findall和finditer的區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Python使用smtplib模塊發(fā)送電子郵件的流程詳解

    Python使用smtplib模塊發(fā)送電子郵件的流程詳解

    Python中自帶的smtplib模塊可以進(jìn)行基于SMTP協(xié)議的郵件操作,這里我們便總結(jié)了Python使用smtplib模塊發(fā)送電子郵件的流程詳解,并對一些常見的問題給出了解決方法:
    2016-06-06
  • python使用matplotlib畫柱狀圖、散點(diǎn)圖

    python使用matplotlib畫柱狀圖、散點(diǎn)圖

    這篇文章主要為大家詳細(xì)介紹了python使用matplotlib畫柱狀圖、散點(diǎn)圖,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • python編程使用PyQt創(chuàng)建UE藍(lán)圖

    python編程使用PyQt創(chuàng)建UE藍(lán)圖

    這篇文章主要為大家介紹了python編程中如何使用PyQt創(chuàng)建UE藍(lán)圖的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2021-10-10
  • 使用Python的Dataframe取兩列時(shí)間值相差一年的所有行方法

    使用Python的Dataframe取兩列時(shí)間值相差一年的所有行方法

    今天小編就為大家分享一篇使用Python的Dataframe取兩列時(shí)間值相差一年的所有行方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • mac安裝pytorch及系統(tǒng)的numpy更新方法

    mac安裝pytorch及系統(tǒng)的numpy更新方法

    今天小編就為大家分享一篇mac安裝pytorch及系統(tǒng)的numpy更新方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-07-07
  • python如何編寫win程序

    python如何編寫win程序

    在本篇文章里小編給大家分享的是關(guān)于python編寫win程序的實(shí)例內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。
    2020-06-06
  • python實(shí)現(xiàn)處理mysql結(jié)果輸出方式

    python實(shí)現(xiàn)處理mysql結(jié)果輸出方式

    這篇文章主要介紹了python實(shí)現(xiàn)處理mysql結(jié)果輸出方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • Python: 傳遞列表副本方式

    Python: 傳遞列表副本方式

    今天小編就為大家分享一篇Python: 傳遞列表副本方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12

最新評(píng)論

宁陕县| 昭通市| 三河市| 泰和县| 宝清县| 佛学| 大余县| 运城市| 武冈市| 阜宁县| 色达县| 贵德县| 太保市| 保定市| 湘阴县| 拉萨市| 兴和县| 东阳市| 汤阴县| 神木县| 宜君县| 杭锦后旗| 建始县| 新和县| 扶余县| 江阴市| 固安县| 宁城县| 水富县| 苍山县| 资阳市| 漳浦县| 囊谦县| 辉县市| 满城县| 岑巩县| 临沭县| 岗巴县| 清新县| 桂平市| 烟台市|