2017-08-26 176 views
-3

如何将图像转换为数据集或numpy的阵列,并预测由fiting它CLF如何转换图像数据集处理机器学习

import PIL as pillow 
from PIL import Image 
import numpy as np 
import matplotlib.pyplot as plt 
from sklearn import svm 

infilename=input() 
im=Image.open(infilename) 
imarr=np.array(im) 
flatim=imarr.flatten('F') 

clf=svm.SVC(gamma=0.0001,C=100) 
x,y=im.size 

#how to fit the numpy array to clf 
clf.fit(flatim[:-1],flatim[:-1]) 
print("prediction:",clf.predict(flatim[-1])) 
plt.imshow(flatim,camp=plt.cm.gray_r,interpolation='nearest') 
plt.show() 

任何人,请和感谢!

+0

你能详细解释你想解决的确切问题吗? –

回答

0

除了这样做的乐趣之外,在单个图像上没有其他原因使用SVM。这是我做的修复。 1)使用.convert(“L”)将图像转换为二维数组灰度。 2)创建一个虚拟目标变量y作为随机一维数组。 3)固定类型的错误再次显示图像(plt.imshow)CMAP(而不是营)和IM(代替flatim)

import PIL as pillow 
from PIL import Image 
import numpy as np 
import matplotlib.pyplot as plt 
from sklearn import svm 

im=Image.open("sample.jpg").convert("L") 
imarr=np.array(im) 
flatim=imarr.flatten('F') 

clf=svm.SVC() 
#X,y=im.size 
X = imarr 
y = np.random.randint(2, size=imarr.shape[0]) 
clf.fit(X, y) 

#how to fit the numpy array to clf 
#clf.fit(flatim[:-1],flatim[:-1]) 
# I HAVE NO IDEA WHAT I"M DOING HERE! 
print("prediction:", clf.predict(X[-2:-1])) 
plt.imshow(im,cmap=plt.cm.gray_r,interpolation='nearest') 
plt.show() 

我看到在一个很好的例子scikit学习使用SVM的website。我想这就是你想要复制的东西。是不是?

相关问题