错误消息:错误:(-215:声明失败)>'setSize'

时间:2019-06-05 00:03:43

标签: python image opencv

我正在使用OpenCV4 Python教科书作为准则。我有一组图像用于训练机器学习算法,以检测LED是绿色,红色还是熄灭。我的步骤是:

  1. 使用globcv.imread来自文件的图像。
  2. 可选的图像处理
  3. 创建标签
  4. 使用KNNSVM(或某些受监督的学习算法)

除了ML算法正常工作外,我什么都做得到。无论尝试什么,为什么都无法使该算法正常工作?

我将粘贴代码的简化版本。

尝试将响应和样本转换为np.float32,从KNN转换为SVM,将我的图像转换为单通道,重塑numpy数组,将样本转换为{{1 }}。

据我所知:

我知道样本应为UMatnp.float32。 样本的长度应与响应的长度相同。

我在Linux上使用Ubuntu,而PyCharm作为我的DE。以下是一些训练图像示例:https://drive.google.com/drive/folders/1AQ2fmCdtpTiSQZNLiKLKXNm1VwvRUrUI?usp=sharing

UMat

我收到错误消息:

import cv2 as cv
import numpy as np
import glob

def unpack(paths):
    files = []
    for path in paths:
        for file in glob.glob(path + '/*.jpg'):
            files.append(file)
    return files

def grab(file_paths):
    grabbed_imgs = []
    for fl in file_paths:
        grabbed_imgs.append(cv.imread(str(fl)))
    return grabbed_imgs

def get(grabbed_imgs, position):
    # pulls coordinates of LED, left out for simplicity. 
    # let xpt = (300, 350), ypt = (250, 300)
    xpt, ypt = locate_squares() 
    xpt = np.squeeze(xpt)
    ypt = ypt[position]
    ypt = np.squeeze(ypt)
    # set all values that aren't the LED location to black
    masked = []
    if len(grabbed_imgs) != 0:
        for im in grabbed_imgs:
        masked = np.zeros_like(im)
        xpt = [int(x) for x in xpt]
        ypt = [int(y) for y in ypt]
        for x in range(xpt[0], xpt[1]):
            for y in range(ypt[0], ypt[1]):
                for d in range(2):
                    masked[y][x][d] = im[y][x][d]
    return masked


g3 = ['/home/me/myFolderPath']
r3 = ['/home/me/otherFolderPath']

g3 = unpack(g3)
r3 = unpack(r3)


g3_images = grab(g3)
r3_images = grab(r3)

# g3 green r3 red.  For simplicity, I am leaving the off state out
g3 = np.squeeze(get(g3_images, position=3))
r3 = np.squeeze(get(r3_images, position=3))
samples3 = np.concatenate((g3, r3), 0)

# labels:  1 stands for green 2 stands for red
a = [1] * len(g3)
b = [2] * len(r3)
responses = a + b

print(len(samples3), len(responses), responses) # is as expected
>>> 75 75 [1,1,1,1,1...,2,2,2,2,2,2,2,...]

# SVM Machine Learning
gamma = 0.50625
C = 12.5
model = cv.ml.SVM_create()
model.setGamma(gamma)
model.setC(C)
model.setKernel(cv.ml.SVM_C_SVC)
model.setType(cv.ml.SVM_C_SVC)
model.setTermCriteria((cv.TERM_CRITERIA_MAX_ITER, 100, 1e-6))

model.train(np.float32(samples3), cv.ml.ROW_SAMPLE,     np.float32(responses))

# I also tried the KNN ML algorithm
knn = cv.ml.KNearest_create()
knn.train(samples3, cv.ml.ROW_SAMPLE, responses)

1 个答案:

答案 0 :(得分:0)

我通过确保样本和响应为Numpy数组解决了这个问题。

def grab(file_paths):
    grabbed_imgs = []
    i = 0
    '''
    locate_squares() gets x and y coords for LED square 
    ex: x=[300,350] y=[200,250]
    '''
    x, y = locate_squares() 
    for fl in file_paths:
        '''
        this for loop helped fixed things for me
        '''
        pic = cv.imread(fl)
        # instead of function get()
        pic_crop = pic[y[0]:y[1], x[0]:x[1]]
        pic_flat = pic_crop.flatten() 
        pic32 = np.float32(pic_flat)

        grabbed_imgs.append(pic32)
    grabbed_imgs = np.squeeze(grabbed_imgs)
    return grabbed_imgs

g3 = ['/home/me/myFolderPath']
r3 = ['/home/me/otherFolderPath']

g3 = unpack(g3)
r3 = unpack(r3)

g3_images = grab(g3)
r3_images = grab(r3)

g3 = np.squeeze(get(g3_images, position=3))
r3 = np.squeeze(get(r3_images, position=3))
samples3 = np.concatenate((g3, r3), 0)

# labels:  1 stands for green 2 stands for red
a = [1] * len(g3)
b = [2] * len(r3)
responses = a + b

'''
switching to np arrays helped fixed things for me
'''
responses = np.array(responses)
samples3 = np.concatenate((g3, r3), 0)
# or could use np.vstack

knn = cv.ml.KNearest_create()
knn.train(samples3, cv.ml.ROW_SAMPLE, responses)