简体   繁体   中英

Error: TypeError: Value passed to parameter 'input' has DataType uint8 not in list of allowed values: float16, bfloat16, float32, float64, int32

So I am trying to predict an image with tensorflow. But I am running into this Error:

TypeError: Value passed to parameter 'input' has DataType uint8 not in list of allowed values: float16, bfloat16, float32, float64, int32

Here is my Code:

import cv2
import tensorflow as tf

CATEGORIES = ["sad","happy"]

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_alt2.xml')
cap = cv2.VideoCapture(0)


if not (cap.isOpened()):

    print("Could not open video device")


ie=0

a=True

while a==True:
    ret, frame = cap.read()
    cv2.imshow('AAAAAAAAAAA', frame)
    if cv2.waitKey(1) & 0xFF == ord('s'):
        cv2.imwrite(r'/home/jongre/Documents/Programming/prediction/predic.jpg',frame)
        img = cv2.imread(r'/home/jongre/Documents/Programming/prediction/predic.jpg')
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        faces = face_cascade.detectMultiScale(gray, 1.3, 5)
        for (x,y,w,h) in faces:
            cv2.rectangle(img, (x,y), (x+w, y+h), (255,0,0), 2)
            crop_img = img[y:y+h, x:x+w]
            roi_gray = gray[y:y+h, x:x+w]
            roi_color = img[y:y+h, x:x+w]
        cv2.imwrite(r'/home/jongre/Documents/Programming/prediction/predic.jpg', crop_img)
        a=False
def prepare(filepath):
    IMG_SIZE = 100 
    img_array = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
    new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))
    return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1)


model = tf.keras.models.load_model("3-conv-128-0-dense.model")

prediction = model.predict([prepare(r'/home/jongre/Documents/Programming/prediction/predic.jpg')])
print(prediction)  # will be a list in a list.
print(CATEGORIES[int(prediction[0][0])])

Does anyone have an idea how to solve this? Thanks in advance!!

I think this error is due to the if condition: ord() gives you a decimal number but you wrote 0xFF which is hexadecimal number.

Correct me if I'm wrong.
And please comment if you can fix it.

try:

import numpy as np

def prepare(filepath):
    IMG_SIZE = 100 
    img_array = cv2.imread(filepath, cv2.IMREAD_GRAYSCALE)
    new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE)) 
    reshaped = new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1)
    return np.float32(reshaped)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM