Home > OS >  How do I use Mindee API with Python3?
How do I use Mindee API with Python3?

Time:06-07

I'm just playing about with code and interested in parsing receipts to text (end point csv file). I came across this tutorial on mindee API where they also provide code to run the parsing. However, I keep getting the below errors when attempting to parse.

import requests

url = "https://api.mindee.net/v1/products/mindee/expense_receipts/v3/predict"

with open("/Users/test/PycharmProjects/PythonCrashCourse", "rb") as myfile: # Here they mention to specify the PATH to file/files which is here as per my windows10 path.
    files = {"IMG_5800.jpg": myfile}
    headers = {"Authorization": "Token asdasd21321"}
    response = requests.post(url, files=files, headers=headers)
    print(response.text)

PermissionError: [Errno 13] Permission denied: '/Users/test/PycharmProjects/PythonCrashCourse'

Why is there permission denied? When I am admin and have full permissions enabled on the file iteself. I have also tried modifying the code and running the below;

import requests

url = "https://api.mindee.net/v1/products/mindee/expense_receipts/v3/predict"
imageFile = "IMG_5800.jpg" #File is in the current directory
files = {"file": open(imageFile, "rb")}

headers = {"Authorization": "Token a4342343c925a"}
response = requests.post(url, files=files, headers=headers)
print(response.text)

#output

  {"api_request":{"error":{"code":"BadRequest","details":{"document":["Missing data for required field."],"file":["Unknown field."]},"message":"Invalid fields in form"},"resources":[],"status":"failure","**status_code":400**,"url":"http://api.mindee.net/v1/products/mindee/expense_receipts/v3/predict"}}
    
    
    Process finished with exit code 0

Status code 400 - suggests something has gone wrong with the syntax....Unfortunately I am stuck and simply just want the API to parse my receipt. Any ideas on what is going wrong please?

Desired output:

  • get results from receipt in text format/json from Mindee API

References Used:

CodePudding user response:

After brushing up on HTML format - https://www.codegrepper.com/code-examples/html/HTML file path. I realised the path I used was wrong and should've used the correct HTML format whether I am on Windows/Mac. To resolve my issue, I mentioned to go 1 directory up to where the image file is, when running my code.

with open("./IMG_5800.jpg", "rb") as myfile: #modified here to go 1 directory up to where the image file is hosted
    files = {"file": myfile}
    headers = {"X-Inferuser-Token": "a4279942d6e2fa8022c27e892b3c925a"}
    response = requests.post(url, files=files, headers=headers)
 

CodePudding user response:

From the error message, it was stated that the document was missing.

I'm glad you found the solution to this.

However, following the documentation, there is an improved code, the authentication header X-Inferuser-Token has been deprecated.

You can try doing this instead

import requests

url = "https://api.mindee.net/v1/products/mindee/expense_receipts/v3/predict"

with open("./IMG_5800.jpg", "rb") as myfile:
    files = {"document": myfile}
    headers = {"Authorization": "a4279942d6e2fa8022c27e892b3c925a"}
    response = requests.post(url, files=files, headers=headers)
    print(response.text)
  • Related