Q:

How to change the size of image read by my camera in opencv?  A machine learning model is showing error

I used  a machine learning model to train a classification dataset and used keras imagedatagenerator for augmentation.  In the program(which was actually forked) the target size was set to(300,300) , but during testing , when I use my pc camera, it shows the following error:
ValueError: Error when checking input: expected conv2d_1_input to have shape (300, 300, 1) but got array with shape (260, 300, 1) 
I dont want to train the dataset again to target size (260,300), as it caused other problems, so is there a way for opencv to solve this ,something like changing the size? 
The opencv program is for videocapture. 

A:

You can use OpenCV's resize function: 
resized_image = cv2.resize(image, (300, 300))

This will distort the image and may result in poor performance of your model. 
Your second option is to create an empty numpy array (filled with zeroes/ones)  of size 300x300 and fit the image in this array.  This way the image distortion is prevented. 
Second Option:
output_img = np.ones((300, 300, 1)) * 128
output_img = (output_img).astype('uint8')

scale = 300 / (image.shape[0] * 1.0)
image_resize = cv2.resize(image, (0, 0), fx=scale, fy=scale, interpolation=cv2.INTER_LANCZOS4)

img_w = image_resize.shape[1]

if img_w < 300:
    #pad the image with values and make it 1:1 aspect ratio
    offset = img_w % 2
    output_img[:, int(300 / 2 - math.floor(img_w / 2)):int(300 / 2 + math.floor(img_w / 2) + offset), :] = image_resize

else:
    #crop the center of the image to maintain 1:1 aspect ratio
    output_img = image_resize[:,int(img_w / 2 - 300 / 2):
                  int(img_w / 2 + 300 / 2), :]

