Search code examples
pythonnumpyopencvimage-processingimage-resizing

How to change the color channel of the OpenCV input frame to fit the model?


I am still new to deep learning. So, I am trying to run OpenCV to capture frames and pass those frames to my trained model. The input required for the model has dimensions of (48,48,1).

The first layer in the model:

model.add(Conv2D(input_shape=(48,48,1),filters=64,kernel_size=(3,3),padding="same", activation="relu"))

I am trying to convert the OpenCV frame input to fit the dimensions of the model. However, I tried to use cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) and resize but the output dimension is (48,48) only

I have tried another method shown below but the output was (48,48,3) and then after adding the axis to be able to pass it to the model the output dimension was (1,48,48,3)

coverted_image= cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces_detected = face_haar_cascade.detectMultiScale(coverted_image)

#Draw Triangles around the faces detected
for (x,y,w,h) in faces_detected:
    cv2.rectangle(frame,(x,y), (x+w,y+h), (255,0,0))
    roi_gray=frame[y:y+w,x:x+h]
    roi_gray=cv2.resize(roi_gray,(48,48))
    
    image_pixels = tf.keras.preprocessing.image.img_to_array(roi_gray)
    print(image_pixels.shape)
    image_pixels = np.expand_dims(image_pixels, axis = 0)
    print(image_pixels.shape)
    image_pixels /= 255
    print(image_pixels.shape)

How can I adjust the shape of the input to (48,48,1) to be able to get the prediction from the model?


Solution

  • openCV images are just numpy arrays, so they can be manipulated easily using numpy commands.

    E.g.:

    import numpy as np
    x = np.array(range(48*48)).reshape(48,48)
    
    x.shape
    

    (48, 48)

    x = x.reshape(48,48,1)
    
    x.shape
    

    (48, 48, 1)