【发布时间】:2021-01-09 20:55:59
【问题描述】:
所以我正在尝试使用 tensorflow 预测图像。但是我遇到了这个错误:
TypeError:传递给参数“输入”的值的 DataType uint8 不在允许值列表中:float16、bfloat16、float32、float64、int32
这是我的代码:
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])])
有人知道如何解决这个问题吗?提前致谢!!
【问题讨论】:
-
你需要将你的数据转换成另一种类型:例如x= np.int32(x),你需要弄清楚哪些特征是错误的类型,然后再进行转换
标签: python tensorflow tensorflow2.0