【问题标题】:How do i find a solution for a prediction error我如何找到预测错误的解决方案
【发布时间】:2023-03-18 12:30:01
【问题描述】:

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

希望有人能帮帮我

我的代码:

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)

【问题讨论】:

    标签: python tensorflow keras computer-vision tf.keras


    【解决方案1】:

    找到你的问题
    您的模型应该使用 3 通道预测 RGB 图像
    但是您输入了一个黑白图像,有 1 个通道
    因此,尝试输入 RGB 图像而不是黑白图像,然后您的模型将不会引发该错误

    【讨论】:

    猜你喜欢
    • 2020-09-03
    • 1970-01-01
    • 2021-07-23
    • 1970-01-01
    • 2022-06-13
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多