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