Win7環(huán)境下VS2015+Python 2.7 編譯face_recognition

最近在研究一個人臉識別開源框架face_recognition,編譯需要依賴 dlib、boost、cmake 等相關環(huán)境,在編譯的時候,踩了一大堆坑,網(wǎng)上資料大都不是很全面再加上 Windows 環(huán)境下去配置這些本身就比 Liunx 或 Mac下配置要相對麻煩一些。如果你是準備在 Windows 環(huán)境下編譯,就做好準備踩坑吧~~~

系統(tǒng)環(huán)境

  • Windows 7
  • Python 2.7.14
  • VS2015

安裝步驟

1. 首先https://github.com/davisking/dlib 下載整個zip

git clone https://github.com/davisking/dlib

2. 前置的一些python庫要安裝, scipy, numpy+mkl 這兩個用pip安裝可能會蛋疼, https://www.lfd.uci.edu/~gohlke/pythonlibs/ 到這里面找對應版本的wheel, 然后用easy_install就KO了

3. 安裝Boost https://sourceforge.net/projects/boost/files/boost-binaries 下載boost-binaries, 最新的,直接點擊exe程序等待安裝完畢, 正常的話安裝的目錄是 X:\local\boost_1_XX_X (保持版本名一致, 也就是XX_X別改)

4. 這一步也貌似可以不用 系統(tǒng)變量加上 VS2015的位置 新建一個

VS140COMNTOOLS 值  X:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\

5. 去到Boost下面, 雙擊一下 bootstrap.bat , 運行OK之后boost_1_66_0\tools\build文件夾下找到以下兩個文件

然后開啟一個命令行,定位到這個文件夾,運行命令:

b2 install

運行完之后再執(zhí)行下面命令:

 b2 -a --with-python address-model=64 toolset=msvc runtime-link=static 

成功后能找到 stage 文件夾

6.在系統(tǒng)環(huán)境配置好下面兩個環(huán)境變量

BOOST_ROOT=C:\local\boost_X_XX_X

BOOST_LIBRARYDIR=C:\local\boost_X_XX_X\stage\lib 

最后執(zhí)行以下命令:

python setup.py install --yes USE_AVX_INSTRUCTIONS --yes DLIB_USE_CUDA

CUDA這個, 主要是用在有顯卡的機器學習, 如果沒有可以不加
完成! 打開python shell 試一下 import dlib 沒問題就可以

pip install face_recognition

安裝成功之后,我們可以在python中正常 import face_recognition 了

編寫人臉檢測程序

此demo主要展示了識別指定圖片中人臉的特征數(shù)據(jù),下面就是人臉的八個特征,我們就是要獲取特征數(shù)據(jù)

    'chin',
    'left_eyebrow',
    'right_eyebrow',
    'nose_bridge',
    'nose_tip',
    'left_eye',
    'right_eye',
    'top_lip',
    'bottom_lip'

人臉檢測代碼

# -*- coding: utf-8 -*-
# 自動識別人臉特征
# filename : find_facial_features_in_picture.py

# 導入pil模塊 ,可用命令安裝 apt-get install python-Imaging
from PIL import Image, ImageDraw
# 導入face_recogntion模塊,可用命令安裝 pip install face_recognition
import face_recognition

# 將jpg文件加載到numpy 數(shù)組中
image = face_recognition.load_image_file("mayun.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() 

運行結果:
自動識別圖片中的人臉,并且識別它的特征

原圖:

原圖

運行效果:

運行結果

編寫人臉識別程序

注意:這里使用了 python-opencv,一定要配置好了opencv才能運行成功。
opencv選擇跟自己python版本相匹配的版本,可以在這個網(wǎng)站(https://www.lfd.uci.edu/~gohlke/pythonlibs/)下載opencv_python-2.4.13.5-cp27-cp27m-win_amd64 .whl(我的python版本是2.7所以選擇該版本安裝),安裝完成之后,打開 cmd 輸入 import cv2 沒有提示任何錯誤說明安裝成功。

opencv安裝成功
# -*- coding: utf-8 -*-
#

# 檢測人臉
import face_recognition
import cv2

# 讀取圖片并識別人臉
img = face_recognition.load_image_file("mayun.jpeg")
face_locations = face_recognition.face_locations(img)
print face_locations

# 調(diào)用opencv函數(shù)顯示圖片
img = cv2.imread("mayun.jpeg")
cv2.namedWindow("原圖")
cv2.imshow("原圖", img)

# 遍歷每個人臉,并標注
faceNum = len(face_locations)
for i in range(0, faceNum):
    top =  face_locations[i][0]
    right =  face_locations[i][1]
    bottom = face_locations[i][2]
    left = face_locations[i][3]
    
    start = (left, top)
    end = (right, bottom)
    
    color = (55,255,155)
    thickness = 3
    cv2.rectangle(img, start, end, color, thickness)

# 顯示識別結果
cv2.namedWindow("人臉識別")
cv2.imshow("人臉識別", img)

cv2.waitKey(0)
cv2.destroyAllWindows()

運行結果:
程序會讀取當前目錄下指定的圖片,然后識別其中的人臉,并標注每個人臉。


人臉識別結果

攝像頭實時識別人臉

此處因為公司臺式機沒有攝像頭,所以用的Mac上運行這個demo,配置都是差不多的,環(huán)境配置好,運行下面代碼即可

# -*- coding: utf-8 -*-
# 攝像頭實時識別人臉
import face_recognition
import cv2

video_capture = cv2.VideoCapture(0)
# 加載本地圖片
xhb_img = face_recognition.load_image_file("xhb.jpg")
xhb_face_encoding = face_recognition.face_encodings(xhb_img)[0]

# 初始化變量
face_locations = []
face_encodings = []
face_names = []
process_this_frame = True

while True:
    ret, frame = video_capture.read()
    
    small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
    
    if process_this_frame:
        face_locations = face_recognition.face_locations(small_frame)
        face_encodings = face_recognition.face_encodings(small_frame, face_locations)
        
        face_names = []
        for face_encoding in face_encodings:
            # 可以自定義設置識別閾值(tolerance)此處設置為0.5,默認為0.6,太小可能識別不出來,太大可能造成識別混淆
            match = face_recognition.compare_faces([xhb_face_encoding], face_encoding,tolerance=0.5)
            
            if match[0]:
                name = "xiaohaibin"
            else:
                name = "unknown"
            
            face_names.append(name)

    process_this_frame = not process_this_frame
    
    for (top, right, bottom, left), name in zip(face_locations, face_names):
        top *= 4
        right *= 4
        bottom *= 4
        left *= 4
        
        cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255),  2)
        
        cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), 2)
        font = cv2.FONT_HERSHEY_DUPLEX
        cv2.putText(frame, name, (left+6, bottom-6), font, 1.0, (255, 255, 255), 1)

    cv2.imshow('Video', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

video_capture.release()
cv2.destroyAllWindows()

運行結果展示


實時人臉識別

相關資料

踩坑

1.解決 cl.exe 找不到的問題

在裝VS2015時,默認是不安裝C++,你需要重新運行setup ,然后選擇modify,選擇 language 下的C++,然后開始安裝,就可以解決問題了
–來自http://stackoverflow.com/

VS2015配置.png

2.解決執(zhí)行 pip install face_recognition 出錯,報SSL Error

SSL Error

方案一(推薦):在pip安裝所在文件夾路徑下,創(chuàng)造python文件(.py)

import os  

ini="""[global] 
index-url = https://pypi.doubanio.com/simple/ 
[install] 
trusted-host=pypi.doubanio.com 
"""  
pippath=os.environ["USERPROFILE"]+"\\pip\\"  

if not os.path.exists(pippath):  
    os.mkdir(pippath)  

with open(pippath+"pip.ini","w+") as f:  
    f.write(ini)  

在cmd上運行這個.py文件即可
之后再用pip install安裝指令下載速度會非???/p>

方案二:修改加大超時時間

pip --default-timeout=100 install -U pip

再執(zhí)行下面指令進行安裝

pip --default-timeout=100 install -U face_recognition

3.Python腳本報錯AttributeError: ‘module’ object has no attribute’xxx’解決方法

最近在編寫Python腳本過程中遇到一個問題比較奇怪:Python腳本完全正常沒問題,但執(zhí)行總報錯"AttributeError: 'module' object has no attribute 'xxx'"。這其實是.pyc文件存在問題。

問題定位:

查看import庫的源文件,發(fā)現(xiàn)源文件存在且沒有錯誤,同時存在源文件的.pyc文件

問題解決方法:

  1. 命名py腳本時,不要與python預留字,模塊名等相同
  2. 刪除該庫的.pyc文件(因為py腳本每次運行時均會生成.pyc文件;在已經(jīng)生成.pyc文件的情況下,若代碼不更新,運行時依舊會走pyc,所以要刪除.pyc文件),重新運行代碼;或者找一個可以運行代碼的環(huán)境,拷貝替換當前機器的.pyc文件即可。
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容