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

使用Python和dlib進行人臉檢測

開發(fā) 后端 人臉識別
“Dlib是一個現(xiàn)代化的C ++工具包,包含用于創(chuàng)建復雜軟件的機器學習算法和工具”。它使您能夠直接在Python中運行許多任務,其中一個例子就是人臉檢測。

使用Python和dlib進行人臉檢測 

“Dlib是一個現(xiàn)代化的C ++工具包,包含用于創(chuàng)建復雜軟件的機器學習算法和工具”。它使您能夠直接在Python中運行許多任務,其中一個例子就是人臉檢測。 

安裝dlib并不像只做一個“pip install dlib”那么簡單,因為要正確配置和編譯dlib,您首先需要安裝其他系統(tǒng)依賴項。如果你按照這里描述的步驟,它應該很容易讓dlib啟動并運行。(在本文中,我將介紹如何在Mac上安裝dlib,但如果您使用的是Ubuntu,請務必查看相關(guān)資源部分的鏈接。) 

你需要確定的***件事是你已經(jīng)安裝和更新了Hombrew。如果您需要安裝它,請將其粘貼到終端中: 

 

  1. $ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"  

或者,如果您需要更新Hombrew,請輸入以下內(nèi)容: 

 

  1. $ brew update  

您現(xiàn)在可以使用Homebrew來安裝CMake,Boost.Python,以及在您的系統(tǒng)中正確配置和編譯dlib所需的兩個依賴關(guān)系: 

 

  1. $ brew install cmake   
  2. $ brew install boost-python  

***,您需要手動下載并安裝XQuartz。 

您現(xiàn)在已準備好安裝dlib。我們將通過首先為這個項目創(chuàng)建一個孤立的虛擬環(huán)境來做到這一點。我將使用virtualenv,但您可以使用任何您熟悉的虛擬環(huán)境工具,包括Python的venv模塊。需要scikit-image庫才能讀取我們稍后將傳遞給dlib的圖像文件,因此我們還需要pip安裝它: 

 

  1. $ virtualenv venv_dlib   
  2. $ source venv_dlib / bin / activate   
  3. $ pip install scikit-image   
  4. $ pip install dlib  

就是這樣。有了這個,你應該有可用的dlib。

Dlib 

Dlib提供了不同的臉部檢測算法。我將在這里使用的是基于CNN的人臉檢測器。您可以下載預訓練模型:https://github.com/davisking/dlib-models。由于使用此模型的計算成本很高,因此***在GPU上執(zhí)行以下代碼。使用CPU也可以,但速度會更慢。 

要在下面的要點中運行人臉檢測代碼,我建議首先在虛擬環(huán)境中再安裝兩個庫。這些庫將使與代碼交互和可視化結(jié)果更容易: 

 

  1. $ pip install matplotlib   
  2. $ pip install jupyterlab  

安裝完庫后,您需要確保: 

  •     下載預訓練模型(http://dlib.net/files/mmod_human_face_detector.dat.bz2)并將其存儲在項目的根目錄中 
  •     創(chuàng)建一個名為'faces'的新目錄,在該目錄中存儲帶有希望檢測的臉部的.jpg。 

有了這個,你終于準備好開始在圖片中檢測臉部了!您可以通過在Jupyter Notebook中運行以下代碼來完成此操作    

  1. import dlib   
  2.     import matplotlib.patches as patches   
  3.     import matplotlib.pyplot as plt   
  4.     from pathlib import Path   
  5.     from skimage import io   
  6.     %matplotlib inline   
  7.     # Load trained model  
  8.     cnn_face_detector = dlib.cnn_face_detection_model_v1(  
  9.     'mmod_human_face_detector.dat')   
  10.     # Function to detect and show faces in images   
  11.     def detect_face_dlib(img_path, ax):   
  12.     # Read image and run algorithm   
  13.     img = io.imread(img_path)   
  14.     dets = cnn_face_detector(img, 1)   
  15.     # If there were faces detected, show them   
  16.     if len(dets) > 0:   
  17.     for d in dets:   
  18.     rect = patches.Rectangle(   
  19.     (d.rect.left(), d.rect.top()),   
  20.     d.rect.width(),   
  21.     d.rect.height(),   
  22.     fill=False,   
  23.     color='b',   
  24.     lw='2')   
  25.     ax.add_patch(rect)   
  26.     ax.imshow(img)   
  27.     ax.set_title(str(img_path).split('/')[-1])   
  28.     # Path to images   
  29.     images = list(Path('faces').glob('*.jpg'))   
  30.     # Show results   
  31.     fig = plt.figure(figsize=(15, 5))   
  32.     for i, img in enumerate(images): 
  33.     ax = fig.add_subplot(1, len(images), i+1)   
  34.     detect_face_dlib(img, ax)  

 

結(jié)果 

在運行代碼之后,您應該看到圖像中的臉部周圍出現(xiàn)藍色方塊,如果您問我,考慮到我們只寫了幾行代碼,這非常棒!  

責任編輯:龐桂玉 來源: 今日頭條
相關(guān)推薦

2022-04-05 20:54:21

OpenCVPython人臉檢測

2021-03-29 15:13:23

深度學習人臉解鎖算法

2022-05-25 07:11:13

Python人臉識別代碼

2012-04-28 14:01:17

HTML5

2023-10-07 09:00:00

人臉檢測Web應用程序

2024-06-05 08:14:26

SpringElasticsea人臉數(shù)據(jù)

2022-01-10 16:40:06

神經(jīng)網(wǎng)絡AI算法

2020-07-30 13:00:00

Python面部識別智能

2023-04-26 22:52:19

視覺人臉檢測人臉對齊

2020-10-15 12:00:01

Python 開發(fā)編程語言

2024-06-12 12:57:12

2024-07-02 10:28:59

2018-01-23 09:17:22

Python人臉識別

2017-01-23 21:35:58

Android人臉檢測介紹

2023-01-29 14:29:59

Python識別車牌

2018-05-02 15:41:27

JavaScript人臉檢測圖像識別

2020-12-30 08:20:04

人臉檢測Retina FacemobileNet

2013-08-26 10:53:26

人臉檢測API

2023-04-03 14:02:32

Python人臉視頻

2021-10-29 10:10:28

人工智能AI人臉識別
點贊
收藏

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