如何更改视觉OCR的注释功能?



我正在尝试使用Python和Vision从本地图像中提取文本,基于云视觉API:检测图像中的文本。

这是提取文本的函数:

def detect_text(path):
    """Detects text in the file."""
    from google.cloud import vision
    import io
    client = vision.ImageAnnotatorClient()
with io.open(path, 'rb') as image_file:
        content = image_file.read()
image = vision.Image(content=content)
response = client.text_detection(image=image)
    texts = response.text_annotations

它可以工作,但是我想指定使用像TEXT_DETECTION这样的功能,而不是默认的DOCUMENT_TEXT_DETECTION功能,以及指定语言提示。我该怎么做呢?text_detection函数似乎不接受这些参数。

或者您可以通过添加image_context对象来请求语言提示:

response = client.text_detection(image=image,
image_context={"language_hints": ["en"]})

下面的文章对此进行了解释,向下滚动到"创建应用程序"一节。

你需要添加一个请求对象到你的代码

request = {
"image": {
"source": {
"image_uri": "IMAGE_URL"
}
},    
"features": [
{
"type": "TEXT_DETECTION"
}
]
"imageContext": {
"languageHints": ["en-t-i0-handwrit"]
}
}

然后将其传递给请求。

response = client.annotate_image(request)

最新更新