2017-11-14 10 views
3

私は画像処理クラシファイアをビルドしました。このコードでは、入力画像フォームキー 'test_image'を取って画像のクラスを予測するAPIを作成しています。 cv2.imread()は、私はcv2.imreadは画像のURLのみを取りますが、私はこれを解決する方法を知らないことを知っている私は、このエラーTypeIrror at/image /期待される文字列またはUnicodeオブジェクト、InMemoryUploadedFileが見つかりました

TypeError at /image/ expected string or Unicode object, InMemoryUploadedFile found

を与えています。

マイコード:

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) 

答えて

2

私は 関数imreadは、ファイルのパスのみを取るの答えを見つけました。

1

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/

+1

このエラーが発生します(TypeError:必須引数 'flags'(pos 2)が見つかりません) –

+0

https://stackoverflow.com/questions/47295025/valueerror-at-image-tensor-tensoractivation-5-softmax0- shape-4-dtyp/47300005?noredirect = 1#comment81555441_47300005すべてのsuggesions –

関連する問題