我已经构建了一个图像处理分类器,并且在这段代码中,我正在创建一个API,它接受键为’test_image’的输入图像,并预测图像的类别,但cv2.imread()
出现了这个错误
TypeError 在 /image/ 处发生:期望字符串或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 只接受文件路径。