如何预测我自己的图像,并使用SVM分类器检查它们是否匹配



我遵循了一个教程,并使用SVM训练了一个模型。当我使用它的测试集测试模型时,它会预测,但我想上传我自己的图像,如果它们匹配,进行比较,然后打印结果的准确性。

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模型的2D输入。因此,您需要将3D图像展平,并创建一个尺寸为[#images,#pixels in images]的特征矩阵。

玩具示例(实际上,我们需要使用训练集拟合模型,并使用测试集进行预测(。

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]

最新更新