2016-08-20 53 views
0

首先,如果标题很长,我很抱歉。我正在使用python进行人脸检测。我正在尝试编写一个脚本,在两个目录/文件夹之间检测到相同的图片或几乎相同的图片/面部时,它将通知用户。 以下是我到目前为止编写的脚本。如何通知用户,如果在python和opencv两个目录之间检测到共同的面孔

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图片下载(仅用于学习目的),另一个文件夹中有2张人脸图片,他们的脸部与前一个文件夹中的部分图片相同。具有相同面部的图片处于不同的角度。

我通过参考https://pythonprogramming.net/haar-cascade-face-eye-detection-python-opencv-tutorial/ 的教程编写了脚本,并添加了一些行来发送通知,如果程序从两个文件夹中检测到相同的面孔。

我的问题是我如何确切地通知用户,如果有检测到相同的面孔。我相信这段代码是不完整的,希望有人可以给我一些建议,指出要在这个脚本中添加/编辑什么或不该写什么。

预先感谢您。

回答

1

我不知道我是否正确理解你,但我认为你寻找脸部识别不仅仅是一个脸部检测。

Haar基于特征的级联分类器学会了非常普遍的“面部应该是什么样子”。它检测给定输入图像中学习对象/形状的位置并返回边界框。

因此,如果您想知道检测到的脸部是否与已知脸部相匹配,您需要训练识别器。 OpenCV有3个内置人脸识别器:EigenFaceRecognizerFisherfaceRecognizerLBPHFaceRecognizer(本地二进制模式直方图人脸识别器)。

使用它们与例如recognizer = cv2.createLBPHFaceRecognizer()

您需要为您的用户提供一套培训。也许你的培训文件夹可能看起来像:

1_001.jpg,1_002.jpg,1_003.jpg,2_001.jpg 2_002.jpg,...,n_xyz.jpg

其中n是标签(用户ID - >每个用户都是唯一的),xyz可能是一个描述或一个序列号。

更新:

我用Faces94 benchmark dataset进行测试。因此,我将它们打包到文件夹trainingSamples和其中两个(同一个人但不同的面孔)文件夹testFaces相对于我的Python脚本。

向上述我用bash命令rename

例如与图案重命名的文件夹匹配的所有图像。 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` 

所以正确地识别用户4和用户1

的代码与OpenCV的测试3.1-dev的在Ubuntu 15.10上使用Python 3.4.3和Python 2.7.9。

+0

谢谢你的回答。我会按你的建议尝试。我无法尽快测试它,因为我有另一个项目。如果我有时间,我一定会按照你的建议去尝试。 – Ling

相关问题