Python 如何将图像转换为数据集进行机器学习

Python 如何将图像转换为数据集进行机器学习,python,image,numpy,image-processing,machine-learning,Python,Image,Numpy,Image Processing,Machine Learning,如何将图像转换为数据集或numpy数组,并通过将其与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

如何将图像转换为数据集或numpy数组,并通过将其与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()

任何人都请,谢谢

在单个图像上使用SVM除了好玩之外,没有其他原因。以下是我做的修复。1) 使用.convert(“L”)将图像转换为二维阵列灰度。2) 创建了一个虚拟目标变量y作为随机1D数组。3) 修复再次显示图像(plt.imshow)cmap(代替camp)和im(代替flatim)时出现的类型错误


我在scikit中看到了一个使用SVM的好例子。我想这就是你想要复制的。不是吗?

你能详细说明你想解决的确切问题吗?
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()