[英]How to predict my own images and check if they match using SVM classifier
我按照教程使用 SVM 训练了 model。 当我使用它预测的测试集测试 model 时,我想上传我自己的图像并比较它们是否匹配,然后打印结果的准确性。
img1 = imageio.imread("test-1.jpg")
img2 = imageio.imread("test-2.jpg")
myTest = []
myTest.append(img1)
myTest.append(img2)
pred = svc_1.predict(myTest)
表明
ValueError: setting an array element with a sequence.
首先,两张图片的大小是否相同?
其次,我们需要为 SVC model 提供 2D 输入。 因此,您需要展平您的 3D 图像并创建尺寸为 [# images, #pixels in image] 的特征矩阵。
玩具示例(实际上我们需要使用训练集拟合 model 并使用测试集进行预测)。
import numpy as np
from sklearn.svm import SVC
img1 = imageio.imread("test-1.jpg")
img2 = imageio.imread("test-2.jpg")
y = [0,1] #labels for the classification
myTest = []
myTest.append(np.array(img1).ravel()) # ravel the 3D images into a vector
myTest.append(np.array(img2).ravel())
svc_1 = SVC().fit(myTest,y ) # fit the model
pred = svc_1.predict(myTest) # predict
print(pred)
# [1 1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.