【问题标题】:Extract Text from an image using Google Cloud Vision API using cv2 in python在 python 中使用 cv2 使用 Google Cloud Vision API 从图像中提取文本
【发布时间】:2019-10-06 13:18:45
【问题描述】:

我们正在尝试使用 google-cloud-vision API 从图像中提取文本:

import io
import os
from google.oauth2 import service_account
from google.cloud import vision

# The name of the image file to annotate (Change the line below 'image_path.jpg' ******)
path = os.path.join(os.path.dirname(__file__), '3.jpg') # Your image path from current directory 


client = vision.ImageAnnotatorClient()

with io.open(path, 'rb') as image_file:
    content = image_file.read()

image = vision.types.Image(content=content)

response = client.text_detection(image=image)
texts = response.text_annotations
print('Texts:')

for text in texts:
    print(format(text.description))

在这段代码中,我们需要让 API 只通过 'cv2' 函数读取图像,而不是使用 'io' 函数:

# Read image file
    with io.open(img_path, 'rb') as image_file:
        content = image_file.read()

任何建议都会有所帮助

【问题讨论】:

  • 如果答案为未来用户解决了您的问题,请点赞并接受!

标签: python python-3.x google-cloud-platform google-cloud-vision


【解决方案1】:

您所需要的只是将从cv2 创建的numpy 数组转换为Google Vision API 使用的字节。以下是你的做法:

import cv2 
with open(path, 'rb') as image_file:
    content1 = image_file.read()
image = cv2.imread(path)
success, encoded_image = cv2.imencode('.jpg', image)
content2 = encoded_image.tobytes()
image_cv2 = vision.types.Image(content=content2)
response =  client.text_detection(image=image_cv2)
texts = response.text_annotations

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-05
    • 1970-01-01
    • 2020-03-17
    • 1970-01-01
    • 2019-05-25
    • 2020-03-20
    相关资源
    最近更新 更多