如何解决OpenCV DNN 在 PNG 图像的情况下抛出异常
我对计算机视觉和 OpenCv、Python、yolo 还很陌生。我尝试构建一个推理层来检测图像和视频中的对象。它适用于 JPEG 和视频,但只要我尝试在 PNG 上运行系统,它就会抛出
(<class 'cv2.error'>,error("OpenCV(4.4.0) /private/var/folders/nz/vv4_9tw56nv9k3tkvyszvwg80000gn/T/pip-req-build-czkpurnv/opencv/modules/dnn/src/layers/convolution_layer.cpp:347: error: (-2:Unspecified error) Number of input channels should be multiple of 3 but got 4 in function 'getMemoryShapes'\n"),<traceback object at 0x7f8a0c879900>)
这是我的示例代码
self.net = cv2.dnn_DetectionModel(path_to_config_file,path_to_yolo_weights)
self.layer_names = self.net.getLayerNames()
self.output_layers = [self.layer_names[i[0] - 1] for i in self.net.getUnconnectedOutLayers()]
blob = cv2.dnn.blobFromImage(input_image_frame,0.00392,(416,416),(0,0),False,crop=False)
# Detection
self.net.setInput(blob)
outs = self.net.forward(self.output_layers) //<<<<<<<Crash happens on this line>>>>>>
这是来自 cfg 文件的片段
[net]
# Testing
#batch=1
#subdivisions=1
# Training
batch=64
subdivisions=64
width=960
height=960
channels=3
momentum=0.949
decay=0.0005
angle=0
saturation = 1.5
exposure = 1.5
hue=.1
learning_rate=0.001
burn_in=1000
max_batches = 6000
policy=steps
steps=4800,5400
scales=.1,.1
#cutmix=1
mosaic=1
因此,如果我将通道值更改为 4,则崩溃会停止,但没有发生检测
可能的解决方案:
将 png 图像转换为 Jpeg,但我想将此作为最后的手段
请赐教。
解决方法
您使用的模块似乎仅适用于具有 3 个通道 (RGB) 的图像,但 PNG 图像具有 4 个通道 (RGB + Alpha),您可以手动转换输入图像。
input_image_frame = cv2.cvtColor(input_image_frame,cv2.COLOR_RGBA2RGB)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。