Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在/image/处出现TypeError,应为字符串或Unicode对象,找到InMemoryUploadedFile_Python_Django_Opencv_Machine Learning - Fatal编程技术网

Python 在/image/处出现TypeError,应为字符串或Unicode对象,找到InMemoryUploadedFile

Python 在/image/处出现TypeError,应为字符串或Unicode对象,找到InMemoryUploadedFile,python,django,opencv,machine-learning,Python,Django,Opencv,Machine Learning,我已经构建了一个图像处理分类器,在这段代码中,我正在制作一个api,它接受输入图像表单键“test_image”,并预测图像的类别,但是cv2.imread()给了我这个错误 在/image/处出现TypeError,应为字符串或Unicode对象,找到InMemoryUploadedFile 我知道cv2.imread只获取图像的url,但我不知道如何解决这个问题 我的代码: def classify_image(request): if request.method == 'POST

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

在/image/处出现TypeError,应为字符串或Unicode对象,找到InMemoryUploadedFile

我知道
cv2.imread
只获取图像的url,但我不知道如何解决这个问题

我的代码:

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)

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

test_image = cv2.imdecode(test_image.read())
资料来源:

我找到了答案 imread只获取文件的路径。

它给了我这个错误(TypeError:Required参数'flags'(位置2)not found)任何建议