Python,OpenCV:使用ORB功能和KNN对性别进行分类

时间:2013-10-07 11:57:32

标签: python opencv image-processing classification image-recognition

任务:将人脸图像分类为女性或男性。可以使用标签训练图像,从网络摄像头获取测试图像。

使用: Python 2.7,OpenCV 2.4.4

我正在使用ORB从灰度图像中提取特征,我希望将其用于训练K-Nearest Neighbor分类器。每个训练图像都是不同的人,因此每个图像的关键点和描述符的数量明显不同。我的问题是我无法理解KNN和ORB的OpenCV文档。我已经看到了关于ORB,KNN和FLANN的其他SO问题,但他们没有多大帮助。

ORB给出的描述符的本质究竟是什么?它与BRIEF,SURF,SIFT等获得的描述符有什么不同?

对于KNN中的每个训练样本,特征描述符似乎应该具有相同的大小。如何确保每个图像的描述符大小相同?更一般地说,应该以什么格式向KNN提供特定数据和标签的培训?数据应该是int还是float?它可以是char吗?

可以找到训练数据here

我也在使用opencv示例中的haarcascade_frontalface_alt.xml

现在KNN模型只有10个图像用于训练,以查看我的程序是否通过而没有错误,但事实并非如此。

这是我的代码:

import cv2
from numpy import float32 as np.float32

def chooseCascade():
    # TODO: Option for diferent cascades
    # HAAR Classifier for frontal face
    _cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
    return _cascade

def cropToObj(cascade,imageFile):
    # Load as 1-channel grayscale image
    image = cv2.imread(imageFile,0)

    # Crop to the object of interest in the image
    objRegion = cascade.detectMultiScale(image) # TODO: What if multiple ojbects in image?

    x1 = objRegion[0,0]
    y1 = objRegion[0,1]
    x1PlusWidth = objRegion[0,0]+objRegion[0,2]
    y1PlusHeight = objRegion[0,1]+objRegion[0,3]

    _objImage = image[y1:y1PlusHeight,x1:x1PlusWidth]

    return _objImage

def recognizer(fileNames):
    # ORB contructor
    orb = cv2.ORB(nfeatures=100)

    keyPoints = []
    descriptors = [] 

    # A cascade for face detection
    haarFaceCascade = chooseCascade()

    # Start processing images
    for imageFile in fileNames:
        # Find faces using the HAAR cascade
        faceImage = cropToObj(haarFaceCascade,imageFile)

        # Extract keypoints and description 
        faceKeyPoints, faceDescriptors = orb.detectAndCompute(faceImage, mask = None)

        #print faceDescriptors.shape
        descRow = faceDescriptors.shape[0]
        descCol = faceDescriptors.shape[1]

        flatFaceDescriptors = faceDescriptors.reshape(descRow*descCol).astype(np.float32)

        keyPoints.append(faceKeyPoints)
        descriptors.append(flatFaceDescriptors)

    print descriptors

    # KNN model and training on descriptors
    responses = []
    for name in fileNames:
        if name.startswith('BF'):
            responses.append(0) # Female
        else:
            responses.append(1) # Male

    knn = cv2.KNearest()
    knnTrainSuccess = knn.train(descriptors,
                                responses,
                                isRegression = False) # isRegression = false, implies classification

    # Obtain test face image from cam
    capture = cv2.VideoCapture(0)
    closeCamera = -1
    while(closeCamera < 0):
        _retval, _camImage = capture.retrieve()      

        # Find face in camera image
        testFaceImage = haarFaceCascade.detectMultiScale(_camImage) # TODO: What if multiple faces?

        # Keyponts and descriptors of test face image
        testFaceKP, testFaceDesc = orb.detectAndCompute(testFaceImage, mask = None)
        testDescRow = testFaceDesc.shape[0]
        flatTestFaceDesc = testFaceDesc.reshape(1,testDescRow*testDescCol).astype(np.float32) 

        # Args in knn.find_nearest: testData, neighborhood
        returnedValue, result, neighborResponse, distance = knn.find_nearest(flatTestFaceDesc,3) 

        print returnedValue, result, neighborResponse, distance


        # Display results
        # TODO: Overlay classification text
        cv2.imshow("testImage", _camImage)

        closeCamera = cv2.waitKey(1)
    cv2.destroyAllWindows()


if __name__ == '__main__':
    fileNames = ['BF09NES_gray.jpg', 
                 'BF11NES_gray.jpg', 
                 'BF13NES_gray.jpg', 
                 'BF14NES_gray.jpg', 
                 'BF18NES_gray.jpg', 
                 'BM25NES_gray.jpg', 
                 'BM26NES_gray.jpg', 
                 'BM29NES_gray.jpg', 
                 'BM31NES_gray.jpg', 
                 'BM34NES_gray.jpg']

    recognizer(fileNames)

目前我在knn.train()的行中收到错误,其中descriptors未被检测为numpy数组。

另外,这种做法完全错了吗?我是否应该使用其他方式进行性别分类?我对opencv facerec演示中的fisherface和eigenface示例不满意所以请不要指导我。

非常感谢任何其他帮助。谢谢。

---编辑---

我尝试过一些事情并想出答案。

我仍然希望SO社区中的某个人可以通过提出一个想法来帮助我,这样我就不需要将内容硬编码到我的解决方案中。我还怀疑knn.match_nearest()没有做我需要它做的事情。

正如预期的那样,识别器完全不准确,并且很容易因旋转,照明等而导致错误分类。任何有关改进此方法的建议都将非常受欢迎。

我用于培训的数据库是:Karolinska Directed Emotional Faces

2 个答案:

答案 0 :(得分:1)

我对所述方法的有效性/可行性有一些疑问。这是您可能想要考虑的另一种方法。 gen文件夹的内容为@ http://www1.datafilehost.com/d/0f263abc。正如您将注意到当数据大小变大(约10k训练样本)时,模型的大小可能变得不可接受(~100-200mb)。那么你需要研究pca / lda等。

import cv2
import numpy as np
import os

def feaCnt():
    mat = np.zeros((400,400,3),dtype=np.uint8)
    ret = extr(mat)
    return len(ret)

def extr(img):
    return sobel(img)

def sobel(img):
    gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    klr = [[-1,0,1],[-2,0,2],[-1,0,1]]
    kbt = [[1,2,1],[0,0,0],[-1,-2,-1]]
    ktb = [[-1,-2,-1],[0,0,0],[1,2,1]]
    krl = [[1,0,-1],[2,0,-2],[1,0,-1]]
    kd1 = [[0,1,2],[-1,0,1],[-2,-1,0]]
    kd2 = [[-2,-1,0],[-1,0,1],[0,1,2]]    
    kd3 = [[0,-1,-2],[1,0,-1],[2,1,0]]
    kd4 = [[2,1,0],[1,0,-1],[0,-1,-2]]
    karr = np.asanyarray([
        klr,
        kbt,
        ktb,
        krl,
        kd1,
        kd2,
        kd3,
        kd4
        ])
    gray=cv2.resize(gray,(40,40))
    res =  np.float32([cv2.resize(cv2.filter2D(gray, -1,k),(15,15)) for k in karr])
    return res.flatten()


root = 'C:/data/gen'

model='c:/data/models/svm/gen.xml'
imgs = []
idx =0
for path, subdirs, files in os.walk(root):
  for name in files:  
    p =path[len(root):].split('\\')
    p.remove('')
    lbl = p[0]
    fpath = os.path.join(path, name)
    imgs.append((fpath,int(lbl)))
    idx+=1

samples = np.zeros((len(imgs),feaCnt()),dtype = np.float32)
labels = np.zeros(len(imgs),dtype = np.float32)

i=0.
for f,l in imgs:
  print i
  img = cv2.imread(f)
  samples[i]=extr(img)
  labels[i]=l
  i+=1

svm = cv2.SVM()
svmparams = dict( kernel_type = cv2.SVM_POLY, 
                       svm_type = cv2.SVM_C_SVC,
                       degree=3.43,
                       gamma=1.5e-4,
                       coef0=1e-1,
                       )
print 'svm train'
svm.train(samples,labels,params=svmparams)
svm.save(model)
print 'done'

result = np.float32( [(svm.predict(s)) for s in samples])
correct=0.
total=0.

for i,j in zip(result,labels):
    total+=1
    if i==j:
      correct+=1
    print '%f'%(correct/total)

答案 1 :(得分:1)

以前,我很难找到ORB,SIFT,SURF等之间的技术差异,我发现这些SO帖子很有帮助:

最值得注意的是,opencv中的这些特征检测算法需要单个通道(通常为8位)灰度图像。

事实证明knn.train()只能接受数据类型为'32 bit floating-point'的'array'。我相信opencv中的SVM培训也有这个要求。在python中,numpy数组需要在每一行中具有相同类型的数据,并且所有行都需要具有相同的形状,这与python列表不同,python列表可以包含任何类型和大小的数据。

因此,在生成描述符列表后,我将列表转换为数组。

但是!在此之前,我将ORB nfeatures参数硬编码为25.我的所有训练数据图像的分辨率大致相同,我能够手动验证每个图像是否可以使用ORB生成至少25个关键点。每个关键点有32个描述符,因此25 * 32为每个面部图像提供800个描述符。 ORB返回一个数组,其元素是整数类型,行数等于关键点数。我将其重新塑造成一行描述符,以生成大小为800的“向量”。

下一个挑战是使用knn.find_nearest()。它需要一个'矩阵',其行的形状与赋予knn.train()的ndarray的行相同。不这样做会产生错误:

OpenCV Error: Bad argument (Input samples must be floating-point matrix (<num_samples>x<var_count>)) in find_nearest

即使你有一个需要传递给knn.find_nearest()的向量,它也需要是1xm的形状,其中m是向量中元素的数量。

因此,我不得不采用粗暴的方式检查我的网络摄像头拍摄的图像是否可以在我的硬编码方法中使用。

代码现在看起来像这样:

import cv2
import numpy as np

def chooseCascade():
    # TODO: Option for diferent cascades
    # HAAR Classifier for frontal face
    _cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
    return _cascade

def cropToObj(cascade,imageFile,flag):
    if flag == 0:
        # Load as 1-channel grayscale image
        image = cv2.imread(imageFile,0)
    elif flag == 1:
        # Load as 3-channel color image
        image = cv2.imread(imageFile,1)
    elif flag == -1: 
        # Load image as is 
        image = cv2.imread(imageFile,-1)
    elif flag == 2:
        # Image is from camera
        image = imageFile
    else:
        print 'improper arguments passed to cropToObj'

    # Crop to the object of interest in the image
    objRegion = cascade.detectMultiScale(image) # TODO: What if multiple ojbects in image?

    x1 = objRegion[0,0]
    y1 = objRegion[0,1]
    x1PlusWidth = objRegion[0,0]+objRegion[0,2]
    y1PlusHeight = objRegion[0,1]+objRegion[0,3]

    objImage = image[y1:y1PlusHeight,x1:x1PlusWidth]

    return objImage

def recognizer(fileNames):
    # ORB contructor
    orb = cv2.ORB(nfeatures=25)

    keyPoints = []
    descriptors = [] 

    # A cascade for face detection
    haarFaceCascade = chooseCascade()

    # Start processing images
    for imageFile in fileNames:
        # Find faces using the HAAR cascade
        faceImage = cropToObj(haarFaceCascade,imageFile,flag)

        # Extract keypoints and description 
        faceKeyPoints, faceDescriptors = orb.detectAndCompute(faceImage, mask = None)

        #print faceDescriptors.shape
        descRow = faceDescriptors.shape[0]
        descCol = faceDescriptors.shape[1]

        flatFaceDescriptors = faceDescriptors.reshape(descRow*descCol)

        keyPoints.append(faceKeyPoints)
        descriptors.append(flatFaceDescriptors)

    descriptors = np.asarray(descriptors, dtype=np.float32)

    # KNN model and training on descriptors
    responses = []
    for name in fileNames:
        if name.startswith('BF'):
            responses.append(0) # Female
        else:
            responses.append(1) # Male

    responses = np.asarray(responses)

    knn = cv2.KNearest()
    knnTrainSuccess = knn.train(descriptors,
                                responses,
                                isRegression = False) # isRegression = false, implies classification

    # Obtain test face image from cam
    capture = cv2.VideoCapture(0)
    closeCamera = -1
    while(closeCamera < 0):
        retval, camImage = capture.read()      

        # Find face in camera image
        try:
            testFaceImage = cropToObj(haarFaceCascade, camImage, 2) # TODO: What if multiple faces?
            testFaceImage = cv2.cvtColor(testFaceImage, cv2.COLOR_BGR2GRAY)
        except TypeError:
            print 'check if front face is visible to camera'
            pass

        # Keyponts and descriptors of test face image
        testFaceKP, testFaceDesc = orb.detectAndCompute(testFaceImage, mask = None)
        testDescRow = testFaceDesc.shape[0]
        testDescCol = testFaceDesc.shape[1]
        flatTestFaceDesc = testFaceDesc.reshape(1,testDescRow*testDescCol)
        flatTestFaceDesc = np.asarray(flatTestFaceDesc,dtype=np.float32) 

        if flatTestFaceDesc.size == 800:
            # Args in knn.find_nearest: testData, neighborhood
            returnedValue, result, neighborResponse, distance = knn.find_nearest(flatTestFaceDesc,5)
            if returnedValue == 0.0:
                print 'Female'
            else:
                print 'Male'
        else: 
            print 'insufficient size of image' 

        # Display results
        # TODO: Overlay classification text
        cv2.imshow("testImage", camImage)

        closeCamera = cv2.waitKey(1)
    cv2.destroyAllWindows()


if __name__ == '__main__':
    fileNames = ['BF09NES_gray.jpg', 
                 'BF11NES_gray.jpg', 
                 'BF13NES_gray.jpg', 
                 'BF14NES_gray.jpg', 
                 'BF18NES_gray.jpg', 
                 'BM25NES_gray.jpg', 
                 'BM26NES_gray.jpg', 
                 'BM29NES_gray.jpg', 
                 'BM31NES_gray.jpg', 
                 'BM34NES_gray.jpg']

    recognizer(fileNames)

我仍然希望SO社区中的某个人可以通过提出一个想法来帮助我,这样我就不需要将内容硬编码到我的解决方案中。我还怀疑knn.match_nearest()没有做我需要它做的事情。

正如预期的那样,识别器完全不准确,并且很容易因旋转,照明等而导致错误分类。任何有关改进此方法的建议都将非常受欢迎。