我如何找到预测错误的解决方案

How do i find a solution for a prediction error

我训练了一个用于石头剪刀布手势识别的模型。 当我尝试使用模型进行预测时,它给出了一个值错误。 我真的很困惑试图找到解决方案,但我没有。

希望有人能帮帮我

我的代码:

from time import sleep
from keras.preprocessing.image import img_to_array
from keras.preprocessing import image
from cv2 import cv2
import numpy as np

face_classifier = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
classifier =load_model('RockPaperScissor_model.h5')

emotion_labels = ['Paper','Rock','Scissor']

cap = cv2.VideoCapture(0)



while True:
    _, frame = cap.read()
    labels = []
    gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    faces = face_classifier.detectMultiScale(gray)

    for (x,y,w,h) in faces:
        cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,255),2)
        roi_gray = gray[y:y+h,x:x+w]
        roi_gray = cv2.resize(roi_gray,(48,48),interpolation=cv2.INTER_AREA)



        if np.sum([roi_gray])!=0:
            roi = roi_gray.astype('float')/255.0
            roi = img_to_array(roi)
            roi = np.expand_dims(roi,axis=0)

            prediction = classifier.predict(roi)[0]
            label=emotion_labels[prediction.argmax()]
            label_position = (x,y)
            cv2.putText(frame,label,label_position,cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),2)
       
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

升值错误:

ValueError: Input 0 of layer sequential_1 is incompatible with the layer: expected axis -1 of input shape to have value 3 but received input with shape (None, 48, 48, 1)

找到您的问题
您的模型应该预测具有 3 个通道的 RGB 图像
但是你输入的是一张黑白图片,有1个通道
因此,尝试输入 RGB 图像而不是黑白图像,这样您的模型就不会引发该错误