var*_*ain 1 python python-3.x google-cloud-platform google-cloud-vision
我们正在尝试使用 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))
Run Code Online (Sandbox Code Playgroud)
在这段代码中,我们需要让 API 仅通过“cv2”函数读取图像,而不是使用“io”函数:
# Read image file
with io.open(img_path, 'rb') as image_file:
content = image_file.read()
Run Code Online (Sandbox Code Playgroud)
任何建议都会有帮助
您所需要做的就是将创建的 numpy 数组转换为cv2所使用的字节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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1950 次 |
| 最近记录: |