【发布时间】:2021-12-19 21:43:39
【问题描述】:
当我根据张量流h5 模型上传图片以检查图片时,我正在使用tensorflow.keras.models 的load_model 加载图像,但它不接受。对于 JPG,它显示为 TypeError: expected str, bytes or os.PathLike object, not JpegImageFile,对于 PNG,它显示为 TypeError: expected str, bytes or os.PathLike object, not PngImageFile。现在该怎么办?
我用原始 python 尝试了代码,但效果很好。
代码:
#views.py
import numpy as np
from tensorflow.keras.models import load_model
from tensorflow.keras.preprocessing import image
pic = request.FILES['image']
img = Image.open(pic)
detection=load_model(os.path.join(settings.BASE_DIR,'static/auto_chloro_model.h5'))
test_img=image.load_img(img,target_size=(48,48))
test_img=image.img_to_array(test_img)
test_img=np.expand_dims(test_img,axis=0)
result=detection.predict(test_img)
a=result.argmax()
print(a)
#models.py
class images(models.Model):
img_main = models.ImageField(upload_to="images_api", default="")
def __str__(self):
return self.product_name
#forms.py
class imageForm(forms.ModelForm):
image = forms.ImageField()
class Meta:
model = images
fields = ['image']
追溯:
Internal Server Error: /upload Traceback (most recent call last): File "C:\Users\joyan\.conda\envs\tensorflow-django\lib\site-packages\django\core\handlers\exception.py", line 47, in inner response = get_response(request) File "C:\Users\joyan\.conda\envs\tensorflow-django\lib\site-packages\django\core\handlers\base.py", line 181, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "H:\Projects + Programming Projects\Auto Chloro\plant\detection\views.py", line 50, in uploadImage test_img=image.load_img(img,target_size=(48,48)) File "C:\Users\joyan\.conda\envs\tensorflow-django\lib\site-packages\keras_preprocessing\image\utils.py", line 113, in load_img with open(path, 'rb') as f: TypeError: expected str, bytes or os.PathLike object, not JpegImageFile
【问题讨论】:
-
你能分享完整的错误跟踪吗?
-
在主要问题中添加。 @ShreeyanshJain
标签: python django tensorflow django-views