【问题标题】:TypeError at /image/ expected string or Unicode object, InMemoryUploadedFile found/image/ 预期字符串或 Unicode 对象处的 TypeError,找到 InMemoryUploadedFile
【发布时间】:2018-04-27 18:25:42
【问题描述】:

我已经构建了一个图像处理分类器,在这段代码中,我正在制作一个 api,它采用输入图像表单键“test_image”并预测图像的类别,但 cv2.imread() 给了我这个错误

TypeError at /image/ 预期字符串或 Unicode 对象,找到 InMemoryUploadedFile

我知道cv2.imread 只获取图片的网址,但我不知道如何解决。

我的代码:

def classify_image(request):
    if request.method == 'POST' and request.FILES['test_image']:
        test_image = request.FILES['test_image']
        test_image = cv2.imread(test_image)
        test_image = cv2.resize(test_image, (128, 128))
        test_image = np.array(test_image)
        test_image = test_image.astype('float32')
        test_image /= 255
        print(test_image.shape)

        test_image = np.expand_dims(test_image, axis=0)
        pred = model.predict_classes(test_image)
        print(pred)

   return JsonResponse(pred, safe=False)

【问题讨论】:

    标签: python django opencv machine-learning


    【解决方案1】:

    我找到了答案 imread 只接受文件的路径。

    【讨论】:

      【解决方案2】:

      看起来imread 方法旨在从文件中读取图像。还有一种不同的方法,imdecode,用于从内存中读取图像。尝试将代码的第 4 行替换为:

      test_image = cv2.imdecode(test_image.read())
      

      来源:

      https://docs.opencv.org/3.0-beta/modules/imgcodecs/doc/reading_and_writing_images.html

      https://docs.djangoproject.com/en/1.11/ref/files/uploads/

      【讨论】:

      猜你喜欢
      • 2012-12-22
      • 2015-04-05
      • 1970-01-01
      • 2018-01-01
      • 2021-04-04
      • 2017-10-28
      • 2014-09-10
      • 1970-01-01
      • 2019-10-31
      相关资源
      最近更新 更多