Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Face Recognition with OpenCV and scikit-learn

23,544 views

Published on

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

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

Published in: Technology
  • My brother found Custom Writing Service ⇒ www.HelpWriting.net ⇐ and ordered a couple of works. Their customer service is outstanding, never left a query unanswered.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Hi there! I just wanted to share a list of sites that helped me a lot during my studies: .................................................................................................................................... www.EssayWrite.best - Write an essay .................................................................................................................................... www.LitReview.xyz - Summary of books .................................................................................................................................... www.Coursework.best - Online coursework .................................................................................................................................... www.Dissertations.me - proquest dissertations .................................................................................................................................... www.ReMovie.club - Movies reviews .................................................................................................................................... www.WebSlides.vip - Best powerpoint presentations .................................................................................................................................... www.WritePaper.info - Write a research paper .................................................................................................................................... www.EddyHelp.com - Homework help online .................................................................................................................................... www.MyResumeHelp.net - Professional resume writing service .................................................................................................................................. www.HelpWriting.net - Help with writing any papers ......................................................................................................................................... Save so as not to lose
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Thank you for sharing this interesting information here. Great post. And I agree with you that it is really hardly to find a student who enjoys executing college assignments. All these processes require spending much time and efforts, that is why i recommend all the students use the professional writing service ⇒ HelpWriting.net ⇐ Good luck.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Dating for everyone is here: ❶❶❶ http://bit.ly/2F7hN3u ❶❶❶
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • Dating direct: ❶❶❶ http://bit.ly/2F7hN3u ❶❶❶
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Face Recognition with OpenCV and scikit-learn

  1. 1. 26, January, 2013OpenCV と 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 BoostedCascade 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 Detectionimport numpy as npimport 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_SEARCHTrects = 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 npfrom sklearn.decomposition import PCAclass 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 distanceclass 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/lppPyFace レポジトリhttps://github.com/lucidfrontier45/PyFace

×