model_config = json_utils.decode(model_config.decode('utf-8')) AttributeError: 'str' 对象没有属性 'decode'

Sam*_*han 2 python numpy raspberry-pi keras tensorflow

我正在从事一个 Python 项目,该项目检测叶子上的疾病并在叶子上喷洒肥料。

在对其他错误进行了数小时的故障排除后,我归结为以下最终错误,该错误总是发生并且我似乎无法修复。

以下是到目前为止我用于依赖项的版本:

  • keras = 2.4.3
  • cv2 = 4.5.1
  • numpy = 1.20.1
  • 张量流 = 2.4.0
  • h5py = 3.2.1
  • 熊猫 = 1.2.3

我面临的错误:

Traceback (most recent call last):
  File "leaf_cnn.py", line 12, in <module>
    model = load_model('Leaf_CNN.h5')
  File "/home/pi/.local/lib/python3.7/site-packages/tensorflow/python/keras/saving/save.py", line 207, in load_model
    compile)
  File "/home/pi/.local/lib/python3.7/site-packages/tensorflow/python/keras/saving/hdf5_format.py", line 182, in load_model_from_hdf5
    model_config = json_utils.decode(model_config.decode('utf-8'))
AttributeError: 'str' object has no attribute 'decode'
Run Code Online (Sandbox Code Playgroud)

代码文件leaf_cnn.py

# importing files/dependencies

from tensorflow.keras.models import load_model
import cv2
import numpy as np
#import Categories
import time
import RPi.GPIO as GPIO

#loading model/ML algorithm 

model = load_model('Leaf_CNN.h5')
cap = cv2.VideoCapture(0) # capture frame
ret, img = cap.read()

channel = 21 
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel,GPIO.OUT)
#cv2.imshow('aaa',img)  'display image with title'

img = cv2.resize(img,(224,224)) 
img = np.reshape(img,[1,224,224,3])
classes = model.predict(img)
y_pred = np.argmax(classes, axis=1)
y_pred = Categories.categories[int(y_pred)]

if "healthy" not in y_pred:
    GPIO.output(21, GPIO.HIGH) #turn-on relay
    time.sleep(1)
else:
    GPIO.output(21, GPIO.LOW) #turn-off relay
    time.sleep(1)
#cv2.waitKey(0)
#cv2.destroyAllWindows()
Run Code Online (Sandbox Code Playgroud)

Hag*_*ard 7

h5py似乎有问题,必须安装在某个版本中才能确保与 tensorflow 兼容。这就是我的诀窍:

pip uninstall h5py
pip install h5py==2.10.0
Run Code Online (Sandbox Code Playgroud)