0
我一直在本週在一個性別識別項目(在python中)使用的第一:Fisherfaces作爲特徵提取方法和1-NN分類器與歐幾里得距離,但現在我雖然它不夠可靠(在我的愚見),所以即將使用支持向量機,但即時通訊失去了,當我必須創建和訓練一個模型使用它在我的圖像數據集,但我找不到我需要在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 感謝您閱讀
感謝@greeness,現在我可以創建該模型進行預測。現在我所要做的就是加載我想要分析的臉部圖像並製作一個clf.predict(圖像),如果我得到的結果是1,那麼如果我得到0,結果是男人還是女人?感謝您的回覆 –
是的。說得通。 – greeness