Webカメラ+Dlib+Python(winPython)導入と動作確認
- Takeshi Suetani
- 2018年5月20日
- 読了時間: 2分

WinPythonにDlibをインストールする方法
1.「WinPython Command Prompt」で
2.pip install cmake
の後、
3.pip install dlib
巷の情報では「Pythonにdlibをインストールはめんどくさい」とあるが、
上記の通り至ってシンプル超簡単。
WinPython万歳ですね。
さて、Webカメラを使った顔認識(学習済ファイルを利用)に関してOpenCVとDlibがどう違うのか確認。
【結論】
OpenCV・・・動作が速い。(HD画像でも5fpsくらいは大丈夫)Web上でサンプル多し。学習済みファイル(xml)の種類豊富。だが、誤認識が結構ある。
Dlib・・・・・動作が遅い。(HD画像だと0.5fpsくらい)OpenCVと比べるとサンプル少ない。学習済みファイルの種類は不明。しかし、割と正確。
用途によってはDlibの正確さは貴重。
以下、Dlibのコード
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
import cv2 import time import dlib
window = "Push ESC key to stop this program" color = (255, 255, 255) #白
if(__name__ == '__main__'):
# デフォルトカメラ capture = cv2.VideoCapture(0) capture.set(3, 1280) # Width capture.set(4, 960) # Heigh capture.set(5, 15) # FPS 15or30
# キャプチャ処理 while(True): key = cv2.waitKey(1) if(key == 27): print("exit.") break
# 画像キャプチャ ret, img = capture.read()
# 取り込み開始になっていなかったら上の処理に戻る if(ret == False): print("Capture Failed. ") break
#顔検出 detector = dlib.get_frontal_face_detector() # RGB変換 (opencv形式からskimage形式に変換) img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # frontal_face_detectorクラスは矩形, スコア, サブ検出器の結果を返す dets, scores, idx = detector.run(img_rgb, 0) # 矩形の色 s = '' if len(dets) > 0: # 顔画像ありと判断された場合 for i, rect in enumerate(dets): # detsが矩形, scoreはスコア、idxはサブ検出器の結果(0.0がメインで数が大きい程弱い) # print rect, scores[i], idx[i] cv2.rectangle(img, (rect.left(), rect.top()), (rect.right(), rect.bottom()), color, thickness=3) s += (str(rect.left()) + ' ' + str(rect.top()) + ' ' + str(rect.right()) + ' ' + str(rect.bottom()) + ' ')
# ループ
capture.release() cv2.destroyAllWindows()