For detecting text in Google cloud, is there a way to just print the texts once rather than printing them line-by-line?

Viewed 33

For example, given a image with a "CAUTION Otters crossing for next 6 miles" sign:

response = client.text_detection(image=image)
text = response.text_annotations
for text in text:
    print(text.description)

This will print this:

CAUTION
Otters crossing
for next 6 miles
CAUTION
Otters
crossing
for
next
6
miles

However, I just want the first part:

CAUTION
Otters crossing
for next 6 miles

Is there a way to make that happen?

1 Answers

text.description is an array, so you could remove the for cycle and only use print(text[0].description).

The code should look like the below one:

response = client.text_detection(image=image)
text = response.text_annotations
    print(text[0].description)

Additionally as seen in the documentation you could use document_text_detection instead of text_detection because document_text_detection is more accurate.

Text detection is used for detecting some text in an image. Basically it gives text values which are found in it, so it can be accurate if the text of the image is in a particular order.

Meanwhile, document text detection is very good in accuracy and detects each detail from the document.

Using document_text_detection the code should look like:

response = client.document_text_detection(image=image)
text = response.text_annotations
    print(text.description)
Related