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!