I am having trouble accessing "analysing an image" feature in gpt-4o

def generate_description_from_image(image_path):
openai.api_key = ‘sk-proj-xxxxxxxxxxxxxX’
headers = {
‘Content-Type’: ‘application/json’,
‘Authorization’: f’Bearer {API_KEY}',
}
with open(image_path, ‘rb’) as image_file:
image_data = image_file.read()

# Encode the image in base64
image_base64 = base64.b64encode(image_data).decode('utf-8')

# Prepare the prompt with the base64 image data
prompt = f"Describe this image: <img src='data:image/png;base64,{image_base64}'>"

try:
    response = openai.chat.completions.create(
        model="gpt-4o",  
        messages=[
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": prompt}
        ]
    )
    
    if response and response.choices:
        # Accessing the content from the 'choices' list correctly
        return response.choices[0].message.content.strip()
    else:
        return "Error: Unable to generate description."
except Exception as e:
    print(f"An error occurred: {e}")
    return f"Error: Unable to generate description, {e}"

Output:
It appears that you are trying to describe a base64-encoded image. However, I cannot directly see or interpret images, including those encoded in base64. If you can, please provide a detailed description of the image’s content, and I’d be happy to help interpret or elaborate on it based on your description.

i am new to this, please help me out

Here is an image analysis function I have that works, hopefully you can see the logic and flow and adapt it to your needs:

def analyze_image_with_openai(image, client, custom_prompt):
    base64_image = encode_image(image)
    chat_completion = client.chat.completions.create(
        messages=[
            {
                "role": "user",
                "content": [
                    {"type": "text", "text": custom_prompt},
                    {
                        "type": "image_url",
                        "image_url": {
                            "url": f"data:image/png;base64,{base64_image}",
                            "detail": "high"
                        }
                    }
                ]
            }
        ],
        seed=42,
        temperature=0,
        max_tokens=300,
        model="gpt-4-vision-preview",
    )

    return chat_completion.choices[0].message. Content
5 Likes

Worked for me, @Foxalabs Thanks! :smiley:

2 Likes