Source code for odoo_xmlrpc_twisted.functions.get_label
"""Detect name and description of an object."""fromgoogle.cloudimportvisionfromgoogle.cloudimporttranslate_v3astranslatefromgoogle.oauth2importservice_accountimportbase64importosimportconfigparserimportwebcolorstry:from.get_languageimportget_languageexceptImportError:fromget_languageimportget_language# ------- for the initialisation get parameters from the File "parameter_google.ini" -------# Name and full path of the Parameter Filefile_name="parameter_google.ini"path_name=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))# Go up to repo rootconfig_path=os.path.join(path_name,'config')fullpath=os.path.join(config_path,file_name)config=configparser.ConfigParser()config.read(fullpath)# get the Google Credentialsgoogle_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 APIvision_client=vision.ImageAnnotatorClient(credentials=Google_Credentials)# Instantiates a client for Google Cloud Translation APItranslate_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 requestparent=f"projects/{google_project_id}/locations/global"# ------- end of the initialisation -------
[docs]defget_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 fileresponse=vision_client.label_detection(image=image_obj)labels=response.label_annotations# Get the first value as it is the best strike iflen(labels)>0:label=labels[0].descriptionelse:label="No Description"# Performs logo detection on the image fileresponse=vision_client.logo_detection(image=image_obj)logos=response.logo_annotations# Get the first value as it is the best strikeiflen(logos)>0:logo=logos[0].descriptionelse:logo=""# Detects text in the image fileresponse=vision_client.text_detection(image=image_obj)texts=response.text_annotations# Get the first value as it is the best strikeiflen(texts)>0:text=texts[0].descriptionelse:text=""# Detects the main colour in the image fileresponse=vision_client.image_properties(image=image_obj)props=response.image_properties_annotationred=(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={}forkey,nameinlist(webcolors.CSS21_HEX_TO_NAMES.items()):r_c,g_c,b_c=webcolors.hex_to_rgb(key)rd=(r_c-rgb_triplet[0])**2gd=(g_c-rgb_triplet[1])**2bd=(b_c-rgb_triplet[2])**2min_colours[(rd+gd+bd)]=namecolour=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.iftarget_language!='en':iflabel!='':text_translate=label# Translate the textresponse=translate_client.translate_text(contents=[text_translate],target_language_code=target_language,parent=parent)# Get the translated text from the responselabel=response.translations[0].translated_text# Translates the colour into the target lanuguageifcolour!='':text_translate=colour# Translate the colourresponse=translate_client.translate_text(contents=[text_translate],target_language_code=target_language,parent=parent)# Get the translated text from the responsecolour=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.iflogo!=''andcolour!='':custom_name=label+' '+colour+' '+logoeliflogo==''andcolour!='':custom_name=label+' '+coloureliflogo!=''andcolour=='':custom_name=label+' '+logoelse:custom_name=label# Change the first letter of the newly builded string in upper-case letter. custom_name=custom_name.capitalize()iftext!='':custom_description=textelse:custom_description=''return(custom_name,custom_description)