2016-08-20 18 views
0

まず、タイトルが長いと申し訳ありません。私は、pythonを使用して顔の検出に取り組んでいます。私は、同じ画像があるとき、または2つのディレクトリ/フォルダの間で検出された顔/顔がほぼ同じときにユーザに通知するスクリプトを作成しようとしています。 以下は私がこれまでに書いたスクリプトです。pythonとopencvを使って2つのディレクトリ間で共通の顔が検出された場合にユーザに通知する方法

import cv2 
import glob, requests 

def detect1(): 
    face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml') 

    for img in glob.glob('/Users/Ling/Pythonfiles/Faces/*.jpg'): 
     cv_img = cv2.imread(img) 
     gray = cv2.cvtColor(cv_img, cv2.COLOR_BGR2GRAY) 
     faces1 = face_cascade.detectMultiScale(gray, 1.3, 5) 

     for (x,y,w,h) in faces1: 
      cv2.rectangle(cv_img,(x,y),(x+w,y+h),(255,0,0),2) 


def detect2(): 
    face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml') 

    for image in glob.glob('/Users/Ling/Pythonfiles/testfolder/*.jpg'): 
     cv_image = cv2.imread(image) 
     gray = cv2.cvtColor(cv_image, cv2.COLOR_BGR2GRAY) 
     faces2 = face_cascade.detectMultiScale(gray, 1.3, 5) 

     for (x,y,w,h) in faces2: 
      cv2.rectangle(cv_image,(x,y),(x+w,y+h),(255,0,0),2) 

def notify(): 
    if detect2 == detect1: 
     key = "<yourkey>" 
     sandbox = "<yoursandbox>.mailgun.org" 
     recipient = "<recipient's email>" 

     request_url = 'https://api.mailgun.net/v2/{0}/messages'.format(sandbox) 
     request = requests.post(request_url, auth=('api', key), 
      data={ 
      'from': '<sender's email', 
      'to': recipient, 
      'subject': 'face detect', 
      'text': 'common face detected' 
     }) 
     print 'Status: {0}'.format(request.status_code) 
     print 'Body: {0}'.format(request.text) 

通知はありませんが、エラーはありません。私はランダムな顔の10枚の写真付きのフォルダを持っています。これはGoogle Image(学習目的のため)からダウンロードしました。また、2人の写真がある別のフォルダには、前のフォルダの画像と同じ顔があります。同じ顔をした画像の角度が異なります。

https://pythonprogramming.net/haar-cascade-face-eye-detection-python-opencv-tutorial/ からチュートリアルを参照してスクリプトを書いて、プログラムが両方のフォルダから同じ顔を検出すると、通知を送信する行を追加します。

私の質問は、同じ顔が検出された場合、どのようにユーザーに正確に通知するかです。私はこのコードが不完全で、誰かが私に何を追加/編集するか、このスクリプトで書いてはいけないことを提案できることを願っています。

ありがとうございます。

答えて

1

私はあなたを正しく理解しているかどうかわかりませんが、私はあなたの顔認識を探していると思います。

Haarフィーチャベースのカスケード分類器は、非常にgenerell「顔がどのように見えるか」を学習しました。与えられた入力画像における学習されたオブジェクト/形状の位置を検出し、境界ボックスを返す。

したがって、検出された顔が既知の顔と一致するかどうかを知りたい場合は、認識ツールを訓練する必要があります。 OpenCVには、顔面認識機能が組み込まれたEigenFaceRecognizerFisherfaceRecognizerLBPHFaceRecognizer(ローカルバイナリパターンヒストグラム顔認識ツール)があります。

は、例えば、 recognizer = cv2.createLBPHFaceRecognizer()

ユーザーのためのトレーニングセットが必要です。

1_001.jpg、1_002.jpg、1_003.jpg、nはラベル(ユーザーIDです2_001.jpg 2_002.jpg、...、n_xyz.jpg

:たぶん、あなたのトレーニングフォルダには、次のようになり - >ユーザーごとに一意)、xyzは説明またはシーケンス番号です。

更新:

私はテストのためにFaces94 benchmark datasetを使用。したがって私はそれらをtrainingSamplesというフォルダにまとめ、そのうちの2つ(同じ人でも顔は違う)を私のpythonスクリプトに関連するフォルダtestFacesにパックしました。私はrename

例えばbashコマンドを使用し、上記パターンのフォルダマッチングですべての画像の名前を変更するには

。 asamma。[1-20] .jpg〜001_ [1-20]。JPG

rename 's/^asamma./001_/' *

import cv2 
import numpy as np 
import os 

class FaceRecognizer: 
    def __init__(self): 
     self.cascadeClassifier = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml') 
     self.faceRecognizer = cv2.face.createLBPHFaceRecognizer() 

     if os.path.isfile('faceRecognizer.xml'): 
      self.faceRecognizer.load('faceRecognizer.xml') 
     else: 
      images = [] 
      labels = [] 
      for file in os.listdir('trainingSamples/'): 
       image = cv2.imread('trainingSamples/'+file, 0) 
       images.append(image) 
       labels.append(int(file.split('_')[0])) 
       ## if you don't have pre-cropped profile pictures you need to detect the face first 
       # faces = self.cascadeClassifier.detectMultiScale(image) 
       # for (x, y, w, h) in faces 
       #  images.append(image[y:y+h, x:x+w]) 
       #  labels.append(int(file.split('_')[0])) 

      self.faceRecognizer.train(images, np.array(labels)) 
      self.faceRecognizer.save('faceRecognizer.xml') 

    def predict(self, image, filename): 
     user, confidence = self.faceRecognizer.predict(image) 
     if confidence < 100.0: 
      print('found user with id {} in picture {} with a confidence of {}'.format(user, filename, confidence)) 

     ## if you don't have pre-cropped profile pictures you need to detect the face first 
     # faces = self.cascadeClassifier.detectMultiScale(image) 
     # for (x, y, w, h) in faces 
     #  user, confidence = self.faceRecognizer.predict(image[y:y+h, x:x+w]) 
     #  # confidence of 0.0 means perfect recognition (same images) 
     #  if confidence < 100.0: 
     #   print('found user with id {} in picture {} with a confidence of {}'.format(user, filename, confidence)) 

faceRecognizer = FaceRecognizer() 
for file in os.listdir('testFaces/'): 
    image = cv2.imread('testFaces/'+file, 0) 
    faceRecognizer.predict(image, file) 

は、コードが出力を生成します。

found user with id 4 in picture 004_20.jpg with a confidence of 27.836526552656732 
found user with id 1 in picture 001_6.jpg with a confidence of 22.473253497606876` 

だから、正しくコードがOpenCVの3.1-devのでテストされ、ユーザ4及びユーザ1

を認識しますUbuntu 15.10ではPython 3.4.3とPython 2.7.9を使用しています。

+0

ご回答ありがとうございます。私はあなたが提案するように試みます。私は別のプロジェクトがあるので、できるだけ早くテストすることはできません。私は時間があれば、私はあなたが提案したように間違いなく試みます。 – Ling

関連する問題