问题描述
我正在研究一个使用Python的项目,该项目可以检测叶子上的疾病并在叶子上喷施肥料.
I am working on a project on Python that detects disease on leaves and sprays fertilizer on the leaf.
在对其他错误进行了数小时的故障排除之后,我得出了以下经常发生的最终错误,但似乎无法修复.
After many hours of troubleshooting other errors, I came down to the following final error that always happens and I can't seem to fix.
以下是到目前为止我用于依赖项的版本:
Following are the versions I have used for the dependencies so far:
- keras = 2.4.3
- cv2 = 4.5.1
- numpy = 1.20.1
- tensorflow = 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'
代码文件leaf_cnn.py
Code file 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()
推荐答案
似乎有一个h5py的问题,必须将其安装在特定版本中才能确保与tensorflow兼容.这就是我的诀窍:
There seems to be an issue with h5py and it has to be installed in a certain version to ensure compatibility with tensorflow. This is what did the trick for me:
pip uninstall h5py
pip install h5py==2.10.0
这篇关于model_config = json_utils.decode(model_config.decode('utf-8'))AttributeError:'str'对象没有属性'decode'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!