python - OpenCV不接受ndarray到TextDecectorCNN.detect()

标签 python numpy opencv

我正在尝试使用文本检测器的this OpenCV class来确定是否可以将其用于我的项目。我正在寻找这样的图像:

src = cv2.imread(img_path)

然后我试图像这样使用前面提到的功能:
bbox, confidence = cv2.text_TextDetectorCNN.detect(src)

但是此代码存在以下错误:
TypeError: descriptor 'detect' for 'cv2.text_TextDetectorCNN' objects doesn't apply to a 'numpy.ndarray' object

我已经尝试过使用image = cv2.cvtColor(src, cv2.COLOR_BGR2RGB, cv2.CV_8UC3),但是它没有做任何事情-image仍然是ndarray。

UPD:附加整个代码和有问题的图片。

import os


# borrowed from https://github.com/lengstrom/fast-style-transfer/blob/master/src/utils.py
def list_images(in_path, formats=[".jpg", ".jpeg", ".gif", ".png", ".pgm", ".bmp"]):
    img_files = []
    for (dirpath, _, filenames) in os.walk(in_path):
        for file in filenames:
            _, ext = os.path.splitext(file)
            ext = str.lower(ext)
            if ext in formats:
                img_files.append(os.path.join(dirpath, file))
    return img_files


def test_accuracy(folder_path):
    image_list = list_images(folder_path)
    for img_path in image_list:
        src = cv2.imread(img_path)
        bbox, confidence = cv2.text_TextDetectorCNN.detect(src)
        print(img_path, bbox, confidence) # To check format of output

测试的图像:image_1
(我已经测试了3个不同的.jpg图像,但没有一个起作用)

问题:有没有一种方法可以将ndarray转换为上述函数可以接受的某些OpenCV格式?

最佳答案

您的问题是您没有创建文本检测器的实例。看到这个:

import cv2
import numpy as np 

# After downloading the caffemodel and
# textbox.prototxt you have to create
# an instance of your model
textSpotter = cv2.text.TextDetectorCNN_create(
    "textbox.prototxt", "TextBoxes_icdar13.caffemodel"
)
src = cv2.imread("1.png")
bbox, confidence = textSpotter.detect(src)
print("HELLO", bbox, confidence)  # To check format of output

关于python - OpenCV不接受ndarray到TextDecectorCNN.detect(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61329523/

相关文章:

python - 无法在python 3.6中导入cv2 OpenCV 2.4.13

python - 在opencv python中应用基于flan的匹配器时出错

python - 通过 lmfit 模型在 python 中最小化拟合两个洛伦兹

python - Python 中的异或实现给出了错误的预测

python - 在 numpy 中索引多个不相邻的范围

python - 将 pandas 矩阵转换为 DataFrame

c++ - 取实时网络摄像头帧的平均值

python - pandas DataFrame 中行之间的线性插值

opencv - Anaconda-手动添加内置程序包

python - 使用python,opencv和libtiff无法保存tiff图像16位。生物科学