gpt4 book ai didi

python - OpenCV断言在函数 'setData'中失败

转载 作者:行者123 更新时间:2023-12-02 16:09:01 24 4
gpt4 key购买 nike

我阅读了这个 SOF Simple Digit Recognition OCR in OpenCV-Python

然后,我尝试自己运行以下代码:

 import cv2
import numpy as np


samples = np.loadtxt('generalsamples.data',np.float32)
responses = np.loadtxt('generalresponses.data',np.float32)
responses = responses.reshape((responses.size,1))

print "samples: \n",samples
print "respnses: \n",responses

model = cv2.ml.KNearest_create()
model.train(samples,cv2.ml.ROW_SAMPLE,responses)

输出:
samples:

[ 1. 1. 1. 1. 1. 1. 1. 1. 1. 65. 0. 0. 0. 0. 0.
0. 0. 1. 65. 0. 0. 0. 0. 0. 0. 0. 1. 49. 16. 0.
0. 0. 0. 0. 0. 1. 1. 65. 0. 0. 0. 0. 0. 0. 1.
1. 65. 0. 0. 0. 0. 0. 0. 0. 1. 65. 0. 0. 0. 0.
0. 0. 0. 1. 65. 0. 0. 0. 0. 0. 0. 0. 1. 65. 0.
0. 0. 0. 0. 0. 0. 1. 11. 55. 0. 0. 0. 0. 0. 0.
1. 4. 62. 0. 0. 0. 0. 0. 0. 0.]

respnses:
[[ 7.]]

错误:
Traceback (most recent call last):
File "test.py", line 10, in <module>
model.train(samples,cv2.ml.ROW_SAMPLE,responses)
cv2.error: OpenCV(4.0.0-pre) /home/shajal/opencv/opencv/modules/ml/src/data.cpp:259: error: (-215:Assertion failed) samples.type() == 5 || samples.type() == 4 in function 'setData'

我该怎么办?

我已经在Ubuntu计算机上重新安装了Opencv,以解决此问题。

最佳答案

当您使用 model.train(samples,cv2.ml.ROW_SAMPLE,responses)时,请检查以下内容:

响应的

  • 维度必须为(n,1),其中n个样本长度为(n,m)。
  • 样本类型必须为np.int64
  • 关于python - OpenCV断言在函数 'setData'中失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49994760/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com