Source code for odoo_xmlrpc_twisted.functions.get_label

"""
Detect name and description of an object.
"""

from google.cloud  import vision
from google.cloud  import translate_v3 as translate
from google.oauth2 import service_account
import base64
import os
import configparser
import webcolors

try:
    from .get_language import get_language
except ImportError:
    from get_language import get_language 


# ------- for the initialisation get parameters from the File "parameter_google.ini" -------

# Name and full path of the Parameter File
file_name  = "parameter_google.ini"
path_name  = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))  # Go up to repo root
config_path = os.path.join(path_name, 'config')
fullpath   = os.path.join(config_path, file_name)

config = configparser.ConfigParser()
config.read(fullpath)

# get the Google Credentials
google_file_name        = config.get('credentials_google', 'credentials_filename')
google_project_id       = config.get('credentials_google', 'project_id')
Google_Credentials_File = os.path.join(config_path, google_file_name)
Google_Credentials      = service_account.Credentials.from_service_account_file(Google_Credentials_File)


# Instantiates a client for Google Cloud Vision API
vision_client    = vision.ImageAnnotatorClient(credentials=Google_Credentials)


# Instantiates a client for Google Cloud Translation API
translate_client = translate.TranslationServiceClient.from_service_account_file(Google_Credentials_File)
# The "parent" parameter is used to tell the API what project and location will be used to process the request
parent = f"projects/{google_project_id}/locations/global"



# ------- end of the initialisation ------- 


[docs] def get_label(partner_id, image): """ Function uses object recognition based on Google Cloud Vision API The objects are detected and labelled for the storage on odoo ERP System and on the Dashboard for the clients. "partner_id": is used to get the language of the client for the labelling "image": image stream of the object Result is the name, colour and description from the image of an object. """ image_obj = vision.Image(content=base64.b64decode(image)) # Performs label detection on the image file response = vision_client.label_detection(image=image_obj) labels = response.label_annotations # Get the first value as it is the best strike if len(labels) > 0: label = labels[0].description else: label = "No Description" # Performs logo detection on the image file response = vision_client.logo_detection(image=image_obj) logos = response.logo_annotations # Get the first value as it is the best strike if len(logos) > 0: logo = logos[0].description else: logo = "" # Detects text in the image file response = vision_client.text_detection(image=image_obj) texts = response.text_annotations # Get the first value as it is the best strike if len(texts) > 0: text = texts[0].description else: text = "" # Detects the main colour in the image file response = vision_client.image_properties(image=image_obj) props = response.image_properties_annotation red = (props.dominant_colors.colors[0].color.red) green = (props.dominant_colors.colors[0].color.green) blue = (props.dominant_colors.colors[0].color.blue) rgb_triplet = (red, green, blue) # Function to get the main colour of an object. # webcolors is a module for working with HTML/CSS color definitions. # Support is included for normalizing and converting between formats # like six-digit hexadecimal and three-digit hexadecimal. # See https://pypi.org/project/webcolors/ # Result is the main colour of the object. min_colours = {} for key, name in list(webcolors.CSS21_HEX_TO_NAMES.items()): r_c, g_c, b_c = webcolors.hex_to_rgb(key) rd = (r_c - rgb_triplet[0]) ** 2 gd = (g_c - rgb_triplet[1]) ** 2 bd = (b_c - rgb_triplet[2]) ** 2 min_colours[(rd + gd + bd)] = name colour = min_colours[min(min_colours.keys())] # Get the target language of the partner for whom the product will be created. # by calling the Python function "get_language" lang = get_language(partner_id) # Get the first 2 charcters as this is the ISO norm for language. target_language = lang[0:2] # Translates the label into the target language. # As English is the language used by Google Translate for detection of labels and colour, # english terms will not be translated. if target_language != 'en': if label != '': text_translate = label # Translate the text response = translate_client.translate_text( contents=[text_translate], target_language_code=target_language, parent=parent ) # Get the translated text from the response label = response.translations[0].translated_text # Translates the colour into the target lanuguage if colour != '': text_translate = colour # Translate the colour response = translate_client.translate_text( contents=[text_translate], target_language_code=target_language, parent=parent ) # Get the translated text from the response colour = response.translations[0].translated_text # Create the product name (by label, colour and logo) and the description. # Use the subjects "logo" and "colour" only when they are not empty. if logo != '' and colour != '': custom_name = label + ' ' + colour + ' ' + logo elif logo == '' and colour != '': custom_name = label + ' ' + colour elif logo != '' and colour == '': custom_name = label + ' ' + logo else: custom_name = label # Change the first letter of the newly builded string in upper-case letter. custom_name = custom_name.capitalize() if text != '': custom_description = text else: custom_description = '' return(custom_name, custom_description)