简体   繁体   English

OpenCV python层不适用于caffe / digits框架

[英]OpenCV python layer is not working with caffe/digits framework

I made following python layer and added it to the LeNet architecture. 我制作了以下python层,并将其添加到LeNet架构中。 But when building model it gives an error. 但是在构建模型时会出现错误。 I am to apply my Python layer using Numpy but when I am using OpenCV it gives an error. 我要使用Numpy应用我的Python层,但是当我使用OpenCV时会出现错误。 Following I am adding my code and corresponding error from a log file. 接下来,我从日志文件添加我的代码和相应的错误。

import cv2
import caffe
import random

def doEqualizeHist(img): img = img.astype(np.uint8) img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) return cv2.equalizeHist(img)

class EqualizeLayer(caffe.Layer): def setup(self, bottom, top): assert len(bottom) == 1, 'requires a single layer.bottom' assert bottom[0].data.ndim >= 3, 'requires image data' assert len(top) == 1, 'requires a single layer.top'

def reshape(self, bottom, top): # Copy shape from bottom top[0].reshape(*bottom[0].data.shape) def forward(self, bottom, top): # Copy all of the data top[0].data[...] = bottom[0].data[...] for ii in xrange(0, top[0].data.shape[0]): imin = top[0].data[ii, :, :, :].transpose(1, 2, 0) top[0].data[ii, :, :, :] = doEqualizeHist(imin).transpose(2, 0, 1) def backward(self, top, propagate_down, bottom): pass

Error Message: 0812 06:41:53.452097 14355 net.cpp:723] Ignoring source layer train-data OpenCV Error: Assertion failed (scn == 3 || scn == 4) in cvtColor, file /build/opencv-SviWsf/opencv-2.4.9.1+dfsg/modules/imgproc/src/color.cpp, line 3737 Traceback (most recent call last): File "/var/lib/digits/jobs/20170812-064148-f44d/digits_python_layers.py", line 27, in forward top[0].data[ii, :, :, :] = doEqualizeHist(imin).transpose(2, 0, 1) File "/var/lib/digits/jobs/20170812-064148-f44d/digits_python_layers.py", line 8, in doEqualizeHist img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY) cv2.error: /build/opencv-SviWsf/opencv-2.4.9.1+dfsg/modules/imgproc/src/color.cpp:3737: error: (-215) scn == 3 || 错误消息:0812 06:41:53.452097 14355 net.cpp:723]忽略源层训练数据OpenCV错误:cvtColor文件/ build / opencv-SviWsf /中的断言失败(scn == 3 || scn == 4) opencv-2.4.9.1 + dfsg / modules / imgproc / src / color.cpp,第3737行回溯(最近一次调用最近):文件“ /var/lib/digits/jobs/20170812-064148-f44d/digits_python_layers.py”,第27行,位于前上方[0] .data [ii,:,:::] = doEqualizeHist(imin).transpose(2,0,1)文件“ / var / lib / digits / jobs / 20170812-064148-f44d /digits_python_layers.py“,第8行,位于doEqualizeHist img = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)cv2.error:/build/opencv-SviWsf/opencv-2.4.9.1+dfsg/modules/imgproc/src/color。 cpp:3737:错误:(-215)scn == 3 || scn == 4 in function cvtColor scn == 4在函数cvtColor中

For future reference, an "Assertion failed" error message in OpenCV means you passed invalid data to a function. 供将来参考,OpenCV中出现“断言失败”错误消息,表示您已将无效数据传递给函数。 In this case, the assertion that failed is scn == 3 || scn == 4 在这种情况下,失败的断言是scn == 3 || scn == 4 scn == 3 || scn == 4 . scn == 3 || scn == 4 To know exactly what that means, you can look at the source file where the assertion failed: modules/impgproc/src/color.cpp and examine the function where it happened: cvtColor at line 3737. Look to see what the variable scn represents. 要确切地知道这意味着什么,您可以查看断言失败的源文件: modules/impgproc/src/color.cpp并检查发生该问题的函数: cvtColor在第3737行。查看变量scn代表什么。

In your case, the problem is that you're converting img to a single-channel format and then attempting to convert it from RGB to grayscale. 就您而言,问题在于您正在将img转换为单通道格式,然后尝试将其从RGB转换为灰度。 That conversion is first asserting the input is a 3- or 4-channel format. 该转换首先声明输入是3通道或4通道格式。 It isn't so the assertion fails. 并不是这样,断言失败了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM