1

I am using the OCRSpace API and I have never received this error message before. The OCR always has worked on this image but recently gave me IndexError: list index out of range for: parse_results = result.get("Parsed Results")[0]. The full code is:

url_api = "https://api.ocr.space/parse/image"
            result = requests.post(url_api,
                          files = {os.path.join(path, file_name): file_bytes},
                          data = {"apikey": "KEY",
                                  "language": "eng",
                                  #"OCREngine": 2,
                                  "isTable": True})
            result = result.content.decode()
            result = json.loads(result)
            parsed_results = result.get("ParsedResults")[0]
            global OCRText
            OCRText = parsed_results.get("ParsedText")

Any help would be appriciated!

  • it looks like the "ParsedResults" field of result is a empty list. – AmaanK Dec 27 '20 at 06:02
  • It has worked in the past, could you explain what you mean by that? – user14816038 Dec 27 '20 at 06:03
  • 1
    What does `result.content.decode()` return? – Jarvis Dec 27 '20 at 06:05
  • basically when you have converted the results to json. the result becomes a dictionary. and the value of `result['ParsedResults'] = []` which is empty list and if you try accessing empty list using index 0, it raises IndexError. It is a server side problem or the results for that were none – AmaanK Dec 27 '20 at 06:06
  • try inserting `from pprint import pprint; pprint(result)` after the `result = json.loads(result)` line. and then share the output to us. (Complete output) – AmaanK Dec 27 '20 at 06:08
  • The response is: 'ParsedResults': [{'ErrorDetails': '', 'ErrorMessage': '', 'FileParseExitCode': 1, 'ParsedText': '16375 N. Kashington St.\t\r\n' 'Thornton, CO 80023\t\r\n' '2X Member 111826278335\t\r\n' '673919 FF \t23.99 E\t\r\n' '633561 KS DICED TOH\t\r\n' '967596 JACKORGSALSR\t2.97 E\t\r\n' '3 e 4.29\t\ – user14816038 Dec 27 '20 at 13:20
  • The code stops working and starts working randomly – user14816038 Dec 27 '20 at 13:22

0 Answers0