python - 如何修复 TypeError:预期的 str、字节或 os.PathLike 对象,而不是 python 中的 PngImageFile
问题描述
请找到下面的代码
def get_document_bounds(image, feature): # [START vision_document_text_tutorial_detect_bounds] """返回给定图像的文档边界。""" os.environ['GOOGLE_APPLICATION_CREDENTIALS']=r'C:\DSpython\bounds.json' client = vision. ImageAnnotatorClient()
bounds = []
with io.open(image, 'rb') as image_file:
content = image_file.read()
image= types.Image(content=content)
response = client.document_text_detection(image=image)
document = response.full_text_annotation
# Collect specified feature bounds by enumerating all document features
for page in document.pages:
for block in page.blocks:
for paragraph in block.paragraphs:
for word in paragraph.words:
for symbol in word.symbols:
if (feature == FeatureType.SYMBOL):
bounds.append(symbol.bounding_box)
if (feature == FeatureType.WORD):
bounds.append(word.bounding_box)
if (feature == FeatureType.PARA):
bounds.append(paragraph.bounding_box)
if (feature == FeatureType.BLOCK):
bounds.append(block.bounding_box)
# The list `bounds` contains the coordinates of the bounding boxes.
# [END vision_document_text_tutorial_detect_bounds]
return bounds
***我的目标是需要从图像中提取文本。作为其中的一部分,我在这个函数“get document _bounds”中创建边界。
“使用 io.open(image, 'rb') as image_file:” 当使用 io.open 函数打开图像时,我得到了上面的代码。
解决方案
你的意思是放
image= types.Image(content=content)
前
with io.open(image, 'rb') as image_file:
content = image_file.read()
?
推荐阅读
- php - 从 Vue 应用程序到在 WAMP 上运行的 PHP API 的 axios 的 CORS 问题
- javascript - 在可观察笔记本中创建对象文字失败
- haskell - 惰性状态转换器在 2D 递归中急切地消耗惰性列表
- java - 为什么 String.format 接受 int 代替 %s?
- python - 编写一个名为 randomization 的函数,将一个正整数 n 作为输入,并返回 A,一个随机的 nx 1 Numpy 数组
- pca - 比例数据的主成分分析
- javascript - 如何在 Puppeteer 中刷新页面时等待?
- javascript - 无法理解控制台错误:'Uncaught TypeError: t is not a function'
- python - 如何计算不同查询集中的匹配项
- laravel - 路由和漂亮的 URL