Search code examples
pythonopencvobject-detectionyolo

OpenCV DNN throwing exception in case of PNG images


I am pretty new to computer vision and OpenCv, Python, yolo. I tried to build an inference layer that detects the objects in images and videos. It works smoothly for JPEGs and Videos but as soon as I tried to run the system on PNG it throws

 (<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>)

Here is my sample code

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>>>>>>

Here is the snippet from cfg file

[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

So the crash stops if I change the channel value to 4 but then no detection happens

Potential solution:

Convert the png image to Jpeg, but I want to keep this as last resort

Please enlighten me on this.


Solution

  • It seems the module you are using only works with images with 3 channels (RGB) but PNG images have 4 channels (RGB + Alpha), you can convert your input image manually.

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