簡體   English   中英

如何預測我自己的圖像並使用 SVM 分類器檢查它們是否匹配

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM