Successfully reported this slideshow.
Your SlideShare is downloading. ×

Face Recognition with OpenCV and scikit-learn

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Loading in …3
×

Check these out next

1 of 14 Ad

Face Recognition with OpenCV and scikit-learn

Download to read offline

A lightweight implementation of Face Recognition system with Python. OpenCV and scikit-learn.

Python, OpenCv, scikit-learnによる簡易な顔認識システムの実装. Tokyo.Scipy5にて発表。

A lightweight implementation of Face Recognition system with Python. OpenCV and scikit-learn.

Python, OpenCv, scikit-learnによる簡易な顔認識システムの実装. Tokyo.Scipy5にて発表。

Advertisement
Advertisement

More Related Content

Slideshows for you (20)

Advertisement

Similar to Face Recognition with OpenCV and scikit-learn (13)

Recently uploaded (20)

Advertisement

Face Recognition with OpenCV and scikit-learn

  1. 1. 26, January, 2013 OpenCV と scikit-learn で楽々顔認識 杜 世橋 FreeBit @lucidfrontier45
  2. 2. 顔認識とは ? Face Detection 画像から人の顔部分を抜き取る Face Recognition 与えられた顔の人物を推定する Lena
  3. 3. 顔認識とは ? Face Detection Haar Cascade Classifier Weak classifier + Weak classifier Weak classifier Haar-like feature AdaBoost 原著論文は Paul Viola and Michael J. Jones. Rapid Object Detection using a Boosted Cascade of Simple Features. IEEE CVPR, 2001. 日本語ではこちらのスライドがわかりやすい。
  4. 4. 顔認識とは ? Face Recognition Input Picture EigenFace X (dim = d1 x d2) M. Turk and A. Pentland (1991). “Face recognition using eigenfaces”. PCA Projected Picture Y (dim = p < d1 x d2) Nearest Neighbor or http://scikit-learn.org/ より Other Supervised Prediction * ちなみに PCA の代わりに Fisher 判別分析を使用した FisherFace や LPP を使用した LaplacianFace などもある。
  5. 5. 実装イメージ Face Detection Face Recognition scikit-learn OpenCV SciPy NumPy Face Detection は OpenCV の CascadeClassifier モジュールを使用。 Face Recognition は scikit-learn を用いて適当に実装。 ( 実は OpenCV にも )
  6. 6. 実装イメージ Face Detection import numpy as np import cv, cv2 #画像ファイルを読み込んでモノクロ化&輝度を正規化 img = cv2.imread(img_file) #imgはndarray型! gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray_img = cv2.equalizeHist(gray_img) #顔検出器を作成 cascade_file = “haarcascade_frontalface_default.xml" detector = cv2.CascadeClassifier(cascade_file)
  7. 7. 実装イメージ Face Detection #検出を実行 flags = cv.CV_HAAR_FIND_BIGGEST_OBJECT | cv.CV_HAAR_DO_ROUGH_SEARCHT rects = detector.detectMultiScale(gray_img, scaleFactor=1.1, minNeighbors=3, minSize=(64, 64), flags=detact_flags) #戻り値はxy座標、幅、高さ。OpenCVのndarrayは(y,x)の順なので注意 x, y, w, h = rects[0] face = np.array(gray_img[y:y+h, x:x+w]) #後のためにリサイズして次元を統一しておく min_size = (64, 64) face = cv2.resize(face, min_size, interpolation=cv.CV_INTER_AREA)
  8. 8. 実装イメージ データの型変換 OpenCV の関数は通常は 2 次元の 8bit int の配列を返してくる。 顔認識に進む前に 32bit float の 1 次元配列に変換しておく。 def convImgMat(img, mat_type="opencv"): shape = img.shape #まずは32bit floatに変換 img = np.asanyarray(img, dtype=np.float32) if mat_type == "opencv" and len(shape) == 1: size = int(math.sqrt(img.size)) img = np.array(img.reshape((size, size)) * 255.0, dtype=np.uint8) #0-255を0-1に正規化し、1次元に展開する elif mat_type == "numpy" and len(shape) == 2: img = img.flatten() / 255.0 else: raise ValueError("wrong format") return img
  9. 9. 実装イメージ Face Recognition( 学習編 ) import numpy as np from sklearn.decomposition import PCA class FaceRecognizer(): def fit(face_imgs, labels) #トランスフォーマーを作成 self.transformer = PCA(n_components=0.9) #係数を学習 self.transformer.fit(face_imgs) #特徴量とラベルをセット self.features = self.transformer.transform(face_imgs) self.labels = np.array(labels)
  10. 10. 実装イメージ Face Recognition( 予測編 ) from scipy.spatial import distance class FaceRecognizer(): … def predict(face_img) #特徴量を計算 feature = self.transformer.transform(face_imgs) #距離を計算し、最も近いものを選ぶ distances = distance.cdist(self.features, [feature]).flatten() idx = distances.argmin() #もし距離がしきい値以上だったら未知人物を返す if distances[idx] > MAX_DISTANCE: return UNKNOWN_PERSON else: return self.labels[idx]
  11. 11. システム化 今回作った顔認識システムの構成 - 顔検出 Haar Cascade Classifier (OpenCV) - 顔認識 LaplacianFace (scikit-learn 独自レポジトリ ) - DB Redis (redis-py 経由 ) - HTTPD Lighttpd (cgi は python) 学習した係数や顔画像、射影した特徴量はndarray として Redis に保存。その際にはtostring メソッドで変換する。 取り出すときには逆に np.fromstring を使用。
  12. 12. システム化 追加学習時には LPP 係数は 学習せず、 射影した特徴量の みを計算して Redis に追加。 予測時には Redis から特徴 量をすべて引っ張ってきて最近 傍探索を行う。 精度はまだあまりよくない ...
  13. 13. おわり おすすめの本 Mastering Opencv with Practical Computer Vision Projects   Chapter 8 が OpenCV を利用した顔認識。性能を大きく左右する前処理 についても詳しく解説されている。 Android や iOS で OpenCV を利用したプログラム開発もある!
  14. 14. おわり 今回使用作成したコード LPP を実装した scikit-learn のレポジトリ ( そのうち本家にマージします ) https://github.com/lucidfrontier45/scikit-learn/tree/lpp PyFace レポジトリ https://github.com/lucidfrontier45/PyFace

×