【问题标题】:how to convert image to dataset to process machine learning如何将图像转换为数据集以处理机器学习
【发布时间】:2018-02-04 09:54:48
【问题描述】:

如何将图像转换为数据集或 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()

请大家帮忙,谢谢!!!

【问题讨论】:

  • 你能详细说明你想解决的具体问题吗?

标签: python image numpy image-processing machine-learning


【解决方案1】:

在单个图像上使用 SVM 没有其他原因,只是因为这样做很有趣。这是我所做的修复。 1)使用 .convert("L") 将图像转换为二维数组灰度。 2) 创建一个虚拟目标变量 y 作为随机一维数组。 3) 修复再次显示图像的类型错误(plt.imshow)cmap(而不是camp)和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-learn website 中看到了一个使用 SVM 的好例子。我想这就是您要复制的内容。不是吗?

【讨论】:

    猜你喜欢
    • 2019-05-06
    • 2012-07-31
    • 2017-11-08
    • 1970-01-01
    • 2014-02-17
    • 2018-08-18
    • 1970-01-01
    • 2021-12-06
    相关资源
    最近更新 更多