io.UnsupportedOperation:文件号

问题描述 投票:0回答:1

在 django 中,我想从表单接收一个文件,然后将其传递给一个函数,然后用它做一些 OCR。但我收到

io.UnsupportedOperation: fileno
错误。 这是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)

这是 recognize_plate() 函数:

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)

frame = cv2.imread(plate)
是发生错误的地方。

这是 django 日志:

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
python django opencv
1个回答
0
投票

一些类似文件的对象(具有功能

.read()
)可能没有价值
.fileno
,这似乎会在
imread()
中产生问题。

但是错误显示

cv2.imdecode(...)
,你可以尝试直接使用它与
plate.read()

#data = open(filename, 'rb').read()
#data = request.FILES["plate_image"].read()
data = plate.read()

arr = np.frombuffer(data, np.uint8)
img2 = cv2.imdecode(arr, cv2.IMREAD_UNCHANGED)

#cv2.imshow('img2', img2)
© www.soinside.com 2019 - 2024. All rights reserved.