最近在研究一我的臉識別開源框架face_recognition,編譯須要依賴 dlib、boost、cmake 等相關環境,在編譯的時候,踩了一大堆坑,網上資料大都不是很全面再加上 Windows 環境下去配置這些自己就比 Liunx 或 Mac下配置要相對麻煩一些。若是你是準備在 Windows 環境下編譯,就作好準備踩坑吧~~~python
系統環境git
- Windows 7
- Python 2.7.14
- VS2015
1. 首先github.com/davisking/d… 下載整個zipgithub
git clone https://github.com/davisking/dlib](https://github.com/davisking/dlib
複製代碼
2. 前置的一些python庫要安裝, scipy, numpy+mkl 這兩個用pip安裝可能會蛋疼, www.lfd.uci.edu/~gohlke/pyt… 到這裏面找對應版本的wheel, 而後用easy_install就KO了shell
3. 安裝Boost sourceforge.net/projects/bo… 下載boost-binaries, 最新的,直接點擊exe程序等待安裝完畢, 正常的話安裝的目錄是 X:\local\boost_1_XX_X (保持版本名一致, 也就是XX_X別改)bootstrap
4. 這一步也貌似能夠不用 系統變量加上 VS2015的位置 新建一個數組
VS140COMNTOOLS 值 X:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\Tools\
複製代碼
5. 去到Boost下面, 雙擊一下 bootstrap.bat , 運行OK以後boost_1_66_0\tools\build文件夾下找到如下兩個文件bash
而後開啓一個命令行,定位到這個文件夾,運行命令:微信
b2 install
複製代碼
運行完以後再執行下面命令:app
b2 -a --with-python address-model=64 toolset=msvc runtime-link=static
複製代碼
成功後能找到 stage 文件夾框架
6.在系統環境配置好下面兩個環境變量
BOOST_ROOT=C:\local\boost_X_XX_X
BOOST_LIBRARYDIR=C:\local\boost_X_XX_X\stage\lib
複製代碼
最後執行如下命令:
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主要展現了識別指定圖片中人臉的特徵數據,下面就是人臉的八個特徵,咱們就是要獲取特徵數據
'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 數組中
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版本相匹配的版本,能夠在這個網站(www.lfd.uci.edu/~gohlke/pyt… .whl(個人python版本是2.7因此選擇該版本安裝),安裝完成以後,打開 cmd 輸入 import cv2 沒有提示任何錯誤說明安裝成功。
# -*- 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
# 調用opencv函數顯示圖片
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,配置都是差很少的,環境配置好,運行下面代碼便可
# -*- 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()
複製代碼
運行結果展現
在裝VS2015時,默認是不安裝C++,你須要從新運行setup ,而後選擇modify,選擇 language 下的C++,而後開始安裝,就能夠解決問題了 –來自stackoverflow.com/
方案一(推薦):在pip安裝所在文件夾路徑下,創造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安裝指令下載速度會很是快
方案二:修改加大超時時間
pip --default-timeout=100 install -U pip
複製代碼
再執行下面指令進行安裝
pip --default-timeout=100 install -U face_recognition
複製代碼
最近在編寫Python腳本過程當中遇到一個問題比較奇怪:Python腳本徹底正常沒問題,但執行總報錯"AttributeError: 'module' object has no attribute 'xxx'"。這實際上是.pyc文件存在問題。
問題定位:
查看import庫的源文件,發現源文件存在且沒有錯誤,同時存在源文件的.pyc文件
問題解決方法:
- 命名py腳本時,不要與python預留字,模塊名等相同
- 刪除該庫的.pyc文件(由於py腳本每次運行時均會生成.pyc文件;在已經生成.pyc文件的狀況下,若代碼不更新,運行時依舊會走pyc,因此要刪除.pyc文件),從新運行代碼;或者找一個能夠運行代碼的環境,拷貝替換當前機器的.pyc文件便可