Skip to content

How to Make Your Images Speak Multiple Languages

Are you looking to enhance your application's accessibility or localization by providing image descriptions in multiple languages?

With Groq’s fast inference and the llama-3.2-90b-vision model, you can generate detailed, accurate image descriptions in English, Spanish, German, and more.

This implementation allows you to upload an image, convert it to base64 format, and request descriptions in multiple languages. Perfect for projects where visual content needs to be understood globally!

The Problem

Many applications require automatic image description generation, especially for accessibility or internationalization purposes. However, generating accurate, contextually-rich descriptions in multiple languages can be a challenge. Manual translation of these descriptions is time-consuming and error-prone.

For example, if you want to create a global e-commerce platform, you need to describe images in various languages to accommodate users across different regions. This is where automation can save time and improve user experience.

The Implementation

Here’s a step-by-step guide on how to use the solution to analyze an image and get descriptions in multiple languages:

import os
from groq import Groq
import base64
from dotenv import load_dotenv

# Load environment variables
load_dotenv()

# Initialize Groq client
client = Groq(api_key=os.getenv("GROQ_API_KEY"))

def encode_image(image_path):
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode('utf-8')

def analyze_image_multilingual(image_path, languages):
    base64_image = encode_image(image_path)

    descriptions = {}

    for lang in languages:
        response = client.chat.completions.create(
            model="llama-3.2-90b-vision-preview",
            messages=[
                {
                    "role": "user",
                    "content": [
                        {"type": "text", "text": f"Describe this image in {lang}. Provide a detailed description."},
                        {
                            "type": "image_url",
                            "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}
                        }
                    ]
                }
            ]
        )
        descriptions[lang] = response.choices[0].message.content

    return descriptions

def main():
    image_path = "path/to/your/image.jpg" 
    languages = ["English", "Spanish", "German"]

    multilingual_descriptions = analyze_image_multilingual(image_path, languages)

    for lang, description in multilingual_descriptions.items():
        print(f"\n{lang} Description:")
        print(description)
        print("-" * 50)

if __name__ == "__main__":
    main()
Note

Preview models are intended for evaluation purposes only and should not be used in production environments as they may be discontinued at short notice.

Output

English Description: The image depicts a kingfisher perched on a branch, showcasing its vibrant plumage and majestic appearance. The bird's feathers are predominantly blue, with white accents on its chest and belly, while its wings display a mix of blue and orange hues.

Key Features:

  • Beak: Long and black
  • Eyes: Black
  • Legs: Short and red
  • Feathers: Primarily blue, with white accents on the chest and belly, and a blend of blue and orange on the wings

Background:

  • The background is a blurred landscape featuring shades of green and yellow, creating a soft and natural ambiance that complements the bird's colors.

Overall Impression:

  • The image presents a striking portrait of the kingfisher, highlighting its unique characteristics and beauty against a serene backdrop.

Spanish Description:

Descripción del imagen en español

El imagen muestra un ave de color azul y naranja, con un largo pico y patas rojas. La ave está sentada en una rama blanca, con la cabeza inclinada hacia la izquierda y las alas relajadas a los lados del cuerpo. El fondo es de un color marrón claro con un gradiente hacia el verde.

Characteristics

  • Color del ave: El plumaje de la ave es principalmente de un azul brillante, con detalles naranjas en la garganta y una raya blanca en el pecho. Las patas son de un rojo intenso.
  • Forma general: La forma del ave es robusta, con un cuerpo compacto y alas y cola proporcionalmente largas.
  • Especificaciones físicas:
    • Pico: El pico es largo y delgado, ideal para capturar peces en el agua.
    • Patron de vuelo: No se aprecia el patron de vuelo debido a que el ave está en un reposo completo en una rama.
  • Entorno de captura: Según se puede observar en la imagen de fondo, el ave ha sido capturada en un entorno natural, probablemente cerca de un cuerpo de agua, dado que el ave es una águila pescadora.
  • Tipo de conteinedor: El ave está situada sobre una rama blanca, que parece haber sido colocada artificialmente como posadero.

En resumen, el imagen presenta una impresionante águila pescadora en su habitat natural.


German Description:

Die Abbildung zeigt ein Bild eines Vogels, der auf einem Ast sitzt. Der Vogel hat ein blaues Gefieder, orange Beine und einen langen schwarzen Schnabel. Der Hintergrund des Bildes ist bunt und verwischt.

Das Bild könnte von einem Fotografen aufgenommen worden sein, der sich für Vögel interessiert oder in der Natur fotografiert. Es könnte auch Teil einer Sammlung von Bildern über Vögel sein oder in einem Naturmagazin veröffentlicht worden sein.

Die Farben des Bildes sind lebhaft und helfen dabei, den Vogel zu betonen. Der Fokus liegt klar auf dem Vogel, während der Hintergrund unscharf ist. Dies suggeriert, dass der Fotograf den Vogel gerne in den Vordergrund rücken wollte.

Insgesamt handelt es sich bei dem Bild um eine attraktive und informative Darstellung eines Vogels in seiner natürlichen Umgebung. Es könnte von großen Tierfreunden und Naturfotografen eingesetzt werden.


How It Works

  1. encode_image(image_path): This function reads an image from your computer and converts it into a base64 string. This step is necessary because the AI model can only process images in this format.

  2. analyze_image_multilingual(image_path, languages): Once the image is encoded, this function sends it to the Groq AI platform to generate descriptions in the languages you specify. It sends the image and a request for a detailed description in each language. The descriptions are returned in a dictionary, where each language is a key, and the description is the value.

  3. The Groq API client is set up using an API key (stored in an environment file for security). The client interacts with the Llama-3.2-90b-vision-preview model, which specializes in processing images and generating text descriptions.

  4. After processing the image, the model generates a description in each requested language. The result is a collection of descriptions in different languages, allowing you to easily display or use them wherever needed.

Conclusion

This implementation provides a scalable and automated way to generate multilingual image descriptions for various applications. Whether you’re enhancing accessibility for users with disabilities or localizing content for a global audience, Groq’s API and llama-3.2-90b-vision-preview model can simplify and accelerate the process.