pythonnumpyopencvimutils

OpenCV frame in unusable shape


I have a CNN which needs the input images to be in the shape [channels, size, size] but when I get frames using imutils videostream function I get frames in the shape [size, size, channel].

Using numpy reshape to reshape this frame makes my CNN unable to recognize what is in the frame and it cannot classify anything correctly.

Is there any way to specify the shape I want my frames to be in? Or to reshape the frame in such a way that I preserve the image?

from imutils.video import VideoStream
import time
vs = VideoStream(src=1).start()
time.sleep(2.0)
frame = vs.read()
print(frame.shape)  # output: (480, 640, 3) -> I need (3, 480, 640)

Solution

  • I think that the function you are looking for is transpose. Assuming frame is a numpy array, you can use:

    frame_cnn = frame.transpose([2,0,1])