Search code examples
pythondjangonumpyultralytics

np.fromfile given django request.FILES element: io.UnsupportedOperation: fileno


in django i want to receive a file from a form and then pass it to a function, then do some OCR with it. but i get the io.UnsupportedOperation: fileno error. here is part of views.py:

@login_required(login_url='/profile/login/')
def repairman_profile(request):
    user = request.user
    repairman = RepairmanUser.objects.get(user=user)

    if request.method == 'POST':
        plate_form = PlateForm(request.POST, request.FILES)

        if plate_form.is_valid():
            plate = recognize_plate(request.FILES["plate_image"])
            return HttpResponse(plate)
    else:
        plate_form = PlateForm()


    context = {
        "repairman": repairman,
        'plate_form': plate_form,
    }
    return render(request, 'repairman/profile.html', context=context)

here is the recognize_plate() function:

def recognize_plate(plate):
    results = {}
    # load models
    model = Model.load("hezarai/crnn-fa-64x256-license-plate-recognition")
    license_plate_detector = YOLO('repairman/plate_recognition/best.pt')

    # load image
    # cap = cv2.imread(plate)


    frame = cv2.imread(plate)

the line frame = cv2.imread(plate) is where the error happens.

here is django log:

Internal Server Error: /profile/
Traceback (most recent call last):
  File "D:\Tamirauto\WebApp\venv\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "D:\Tamirauto\WebApp\venv\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Tamirauto\WebApp\venv\Lib\site-packages\django\contrib\auth\decorators.py", line 23, in _wrapper_view
    return view_func(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Tamirauto\WebApp\tamirauto\repairman\views.py", line 94, in repairman_profile
    plate = recognize_plate(request.FILES["plate_image"])
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\Tamirauto\WebApp\tamirauto\repairman\plate_recognition\main.py", line 18, in recognize_plate
    frame = cv2.imread(plate)
            ^^^^^^^^^^^^^^^^^
  File "D:\Tamirauto\WebApp\venv\Lib\site-packages\ultralytics\utils\patches.py", line 26, in imread
    return cv2.imdecode(np.fromfile(filename, np.uint8), flags)
                        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
io.UnsupportedOperation: fileno

Solution

  • Some file-like objects (which have function .read()) may not have value .fileno and it seems this makes problem in imread().

    But error shows cv2.imdecode(...) and you may try use it directly with plate.read()

    import numpy as np
    
    #data = open(filename, 'rb').read()
    #data = request.FILES["plate_image"].read()
    data = plate.read()
    
    arr = np.frombuffer(data, np.uint8)
    img = cv2.imdecode(arr, cv2.IMREAD_UNCHANGED)
    
    #cv2.imshow('image', img)