2

I'm trying to upload a jpeg in django and then pass the jpeg into a function that will read send a post request to an OCR API. It seems that I can't pass the jpeg file itself and can't figure out how to just pass bytes to the function.

views- trying to call the read_menu function in the upload_file function.

def ocr_space_file(filename, overlay=False, api_key='helloworld', language='eng'):
    payload = {'isOverlayRequired': overlay,
               'apikey': api_key,
               'language': language,
               }
    with open(filename, 'rb') as f:
        r = requests.post('https://api.ocr.space/parse/image',
                          files={filename: f},
                          data=payload,
                          )
    return r.content.decode()

def read_menu(image, api_key):
    output = ocr_space_file(image, overlay=False, api_key=api_key, language='eng')
    output = output.split('\\n')
    words = [i.replace('\\r',"") for i in output]
    return words

def upload_file(request):
    if request.method == 'POST':
        uploaded_menu = request.FILES['menu_image'].file.read()
        with open(uploaded_menu, 'rb') as f:
            words = read_menu(f, api_key)
            #print(type(f))
        print(words)

    return render(request, 'main/home.html')

This provides a "ValueError at embedded null byte"

Also, read_menu("menu_image.jpeg") works locally if that gives any insight.

Appreciate any help or guidance! Thanks!

DKW
  • 21
  • 4

1 Answers1

0

In your code, you are not calling a read_menu function. All the things depend on upload_file function than it will call a read_menu function which is commented. Please uncomment it and try again.

vikasvmads
  • 542
  • 1
  • 7
  • 12
  • Thanks for the quick response- but that was just code I was testing to see what kind of data types were being passed. I updated the post though :) – DKW Apr 06 '19 at 12:57