python-2.7 - 以下代码实现 knn 有什么问题?

标签 python-2.7 opencv knn

这是我发现的修改后的代码 here .

import numpy as np
import cv2
from matplotlib import pyplot as plt

img = cv2.imread('digits.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# Now we split the image to 5000 cells, each 20x20 size
cells = [np.hsplit(row,100) for row in np.vsplit(gray,50)]

# Make it into a Numpy array. It size will be (50,100,20,20)
x = np.array(cells)

# Now we prepare train_data.
train = x[:,:50].reshape(-1,400).astype(np.float32) # Size = (2500,400)


img = cv2.imread('1.png')
img1 = cv2.imread('2.png')
img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
img1 = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY)
img = cv2.resize(img, (20,20)).astype(np.float32)
img1 = cv2.resize(img1, (20,20)).astype(np.float32)
img = img.flatten()
img1 = img1.flatten()
arr = [img,img1]
arr = np.asarray(arr)
# Create labels for train and test data
k = np.arange(10)
train_labels = np.repeat(k,250)[:,np.newaxis]

# Initiate kNN, train the data, then test it with test data for k=1

knn = cv2.ml.KNearest_create()
knn.train(train, 0,train_labels)
ret, result, neighbours, dist = knn.findNearest(arr, k=5)
for i in result:
    print i
# save the data
np.savez('knn_data.npz',train=train, train_labels=train_labels)

# Now load the data
with np.load('knn_data.npz') as data:
    print data.files
    train = data['train']
    train_labels = data['train_labels']

效果很好。但我不知道如何使用这个 knn_data.npz 文件。 这是我的尝试:

import numpy as np
import cv2
from matplotlib import pyplot as plt

with np.load('knn_data.npz') as data:
    print data.files
    train = data['train']
    train_labels = data['train_labels']

    img = cv2.imread('1.png')
    img1 = cv2.imread('2.png')

    img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    img1 = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY)

    img = cv2.resize(img, (20,20)).astype(np.float32)
    img1 = cv2.resize(img1, (20,20)).astype(np.float32)

    img = img.flatten()
    img1 = img1.flatten()

    arr = [img,img1]
    arr = np.asarray(arr)

    knn = cv2.ml.KNearest_create()
    ret, result, neighbours, dist = knn.findNearest(arr, k=5)
    for i in result:
        print i

我收到的无法修复的错误消息是:

OpenCV Error: Assertion failed (test_samples.type() == 5 && test_samples.cols == samples.cols) in findNearest, file /io/opencv/modules/ml/src/knearest.cpp, line 325

Traceback (most recent call last): File "knn1.py", line 20, in ret, result, neighbours, dist = knn.findNearest(img, k=5) cv2.error: /io/opencv/modules/ml/src/knearest.cpp:325: error: (-215) test_samples.type() == 5 && test_samples.cols == samples.cols in function findNearest

我在 ubuntu 18.04 中的 python 2.7.15 上使用 opencv 3.2.0。文件 1.png2.png 是 RGB 图像文件。

最佳答案

在您的示例中,您创建了变量 traintrain_labels 但从未使用它们。

在调用 knn.findNearest(arr, k=5) 之前的任意位置添加以下内容:

train = data['train']
train_labels = data['train_labels']
knn = cv2.ml.KNearest_create()
knn.train(train, 0,train_labels)

关于python-2.7 - 以下代码实现 knn 有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50719793/

相关文章:

python - 使用 boto 连接到共享 SQS 队列

Python OpenCV 人脸检测代码有时会引发 `' 元组'对象没有属性 'shape'`

c# - 在 emgu CV 中使用 SURF 进行对象识别

word2vec - 使用 fasttext 或 word2vec 计算最近的文档

hadoop - 如何使用基于 KNN 的项目推荐器从命令行运行 mahout?

python-2.7 - Python 'except' 失败

python - 我是否正确地为曲线拟合模型编写了代码?除了线性回归之外,每个模型的这条线都偏离得很远

python - 将 Python 2.7 和 3.x 解释器添加到 PyCharm

c++ - OpenCV:全景拼接 - 忽略图像的一部分

elasticsearch - 弹性 KNN 搜索候选数与分片大小