[英]EAST text detection -215:Assertion failed (OpenCV Python)
When trying to use EAST text detector on some images, with OpenCV in Python on Windows 10, I get the following error:当尝试在某些图像上使用EAST 文本检测器时,在 Windows 10 上的 Python 中使用 OpenCV,我收到以下错误:
cv2.error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\dnn\src\dnn.cpp:835: error: (-215:Assertion failed) ld.inputBlobs[0]->total() == total(shapes[index]) in function 'cv::dnn::dnn4_v20180917::BlobManager::allocateBlobsForLayer'
(Strangely, that path does not exist on my file system) (奇怪的是,我的文件系统上不存在该路径)
I started from the excellent tutorial by Adrian Rosebrock .我从Adrian Rosebrock的优秀教程开始。 Here is a code snippet (it fails on the last line):这是一个代码片段(它在最后一行失败):
# sFileName is the path to the image, previously set
oInputImage = cv.imread(sFileName)
aiShape = oInputImage.shape
(iH, iW) = aiShape[:2]
iRequiredUnit = 32
# check if the image height is enough
iHr = iH % iRequiredUnit
iBottom = 0
iHr = iH % iRequiredUnit
if 0 < iHr:
# calculate how much padding is necessary
iBottom = iRequiredUnit - iHr
# check if the image width is enough
iRight = 0
iWr = iW % iRequiredUnit
if 0 < iWr:
# calculate how much padding is necessary
iRight = iRequiredUnit - iWr
if iBottom > 0 or iRight > 0:
# add padding to make the image proportions correct
oImage = cv.copyMakeBorder(
src=oInputImage,
top=iTop,
bottom=iBottom,
left=iLeft,
right=iRight,
borderType=cv.BORDER_CONSTANT,
value=[0, 0, 0]
)
else:
# no need to add padding
oImage = oInputImage.copy()
(iH, iW) = oImage.shape[:2])
ib, ig, ir, _ = cv.mean(oImage)
oBlob = cv.dnn.blobFromImage(
oImage, 1.0, (iW, iH), (ib, ig, ir),
swapRB=True, crop=False
)
# load the EAST network
# EAST_path initialized appropriately previously
oNet = cv.dnn.readNet(EAST_path)
oNet.setInput(oBlob)
asLayerNames = [
"feature_fusion/Conv_7/Sigmoid",
"feature_fusion/concat_3"]
(afScores, aoGeometry) = oNet.forward(asLayerNames)
I made some modifications, eg I recalculate the mean instead of using the hardcoded value shown in the example.我做了一些修改,例如我重新计算平均值而不是使用示例中显示的硬编码值。 I also tried to call blobFromImage
without mean (or with the default from the example) and with swapRB=False
, but the error keeps occurring.我还尝试调用blobFromImage
没有意思(或使用示例中的默认值)和swapRB=False
,但错误不断发生。
The problem happens systematically with some files ( here's an example ), whereas it doesn't with others, on which EAST instead runs smoothly.该问题在某些文件中系统地发生(这里是一个示例),而在其他文件中则没有发生,而 EAST 则在这些文件上运行平稳。 I cannot identify the characteristics that make an image troublesome, however I am inclined to think that the error is independent of the need to resize the image, since most of the images that can be analyzed without issues must be resized anyway.我无法确定使图像变得麻烦的特征,但是我倾向于认为错误与调整图像大小的需要无关,因为大多数可以毫无问题地分析的图像无论如何都必须调整大小。
I haven't found any documentation specific to the issue and cannot reconstruct the problem easily from the source (which, I guess, is this ).我还没有找到任何特定于该问题的文档,也无法从源代码轻松重建该问题(我猜是this )。
How can I prevent the error?我怎样才能防止错误?
I faced a similar problem with another application.我在另一个应用程序中遇到了类似的问题。 This has something to do with the input size.这与输入大小有关。 I used the following workaround that did not fail so far:我使用了以下迄今为止没有失败的解决方法:
orig_height, orig_width = image.shape[:2]
while True:
height, width = image.shape[:2]
blob = cv2.dnn.blobFromImage(image, scalefactor=1.0, size=(width, height),mean=(104.00698793, 116.66876762, 122.67891434),swapRB=False, crop=False)
self.net.setInput(blob)
try:
prediction = self.net.forward()
break
except:
pass
if width*height < 100:
raise
image = cv2.resize(image, (int(width*0.9), int(height*0.9)))
prediction = cv2.resize(prediction[0, 0], (orig_width, orig_height))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.