错误消息:函数“setSize”中的错误:(215:断言失败)s>=0

2024-04-27 15:52:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用opencv4python教科书作为指导。我有一组图像,我正在用它来训练一个机器学习算法,以检测LED是绿色,红色还是关闭。我的步骤是:

  1. 从文件中使用globcv.imread图像。你知道吗
  2. 可选图像处理
  3. 创建标签
  4. 使用KNNSVM(或一些监督学习算法)

除了ML算法,我什么都有。为什么不管我怎么做都不能让这个算法工作?你知道吗

我将粘贴代码的简化版本。你知道吗

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

据我所知:

我知道样本应该是np.float32&;UMat。 样本长度应与响应长度相同。你知道吗

我正在Linux上使用Ubuntu,PyCharm是我的DE

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)

我得到错误消息:

model.train(np.float32(samples3), cv.ml.ROW_SAMPLE,         np.float32(responses))
cv2.error: OpenCV(4.1.0)     /io/opencv/modules/core/src/matrix.cpp:235: error: (-215:Assertion     failed) s >= 0 in function 'setSize'

Tags: informodelnpresponsesmlcvr3
1条回答
网友
1楼 · 发布于 2024-04-27 15:52:37

我通过确保样本和响应是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)

相关问题 更多 >