python - 如何在不使用 S3 从图像中检测文本的情况下在本地测试来自 AWS 的 Rekognition
问题描述
我正在尝试从图像中扫描文本,但如果不使用 S3 存储桶,我就找不到源代码。这是我找到的唯一源代码,但它使用 S3。我在这个项目中使用 python。
https://docs.aws.amazon.com/rekognition/latest/dg/text-detecting-text-procedure.html
import boto3
if __name__ == "__main__":
bucket='bucket'
photo='text.png'
client=boto3.client('rekognition')
response=client.detect_text(Image={'S3Object':{'Bucket':bucket,'Name':photo}})
textDetections=response['TextDetections']
print ('Detected text')
for text in textDetections:
print ('Detected text:' + text['DetectedText'])
print ('Confidence: ' + "{:.2f}".format(text['Confidence']) + "%")
print ('Id: {}'.format(text['Id']))
if 'ParentId' in text:
print ('Parent Id: {}'.format(text['ParentId']))
print ('Type:' + text['Type'])
print
在这里找到一个我可以在没有 S3 存储桶的情况下使用 Amazon Rekognition 吗?并运行它与我需要的不同,因为它只检测标签。
解决方案
DetectText
Rekognition API 中的方法(对于 boto, detect_text
)可以采用以下参数之一:
- 对 Amazon S3 存储桶中的图像的引用
- base64 编码的图像字节
因此,如果您不使用 S3 存储桶,则必须提供它的bytes。文档中没有提到第三种方法。输入结构如下图所示:
{
"Image": {
"Bytes": blob,
"S3Object": {
"Bucket": "string",
"Name": "string",
"Version": "string"
}
}
}
并且,获取非S3图像的字节流;您可以从此答案复制实现:
client = boto3.client('rekognition')
image_path='images/4.jpeg'
image = Image.open(image_path)
stream = io.BytesIO()
image.save(stream,format="JPEG")
image_binary = stream.getvalue()
response = client.detect_text(Image={'Bytes':image_binary})
推荐阅读
- c# - 无法在 C# 中使用集合初始化程序错误初始化十进制类型
- javascript - 如何查找数组是否有其他元素?
- sendgrid - 如何在 sendgrid node.js v3 客户端中将 pdf 文件作为附件发送
- javascript - Number.parseInt() 不断返回 NaN
- excel - Excel VBA:如何根据具有附加条件的数据范围将行从一个工作表复制到另一个工作表以缩小结果范围?
- javascript - 查询字符串更改时重新渲染 React 组件
- c - 从 MMA7455L 传感器 i2c 获取数据
- node.js - 如何正确使用带有 npm 脚本的 parallelshell?
- java - 如何验证具有不同架构的 Avro 格式数据
- android - 带有错误代码 3 的乱舞横幅