【问题标题】:AttributeError: 'str' object has no attribute 'get' ocrAttributeError:“str”对象没有属性“get”ocr
【发布时间】:2019-11-24 13:45:03
【问题描述】:
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()

一切都与代码有关。

尝试使用 OCR 将图像更改为文本时出错。令人尴尬的是,它没有突然出现。我需要你的帮助...

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'

都是关于错误的。

【问题讨论】:

  • 在继续之前,您需要查看resultprint(result)
  • 看起来result是一个字符串,你不能在上面调用get()函数。
  • 这些 cmets 是否有助于解决您的问题?

标签: python ocr cv2


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2020-04-01
    • 2021-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-10
    相关资源
    最近更新 更多