在 Python 中加载 OpenCV EAST 文本检测器时出错 [英] Error on loading OpenCV EAST text detector in Python

查看:66
本文介绍了在 Python 中加载 OpenCV EAST 文本检测器时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 EAST 文本检测器来检测图像中的文本区域,但在加载预训练的 EAST 文本检测器时遇到问题.

I'm trying to use EAST text detector to detect areas of text in images, but am having trouble on loading the pre-trained EAST text detector.

以下是我的text_detection.py文件

The following is my text_detection.py file

from imutils.object_detection import non_max_suppression
import numpy as np
import argparse
import time
import cv2
import requests
import urllib

# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", type=str,help="path to input image")
ap.add_argument("-east", "--east", type=str,help="path to input EAST text detector")
ap.add_argument("-c", "--min-confidence", type=float, default=0.5,help="minimum probability required to inspect a region")
ap.add_argument("-w", "--width", type=int, default=320,help="resized image width (should be multiple of 32)")
ap.add_argument("-e", "--height", type=int, default=320,help="resized image height (should be multiple of 32)")
args = vars(ap.parse_args())

# load the input image and grab the image dimensions
req = urllib.request.urlopen('https://hips.hearstapps.com/ghk.h-cdn.co/assets/18/02/mandy-hale-inspirational-quote.jpg')
arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
image = cv2.imdecode(arr, -1)
orig = image.copy()

(H, W) = image.shape[:2]

# set the new width and height and then determine the ratio in change
# for both the width and height
(newW, newH) = (args["width"], args["height"])
rW = W / float(newW)
rH = H / float(newH)

# resize the image and grab the new image dimensions
image = cv2.resize(image, (newW, newH))
(H, W) = image.shape[:2]

# define the two output layer names for the EAST detector model that
# we are interested -- the first is the output probabilities and the
# second can be used to derive the bounding box coordinates of text
layerNames = [
    "feature_fusion/Conv_7/Sigmoid",
    "feature_fusion/concat_3"]

# load the pre-trained EAST text detector
print("[INFO] loading EAST text detector...")
net = cv2.dnn.readNet(args["east"])

# construct a blob from the image and then perform a forward pass of
# the model to obtain the two output layer sets
blob = cv2.dnn.blobFromImage(image, 1.0, (W, H),
                             (123.68, 116.78, 103.94), swapRB=True, crop=False)
start = time.time()
net.setInput(blob)
(scores, geometry) = net.forward(layerNames)
end = time.time()

# show timing information on text prediction
print("[INFO] text detection took {:.6f} seconds".format(end - start))

# grab the number of rows and columns from the scores volume, then
# initialize our set of bounding box rectangles and corresponding
# confidence scores
(numRows, numCols) = scores.shape[2:4]
rects = []
confidences = []

# loop over the number of rows
for y in range(0, numRows):
    # extract the scores (probabilities), followed by the geometrical
    # data used to derive potential bounding box coordinates that
    # surround text
    scoresData = scores[0, 0, y]
    xData0 = geometry[0, 0, y]
    xData1 = geometry[0, 1, y]
    xData2 = geometry[0, 2, y]
    xData3 = geometry[0, 3, y]
    anglesData = geometry[0, 4, y]

# loop over the number of columns
for x in range(0, numCols):
    # if our score does not have sufficient probability, ignore it
    if scoresData[x] < args["min_confidence"]:
        continue

    # compute the offset factor as our resulting feature maps will
    # be 4x smaller than the input image
    (offsetX, offsetY) = (x * 4.0, y * 4.0)

    # extract the rotation angle for the prediction and then
    # compute the sin and cosine
    angle = anglesData[x]
    cos = np.cos(angle)
    sin = np.sin(angle)

    # use the geometry volume to derive the width and height of
    # the bounding box
    h = xData0[x] + xData2[x]
    w = xData1[x] + xData3[x]

    # compute both the starting and ending (x, y)-coordinates for
    # the text prediction bounding box
    endX = int(offsetX + (cos * xData1[x]) + (sin * xData2[x]))
    endY = int(offsetY - (sin * xData1[x]) + (cos * xData2[x]))
    startX = int(endX - w)
    startY = int(endY - h)

    # add the bounding box coordinates and probability score to
    # our respective lists
    rects.append((startX, startY, endX, endY))
    confidences.append(scoresData[x])

# apply non-maxima suppression to suppress weak, overlapping bounding
# boxes
boxes = non_max_suppression(np.array(rects), probs=confidences)

# loop over the bounding boxes
for (startX, startY, endX, endY) in boxes:
    # scale the bounding box coordinates based on the respective
    # ratios
    startX = int(startX * rW)
    startY = int(startY * rH)
    endX = int(endX * rW)
    endY = int(endY * rH)

    # draw the bounding box on the image
    cv2.rectangle(orig, (startX, startY), (endX, endY), (0, 255, 0), 2)

# show the output image
cv2.imshow("Text Detection", orig)
cv2.waitKey(0)

# apply non-maxima suppression to suppress weak, overlapping bounding
# boxes
boxes = non_max_suppression(np.array(rects), probs=confidences)

# loop over the bounding boxes
for (startX, startY, endX, endY) in boxes:
    # scale the bounding box coordinates based on the respective
    # ratios
    startX = int(startX * rW)
    startY = int(startY * rH)
    endX = int(endX * rW)
    endY = int(endY * rH)

    # draw the bounding box on the image
    cv2.rectangle(orig, (startX, startY), (endX, endY), (0, 255, 0), 2)

# show the output image
cv2.imshow("Text Detection", orig)
cv2.waitKey(0)

错误

net = cv2.dnn.readNet(args["east"])cv2.error: OpenCV(3.4.3) C:\projects\opencv-python\opencv\modules\dnn\src\dnn.cpp:3443: error: (-2:Unspecified error) 无法确定文件的原始框架:在函数'cv::dnn::experimental_dnn_34_v7::readNet'

net = cv2.dnn.readNet(args["east"]) cv2.error: OpenCV(3.4.3) C:\projects\opencv-python\opencv\modules\dnn\src\dnn.cpp:3443: error: (-2:Unspecified error) Cannot determine an origin framework of files: in function 'cv::dnn::experimental_dnn_34_v7::readNet'

在加载 EAST 文本检测器时显示

is shown on loading the EAST text detector

我使用的是 opencv-python 3.4.3.18.这个错误的原因是什么?和Python版本有关系吗?

I am using opencv-python 3.4.3.18. What is the cause for this error? Does it have anything to do with the Python version?

推荐答案

问题是我没有传递参数.要使用 PyCharm 传递参数,请在运行"菜单上选择编辑配置"并传递参数--image :输入图像的路径.--east : EAST 场景文本检测器模型文件路径.--min-confidence :确定文本的概率阈值.--width : 调整后的图像宽度--height : 调整后的图像高度

The issue was that I hadn't passed the arguments. To pass the arguments using PyCharm, on the 'run'menu select "edit configurations" and pass the arguments --image : The path to the input image. --east : The EAST scene text detector model file path. --min-confidence : Probability threshold to determine text. --width : Resized image width --height : Resized image height

这篇关于在 Python 中加载 OpenCV EAST 文本检测器时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆