简体   繁体   中英

AttributeError: 'str' object has no attribute 'get' ocr

import cv2
import numpy as np
import requests
import io
import json
import os

cur_path=os.getcwd()
img_name = input("input file name : ")
image_path = os.path.join(cur_path, img_name)
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)

sample = img

# Ocr
url_api = "https://api.ocr.space/parse/image"
_, compressedimage = cv2.imencode(".jpg", sample, [1, 90])
file_bytes = io.BytesIO(compressedimage)

result = requests.post(url_api,
              files = {"screenshot.jpg": file_bytes},
              data = {"apikey": "helloworld",
                      "language": "eng"})

result = result.content.decode()
result = json.loads(result)

parsed_results = result.get("ParsedResults")[0]
text_detected = parsed_results.get("ParsedText")
print(text_detected)


cv2.imshow("Img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

It's all about the code.

An error occurred while trying to change the image to text with OCR. It's embarrassing that it didn't work out of the blue. I need your help...

Traceback (most recent call last):
  File "D:/Programming/OCR_PY/venv/main.py", line 31, in <module>
    parsed_results = result.get("ParsedResults")[0]
AttributeError: 'str' object has no attribute 'get'

It's all about the error.

api 密钥已过期 通过在网站 ocr.space 中注册获得一个免费的新密钥,但每个 ip 每天的调用次数限制为 500

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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