Files
Botator/tests/test-google-vision.py

62 lines
1.5 KiB
Python
Raw Permalink Normal View History

2023-03-15 22:30:54 +01:00
import io
import os
import asyncio
2023-03-15 22:30:54 +01:00
# Imports the Google Cloud client library
from google.cloud import vision
# we set the env variable GOOGLE_APPLICATION_CREDENTIALS to the path of the json file
os.environ[
"GOOGLE_APPLICATION_CREDENTIALS"
] = "./../database/google-vision/botator-vision-8cd1030a7541.json"
2023-03-15 22:30:54 +01:00
# Instantiates a client
client = vision.ImageAnnotatorClient()
# The name of the image file to annotate
file_name = os.path.abspath("./../database/google-vision/label.jpg")
2023-03-15 22:30:54 +01:00
print(file_name)
# Loads the image into memory
with io.open(file_name, "rb") as image_file:
2023-03-15 22:30:54 +01:00
content = image_file.read()
image = vision.Image(content=content)
# Performs label detection on the image file
# response = client.label_detection(image=image)
# labels = response.label_annotations
2023-03-15 22:30:54 +01:00
# print('Labels:')
# for label in labels:
2023-03-15 22:30:54 +01:00
# print(label.description)
2023-03-15 22:30:54 +01:00
async def get_labels(image):
response = client.label_detection(image=image)
labels = response.label_annotations
return labels
2023-03-15 22:30:54 +01:00
async def get_text(image):
response = client.text_detection(image=image)
texts = response.text_annotations
return texts
# now we print the labels
2023-03-15 22:30:54 +01:00
async def main():
labels = await get_labels(image)
print("Labels:")
2023-03-15 22:30:54 +01:00
for label in labels:
print(label.description)
texts = await get_text(image)
print("Texts:")
2023-03-15 22:30:54 +01:00
for text in texts:
print(text.description)
# now we run the main function
if __name__ == "__main__":
2023-03-15 22:30:54 +01:00
loop = asyncio.get_event_loop()
loop.run_until_complete(main())