简体   繁体   中英

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

I followed a tutorial and trained a model using SVM. 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.

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. So you need to flatten your 3D images and create a feature matrix with dimension [# images, # pixels in image].

Toy example (in reality we need to fit the model using the training set and predict using a test set).

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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