首页 > 解决方案 > 在 PNG 图像的情况下 OpenCV DNN 抛出异常

问题描述

我对计算机视觉和 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, 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,但我想将此作为最后的手段

请赐教。

标签: pythonopencvobject-detectionyolo

解决方案


您使用的模块似乎仅适用于具有 3 个通道 (RGB) 的图像,但 PNG 图像具有 4 个通道 (RGB + Alpha),您可以手动转换输入图像。

input_image_frame = cv2.cvtColor(input_image_frame, cv2.COLOR_RGBA2RGB)

推荐阅读