TypeError:应为str、bytes或os.PathLike对象,而不是InMemoryUploadedFile

2024-09-30 18:33:32 发布

您现在位置:Python中文网/ 问答频道 /正文

当我试图上传预测模型的图像时,出现以下错误

Trackback

文件“/Users/ragnar/plant disease recognition/lib/python3.8/site packages/django/core/handlers/exception.py” 34响应=获取响应(请求)

文件“/Users/ragnar/plant disease recognition/lib/python3.8/site packages/django/core/handlers/base.py”在_get_response中 115response=self.process\u异常\u由\u中间件(e,请求)

文件“/Users/ragnar/plant disease recognition/lib/python3.8/site packages/django/core/handlers/base.py”在_get_response中 113响应=包装的回调(请求,*回调参数,**回调参数)

索引中的文件“/Users/ragnar/Downloads/Plant Diseases Recognition master/Plant_Diseases/Plant_app/views.py” 12img=image.load\u img(myfile,target\u size=(224224))

load\u img中的文件“/Users/ragnar/plant disease recognition/lib/python3.8/site packages/tensorflow/python/keras/preprocessing/image.py” 299返回图像。加载图像(路径,灰度=灰度,颜色模式=颜色模式

load\u img中的文件“/Users/ragnar/plant disease recognition/lib/python3.8/site packages/keras\u preprocessing/image/utils.py” 113.打开(路径“rb”)作为f:

异常类型:位于的TypeError/ 异常值:应为str、bytes或os.PathLike对象,而不是InMemoryUploadedFile

views.py

class Predict(generics.CreateAPIView):
    serializer_class = ImageSerializer
    def post(self, request):
        """
            post:
            API to send leaf image and get its health status or disease.
        """
        data = ImageSerializer(data=request.data)
        if data.is_valid():
            photo = request.data['photo']
            img = image.load_img(photo, target_size=(224, 224))
            img = image.img_to_array(img)
            img = np.expand_dims(img, axis=0)
            img = img/255

            with graph.as_default():
                prediction = model.predict(img)

            prediction_flatten = prediction.flatten()
            max_val_index = np.argmax(prediction_flatten)
            result = output_list[max_val_index]

            return Response({'result': result})

Tags: 文件pyimageimgdatalibpackagessite