python - SVM to gender recognition -
i've been working weeks in gender recognition project (in python) using @ first: fisherfaces feature extraction method , 1-nn classifier euclidean distance though not enough reliable (in humble opinion) i'm use svm im lost when have create , train model use in dataset of images, can't find solution commands need in http://scikit-learn.org. i've tried code doesn't work, dunno why have error while executing:
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 dim 3. expected <= 2
and code:
import os, sys import numpy np import pil.image image import cv2 sklearn import svm def read_images(path, id, sz=none): c = id x,y = [], [] dirname, dirnames, filenames in os.walk(path): subdirname in dirnames: subject_path = os.path.join(dirname, subdirname) filename in os.listdir(subject_path): try: im = image.open(os.path.join(subject_path, filename)) im = im.convert("l") # resize given size (if given) if (sz not none): im = im.resize(sz, image.antialias) x.append(np.asarray(im, dtype=np.uint8)) y.append(c) except ioerror 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 , put them vectors , id's [x,x] = read_images(sys.argv[1], 1) [y, y] = read_images(sys.argv[2], 0) # r images , r id's [r, r] = [x+y, x+y] clf = svm.svc() clf.fit(r, r) if __name__ == '__main__': main()
i'd appreciate kind of in how can make gender recognition svm reading
x.append(np.asarray(im, dtype=np.uint8))
i guess appending 2d-array. might want flatten before appending each instance becomes looking:
array([255, 255, 255, ..., 255, 255, 255], dtype=uint8)
instead of:
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)
try this:
x.append(np.asarray(im, dtype=np.uint8).ravel())
Comments
Post a Comment