python - AttributeError:“str”对象没有属性“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()
这都是关于代码的。
尝试使用 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'
这都是关于错误的。
解决方案
api 密钥已过期通过在网站 ocr.space 注册获得一个免费的新密钥,但每个 ip 每天的调用次数限制为 500 次
推荐阅读
- r - 使用闪亮的点击操作更新图表和数据
- python - 有没有办法用白色填充图像的内部?
- java - 如何从用户那里获取“Next Line”/“Enter”的 ascii 值
- php - 我无法解决“缺少控制器错误”
- sql - 尝试获取按相同促销 ID 分组的促销代码的结果。(SQL)
- python - 如何在 Pandas 中查找两列值之间的组合?
- javascript - 查找SVG的中心点坐标
- python-3.x - 使用 selenium 和 xpath 绕过/跳过包含没有文本的单元格的表行
- c++ - 如何在编写模板模板函数时使用类型别名
- java - 如何从 csv 文件为 JOOQ 创建模拟数据库?