我已经构建了一个图像处理分类器,在这段代码中,我正在制作一个 api,它采用输入图像形式键“test_image”并预测图像的类别,但 cv2.imread()
是给我这个错误
TypeError at /image/ expected string or Unicode object, InMemoryUploadedFile found
我知道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())
来源:
https://docs.opencv.org/3.0-beta/modules/imgcodecs/doc/reading_and_writing_images.html
关于python - 在/image/expected string 或 Unicode 对象处出现 TypeError,InMemoryUploadedFile 已找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47291987/