简体   繁体   English

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

[英]How to predict my own images and check if they match using SVM classifier

I followed a tutorial and trained a model using SVM.我按照教程使用 SVM 训练了 model。 When I test the model using its test set it predicts, but I want to upload my own images and compare them if they match and then print the accuracy of the result.当我使用它预测的测试集测试 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)

It shows表明

ValueError: setting an array element with a sequence.

First of all, do the 2 images have the same size?首先,两张图片的大小是否相同?

Second, we need to have a 2D input for the SVC model.其次,我们需要为 SVC model 提供 2D 输入。 So you need to flatten your 3D images and create a feature matrix with dimension [# images, # pixels in image].因此,您需要展平您的 3D 图像并创建尺寸为 [# images, #pixels in image] 的特征矩阵。

Toy example (in reality we need to fit the model using the training set and predict using a test set).玩具示例(实际上我们需要使用训练集拟合 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM