【问题标题】:SVM to gender recognitionSVM 到性别识别
【发布时间】:2015-07-09 02:46:14
【问题描述】:

这几周我一直在从事一个性别识别项目(在 python 中),起初使用的是:Fisherfaces as Feature Extraction method 和 1-NN classifier with Euclidean Distance 但现在我虽然还不够可靠(以我的拙见) 所以我即将使用 SVM,但是当我必须创建和训练模型以在我的图像数据集中使用它时我迷路了,但我在 http://scikit-learn.org 中找不到我需要的命令的解决方案。 我试过这个代码,但它不起作用,不知道为什么 执行时出现此错误:

  File "prueba.py", line 46, in main
    clf.fit(R, r)
  File "/Users/Raul/anaconda/lib/python2.7/site-packages/sklearn/svm/base.py", line 139, in fit    
 X = check_array(X, accept_sparse='csr', dtype=np.float64, order='C')
  File "/Users/Raul/anaconda/lib/python2.7/site-packages/sklearn/utils/validation.py", line 350, in check_array
    array.ndim)
ValueError: Found array with dim 3. Expected <= 2

这是我的代码:

import os, sys
import numpy as np
import PIL.Image as Image
import cv2
from sklearn import svm


def read_images(path, id, sz=None):
    c = id
    X,y = [], []
    for dirname, dirnames, filenames in os.walk(path):
        for subdirname in dirnames:
            subject_path = os.path.join(dirname, subdirname)
            for filename in os.listdir(subject_path):
                try:
                    im = Image.open(os.path.join(subject_path, filename))
                    im = im.convert("L")
                    # resize to given size (if given)
                    if (sz is not None):
                        im = im.resize(sz, Image.ANTIALIAS)
                    X.append(np.asarray(im, dtype=np.uint8))
                    y.append(c)
                except IOError as e:
                    print "I/O error({0}): {1}".format(e.errno, e.strerror)
                except:
                    print "Unexpected error:", sys.exc_info()[0]
                    raise
                        #c = c+1
    return [X,y]


def main():
    # check arguments
    if len(sys.argv) != 3:
        print "USAGE: example.py </path/to/images/males> </path/to/images/females>"
        sys.exit()
    # read images and put them into Vectors and id's
    [X,x] = read_images(sys.argv[1], 1)
    [Y, y] = read_images(sys.argv[2], 0)
    # R all images and r all id's
    [R, r] = [X+Y, x+y]
    clf = svm.SVC()
    clf.fit(R, r)





if __name__ == '__main__':
    main()

对于如何使用 SVM 进行性别识别,我将不胜感激 感谢阅读

【问题讨论】:

    标签: python machine-learning svm libsvm


    【解决方案1】:
    X.append(np.asarray(im, dtype=np.uint8))
    

    我猜这是附加一个二维数组。您可能想在附加之前flatten 它,以便每个实例都变成这样:

    array([255, 255, 255, ..., 255, 255, 255], dtype=uint8)
    

    代替:

    array([
       [255, 255, 255, ..., 255, 255, 255],
       [255, 255, 255, ..., 255, 255, 255],
       [255,   0,   0, ...,   0,   0,   0],
       ..., 
       [255,   0,   0, ...,   0,   0,   0],
       [255, 255, 255, ..., 255, 255, 255],
       [255, 255, 255, ..., 255, 255, 255]], dtype=uint8)
    

    试试这个:

    X.append(np.asarray(im, dtype=np.uint8).ravel())
    

    【讨论】:

    • 感谢@greeness,现在我可以创建模型进行预测。现在我所要做的就是加载我想要分析的面部图像并制作一个 clf.predict(of the image) 如果我得到的结果是 1 是男人和女人,如果我得到 0?感谢回复
    猜你喜欢
    • 1970-01-01
    • 2012-05-29
    • 2015-03-14
    • 2019-08-15
    • 2014-05-04
    • 2013-12-13
    • 2017-12-09
    • 2017-06-13
    • 2012-10-23
    相关资源
    最近更新 更多