简体   繁体   English

如何以二进制形式读取Django request.FILES(图像)

[英]how to read Django request.FILES (which is an image) as binary

I want to get a binary image from request which contains an image file from ajax. 我想从请求中获取一个二进制图像,其中包含来自ajax的图像文件。

Senario - When a user uploads an image file, I want to do OCR(Optical Character Recognition) to the image file using google VISION API. Senario-用户上传图像文件时,我想使用Google VISION API对图像文件进行OCR(光学字符识别)。 For that, I need to read the file as binary. 为此,我需要将文件读取为二进制文件。

Background - Currently, In my program, when the user uploads the image, the file is passed to Django view by ajax. 背景-当前,在我的程序中,当用户上传图像时,文件由ajax传递到Django视图。 I tried to get a binary file in the views.py. 我试图在views.py中获取一个二进制文件。 When I use request.FILES.get('file').read() , there is no data. 当我使用request.FILES.get('file').read() ,没有数据。 But print(request.FILES.get('file').name) is 'bank.png'... I think it's weird. 但是print(request.FILES.get('file').name)是'bank.png'...我认为这很奇怪。

Here is some code 这是一些代码

views.py views.py

def receipt_ocr(request):
    if request.method == 'POST':
        print(type(request.FILES["file"]))              #<class 'django.core.files.uploadedfile.InMemoryUploadedFile'>
        print(type(request.FILES.get('file')))          #<class 'django.core.files.uploadedfile.InMemoryUploadedFile'>
        print(request.FILES.get('file'))                #bank.png
        imageFile = request.FILES.get('file')
        print(imageFile.size)                           #119227
        print(request.FILES.get('file').name)           #bank.png
        print(request.FILES.get('file').content_type)   #image/png
        print(type(request.FILES.get('file').open()))   #<class 'NoneType'>
        print(request.FILES.get('file').open())         #None
        print(type(request.FILES["file"].read()))       #<class 'bytes'>
        for chunk in imageFile.chunks():
            print(chunk)                               #this generates lot of binary and some rdf:Description rdf:about= ... things
        receipt_image = imageFile.read()
        print(type(receipt_image))
        print(receipt_image)

ajax part 阿贾克斯部分


                // receipt OCR process
                $.ajax({
                    url: "{% url 'place:receipt_ocr' %}",
                    enctype: 'multipart/form-data',
                    type: 'POST',
                    processData: false,
                    contentType: false,
                    data: postData,
                    complete: function(req){
                        alert('good');
                    },
                    error: function(req, err){
                        alert('message:' + err);
                    }
                });

What I exactly want to do is using this kind of code (google VISION API example code, OCR) 我真正想做的就是使用这种代码(Google VISION API示例代码,OCR)

def detect_text(path):
    """Detects text in the file."""
    client = vision.ImageAnnotatorClient()

    with io.open(path, 'rb') as image_file:
        content = image_file.read() #read binary, save to content

    image = vision.types.Image(content=content)

    response = client.text_detection(image=image)
    texts = response.text_annotations
    print('Texts:')
    print(type(texts))

    for text in texts:
        print('\n"{}"'.format(text.description))

        vertices = (['({},{})'.format(vertex.x, vertex.y)
                    for vertex in text.bounding_poly.vertices])

        print('bounds: {}'.format(','.join(vertices)))

So I want to get 'content' of above code in my views.py from the request. 因此,我想从请求中在views.py中获取上述代码的“内容”。 (I don't need to use this function itself. I'll use some part of the function) (我本身不需要使用此函数。我将使用该函数的某些部分)

How can I get the binary image in views.py? 如何在views.py中获取二进制图像? I thought the code below would works, but it doens't work. 我以为下面的代码可以用,但是不能用。 and I don't know why. 我不知道为什么。

f = request.FILES.get('file')
receipt_image = f.read()
image = vision.types.Image(content=receipt_image)

Thank you for your any help and comments. 感谢您的帮助和评论。

===ADD=== ===添加===

'it doesn't work' means that I got this Traceback from the log. “它不起作用”意味着我从日志中获得了此追溯。

Internal Server Error: /mdd_event/receipt_ocr/ Traceback (most recent call last): File "C:\Users\user\liam\dev\git\mdd_bean_env\lib\site-packages\google\api_core\grpc_helpers.py", line 57, in error_remapped_callable return callable_(*args, **kwargs) 
File "C:\Users\user\liam\dev\git\mdd_bean_env\lib\site-packages\grpc\_channel.py", line 565, in __call__ return _end_unary_response_blocking(state, call, False, None) 
File "C:\Users\user\liam\dev\git\mdd_bean_env\lib\site-packages\grpc\_channel.py", line 467, in _end_unary_response_blocking raise _Rendezvous(state, None, None, deadline) grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with: status = StatusCode.INVALID_ARGUMENT details = "Request must specify image and features." debug_error_string = "{"created":"@1566165461.211000000","description":"Error received from peer ipv4:216.58.197.234:443","file":"src/core/lib/surface/call.cc","file_line":1052,"grpc_message":"Request must specify image and features.","grpc_status":3}" > 
The above exception was the direct cause of the following exception: Traceback (most recent call last): 
File "C:\Users\user\liam\dev\git\mdd_bean_env\lib\site-packages\django\core\handlers\exception.py", line 41, in inner response = get_response(request) 
File "C:\Users\user\liam\dev\git\mdd_bean_env\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response response = self.process_exception_by_middleware(e, request) 
File "C:\Users\user\liam\dev\git\mdd_bean_env\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) 
File "C:\Users\user\liam\dev\git\mdd_bean_env\lib\site-packages\django\contrib\auth\decorators.py", line 23, in _wrapped_view return view_func(request, *args, **kwargs) 
File "C:\Users\user\liam\dev\git\mdd_bean_env\lib\site-packages\django\contrib\auth\decorators.py", line 23, in _wrapped_view return view_func(request, *args, **kwargs) 
File "C:\Users\user\liam\dev\git\mdd_bean\mdd_event\views.py", line 530, in receipt_ocr response = client.text_detection(image=image) 
File "C:\Users\user\liam\dev\git\mdd_bean_env\lib\site-packages\google\cloud\vision_helpers\decorators.py", line 101, in inner response = self.annotate_image(request, retry=retry, timeout=timeout) 
File "C:\Users\user\liam\dev\git\mdd_bean_env\lib\site-packages\google\cloud\vision_helpers\__init__.py", line 72, in annotate_image r = self.batch_annotate_images([request], retry=retry, timeout=timeout) 
File "C:\Users\user\liam\dev\git\mdd_bean_env\lib\site-packages\google\cloud\vision_v1\gapic\image_annotator_client.py", line 274, in batch_annotate_images request, retry=retry, timeout=timeout, metadata=metadata File "C:\Users\user\liam\dev\git\mdd_bean_env\lib\site-packages\google\api_core\gapic_v1\method.py", line 143, in __call__ return wrapped_func(*args, **kwargs) 
File "C:\Users\user\liam\dev\git\mdd_bean_env\lib\site-packages\google\api_core\grpc_helpers.py", line 59, in error_remapped_callable six.raise_from(exceptions.from_grpc_error(exc), exc) 
File "<string>", line 3, in raise_from google.api_core.exceptions.InvalidArgument: 400 Request must specify image and features.

I posted another question . 我提出了另一个问题 Because this question include not one issue but some issues like 'there's no actual data but name and size', 'get binary image', 'google vision api using', ... So I decided post another one for only 'no data'issue. 由于此问题不只涉及一个问题,还涉及诸如“没有实际数据,但名称和大小”,“获取二进制图像”,“使用Google视觉api”之类的问题,因此我决定仅针对“无数据”发布另一个问题问题。 and there is the answer 答案

Summary : I already read the data in request.FILES["file"].read() . 摘要:我已经读取了request.FILES["file"].read() So there's no data in buffer of subsequent calls. 因此,后续调用的缓冲区中没有数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM