手把手教你用 1 行命令實現人臉識別
環境要求
環境搭建
1、 安裝 Ubuntu17.10 > 安裝步驟在這裡。
2、 安裝 Python2.7.14 (Ubuntu17.10 預設Python版本為2.7.14)
3、 安裝 git 、cmake 、 python-pip
# 安裝 git $ sudo apt-get install -y git # 安裝 cmake $ sudo apt-get install -y cmake # 安裝 python-pip $ sudo apt-get install -y python-pip
4、 安裝編譯 dlib
安裝 face_recognition
這個之前需要先安裝編譯 dlib。
# 編譯dlib前先安裝 boost $ sudo apt-get install libboost-all-dev # 開始編譯dlib # 克隆dlib原始碼 $ git clone https://github.com/davisking/dlib.git $ cd dlib $ mkdir build $ cd build $ cmake .. -DDLIB_USE_CUDA=0 -DUSE_AVX_INSTRUCTIONS=1 $ cmake --build .(注意中間有個空格) $ cd .. $ python setup.py install --yes USE_AVX_INSTRUCTIONS --no DLIB_USE_CUDA
5、 安裝 face_recognition
# 安裝 face_recognition $ pip install face_recognition # 安裝face_recognition過程中會自動安裝 numpy、scipy 等
環境搭建完成後,在終端輸入 face_recognition 命令檢視是否成功
實現人臉識別
示例一(1 行命令實現人臉識別):
1、 首先你需要提供一個資料夾,裡面是所有你希望系統認識的人的圖片。其中每個人一張圖片,圖片以人的名字命名:
known_people 資料夾下有 babe、成龍、容祖兒的照片
2、 接下來,你需要準備另一個資料夾,裡面是你要識別的圖片:
unknown_pic 資料夾下是要識別的圖片,其中韓紅是機器不認識的
3、 然後你就可以執行 face_recognition
命令了,把剛剛準備的兩個資料夾作為引數傳入,命令就會返回需要識別的圖片中都出現了誰:
識別成功!!!
示例二(識別圖片中的所有人臉並顯示出來):
# filename : find_faces_in_picture.py # -*- coding: utf-8 -*- # 匯入pil模組 ,可用命令安裝 apt-get install python-Imaging from PIL import Image # 匯入face_recogntion模組,可用命令安裝 pip install face_recognition import face_recognition # 將jpg檔案載入到numpy 陣列中 image = face_recognition.load_image_file("/opt/face/unknown_pic/all_star.jpg") # 使用預設的給予HOG模型查詢影像中所有人臉 # 這個方法已經相當準確了,但還是不如CNN模型那麼準確,因為沒有使用GPU加速 # 另請參見: find_faces_in_picture_cnn.py face_locations = face_recognition.face_locations(image) # 使用CNN模型 # 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()
用於識別的圖片
# 執行python檔案 $ python find_faces_in_picture.py
從圖片中識別出 7 張人臉,並顯示出來
示例三(自動識別人臉特徵):
# filename : find_facial_features_in_picture.py # -*- coding: utf-8 -*- # 匯入pil模組 ,可用命令安裝 apt-get install python-Imaging from PIL import Image, ImageDraw # 匯入face_recogntion模組,可用命令安裝 pip install face_recognition import face_recognition # 將jpg檔案載入到numpy 陣列中 image = face_recognition.load_image_file("biden.jpg") #查詢影像中所有面部的所有面部特徵 face_landmarks_list = face_recognition.face_landmarks(image) print("I found {} face(s) in this photograph.".format(len(face_landmarks_list))) for face_landmarks in face_landmarks_list: #列印此影像中每個面部特徵的位置 facial_features = [ 'chin', 'left_eyebrow', 'right_eyebrow', 'nose_bridge', 'nose_tip', 'left_eye', 'right_eye', 'top_lip', 'bottom_lip' ] for facial_feature in facial_features: print("The {} in this face has the following points: {}".format(facial_feature, face_landmarks[facial_feature])) #讓我們在影像中描繪出每個人臉特徵! pil_image = Image.fromarray(image) d = ImageDraw.Draw(pil_image) for facial_feature in facial_features: d.line(face_landmarks[facial_feature], width=5) pil_image.show()
自動識別出人臉特徵
示例四(識別人臉鑑定是哪個人):
# filename : recognize_faces_in_pictures.py # -*- conding: utf-8 -*- # 匯入face_recogntion模組,可用命令安裝 pip install face_recognition import face_recognition #將jpg檔案載入到numpy陣列中 babe_image = face_recognition.load_image_file("/opt/face/known_people/babe.jpeg") Rong_zhu_er_image = face_recognition.load_image_file("/opt/face/known_people/Rong zhu er.jpg") unknown_image = face_recognition.load_image_file("/opt/face/unknown_pic/babe2.jpg") #獲取每個影像檔案中每個面部的面部編碼 #由於每個影像中可能有多個面,所以返回一個編碼列表。 #但是由於我知道每個影像只有一個臉,我只關心每個影像中的第一個編碼,所以我取索引0。 babe_face_encoding = face_recognition.face_encodings(babe_image)[0] Rong_zhu_er_face_encoding = face_recognition.face_encodings(Rong_zhu_er_image)[0] unknown_face_encoding = face_recognition.face_encodings(unknown_image)[0] known_faces = [ babe_face_encoding, Rong_zhu_er_face_encoding ] #結果是True/false的陣列,未知面孔known_faces陣列中的任何人相匹配的結果 results = face_recognition.compare_faces(known_faces, unknown_face_encoding) print("這個未知面孔是 Babe 嗎? {}".format(results[0])) print("這個未知面孔是 容祖兒 嗎? {}".format(results[1])) print("這個未知面孔是 我們從未見過的新面孔嗎? {}".format(not True in results))
顯示結果如圖
示例五(識別人臉特徵並美顏):
# filename : digital_makeup.py
# -*- coding: utf-8 -*-
# 匯入pil模組 ,可用命令安裝 apt-get install python-Imaging
from PIL import Image, ImageDraw
# 匯入face_recogntion模組,可用命令安裝 pip install face_recognition
import face_recognition
#將jpg檔案載入到numpy陣列中
image = face_recognition.load_image_file("biden.jpg")
#查詢影像中所有面部的所有面部特徵
face_landmarks_list = face_recognition.face_landmarks(image)
for face_landmarks in face_landmarks_list:
pil_image = Image.fromarray(image)
d = ImageDraw.Draw(pil_image, 'RGBA')
#讓眉毛變成了一場噩夢
d.polygon(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 128))
d.polygon(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 128))
d.line(face_landmarks['left_eyebrow'], fill=(68, 54, 39, 150), width=5)
d.line(face_landmarks['right_eyebrow'], fill=(68, 54, 39, 150), width=5)
#光澤的嘴脣
d.polygon(face_landmarks['top_lip'], fill=(150, 0, 0, 128))
d.polygon(face_landmarks['bottom_lip'], fill=(150, 0, 0, 128))
d.line(face_landmarks['top_lip'], fill=(150, 0, 0, 64), width=8)
d.line(face_landmarks['bottom_lip'], fill=(150, 0, 0, 64), width=8)
#閃耀眼睛
d.polygon(face_landmarks['left_eye'], fill=(255, 255, 255, 30))
d.polygon(face_landmarks['right_eye'], fill=(255, 255, 255, 30))
#塗一些眼線
d.line(face_landmarks['left_eye'] + [face_landmarks['left_eye'][0]], fill=(0, 0, 0, 110), width=6)
d.line(face_landmarks['right_eye'] + [face_landmarks['right_eye'][0]], fill=(0, 0, 0, 110), width=6)
pil_image.show()
美顏前後對比
相關文章
- 手把手教你實現人臉識別,有手就行
- 64行程式碼實現簡單人臉識別行程
- 基於Android平臺實現人臉識別Android
- 人臉識別系列(二):DeepID1
- 手把手教你運用深度學習構建影片人臉識別模型(Python實現)深度學習模型Python
- 如何用Excel 9步實現CNN人臉識別ExcelCNN
- matlab實現人臉識別(數學基礎原理)Matlab
- 手把手教你 python 人臉識別自動開機Python
- 美軍開發遠端人臉識別系統,實現1公里內目標識別
- 手把手教你運用深度學習構建視訊人臉識別模型(Python實現)深度學習模型Python
- 機器學習實戰-SVM模型實現人臉識別機器學習模型
- opencv 人臉識別OpenCV
- 【ROS】OpenCV+ROS 實現人臉識別(Ubantu16.04)ROSOpenCV
- 虹軟人臉識別ArcSoft3.0NodeJs 版本實現NodeJS
- 人臉檢測識別,人臉檢測,人臉識別,離線檢測,C#原始碼C#原始碼
- 【opencv3】 svm實現手寫體與人臉識別OpenCV
- 妙招:使用Python實現圖片在人臉識別並顯示Python
- 人臉識別之特徵臉方法(Eigenface)特徵
- Mars演算法實踐——人臉識別演算法
- 人臉識別檢測專案實戰
- [OpenCV實戰]1 基於深度學習識別人臉性別和年齡OpenCV深度學習
- opencv安裝實錄附十幾行C++實現的一個人臉識別demoOpenCVC++
- python ubuntu人臉識別 -1 SVM binary 分類器PythonUbuntu
- C#人臉識別入門篇-STEP BY STEP人臉識別--入門篇C#
- 智慧人臉識別門禁系統開發,人臉識別開鎖流程
- 基於mtcnn/facenet/tensorflow實現人臉識別登入系統CNN
- 【Linux學習】OpenCV+ROS 實現人臉識別(Ubantu16.04)LinuxOpenCVROS
- 前端人臉識別--兩張臉相似度前端
- python—呼叫API人臉識別PythonAPI
- 在Mac系統執行dlib人臉識別DemoMac
- 支小蜜人臉識別消費系統助力學校食堂實現“刷臉吃飯”
- python ubuntu dlib人臉識別3-人臉對齊PythonUbuntu
- 人臉活體檢測人臉識別:眨眼+張口
- 人臉識別之Python DLib庫進行人臉關鍵點識別Python
- 如何快速實現人臉識別通道?一文了解具體技巧
- 乾貨 | AI人臉識別之人臉搜尋AI
- 人臉識別智慧考勤系統開發_人臉識別考勤管理系統開發
- 前端如何玩轉人臉識別前端
- 人臉識別技術應用