Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

python - 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!!

question from:https://stackoverflow.com/questions/65647515/error-typeerror-value-passed-to-parameter-input-has-datatype-uint8-not-in-li

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...