import numpy as np
import time
import tensorflow as tf
from keras.models import load_model
from keras.preprocessing import image
import cv2
from picamera2 import Picamera2
cv2.startWindowThread()
picam2 = Picamera2()
picam2.configure(picam2.create_preview_configuration(main={"format": 'XRGB8888', "size": (640,480)}))
picam2.start()
#Disable scientific notation for clarity
np.set_printoptions(suppress=True)
#Load the model
model = load_model("keras_model.h5", compile=False)
#Load the labels
class_names = open("labels.txt", "r").readlines()
while True:
im = picam2.capture_array()
im = cv2.resize(im, (224, 224), cv2.INTER_NEAREST)
grey = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
cv2.imshow("Camera", grey)
#---Make the image a numpy array and reshape it to the models input shape.---
im = np.asarray(im, dtype=np.float32).reshape(1, 224, 224, 3)
#---Normalize the image array------------------------------------------------
im = (im / 127.5) - 1
#---Predicts the model-------------------------------------------------------
prediction = model.predict(image)
index = np.argmax(prediction)
class_name = class_names[index]
confidence_score = prediction[0][index]
print("Class:", class_name[2:], end="")
print("Confidence Score:", str(np.round(confidence_score * 100))[:-2], "%")
im = np.asarray(im, dtype=np.float32).reshape(1,224,224, 3) ValueError: cannot reshape array of size 200704 into shape (1,224,224,3)
I tried to change the inputs on reshape function. I expected that the picamera2 will run smoothly, but the result is that it cannot accept the shape array size. The code works on a normal webcam except picamera2
You are using XRGB8888, which has 4 channels. That means the shape should be (1,224,224, 4), which is equivalent to the shape of the array you want to reshape, 1 * 244 * 244 * 4 = 200704
Try:
im = np.asarray(im, dtype=np.float32).reshape(1, 224, 224, 4)