import requests
import json
import base64
from dotenv import find_dotenv, load_dotenv
from openai import OpenAI
import os
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
image_path1 = "2.jpg"
base64_image = encode_image(image_path1)
url = "https://api.openai-hk.com/v1/chat/completions"
load_dotenv(find_dotenv('.env'))
openai_api_key = os.getenv("OPENAI_API_KEY")
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {openai_api_key}"
}
data = {
"model": "gpt-4-vision-preview",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "What's in this image?"
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}",
},
},
],
}
],
"max_tokens": 300,
}
response = requests.get(url, headers=headers, data=json.dumps(data))
result = response.json()['choices'][0]['message']['content']
The above is my code. I wrote this with reference to the tutorial on the OpanAi official website. The only small difference is that I used a transit api key from HongKong, rather than the official api key provided by OpenAi. It reported an error after running the above code, that is:
enter image description here
I don’t know what the problem is and how to solve it.
However, When I fill in the “url” parameter with the image website link instead of the base64 encoding of the image, the program works and gives a description of the image. The code is as below:
import requests
import json
import base64
from dotenv import find_dotenv, load_dotenv
import os
url = "https://api.openai-hk.com/v1/chat/completions"
load_dotenv(find_dotenv('.env'))
openai_api_key = os.getenv("OPENAI_API_KEY")
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {openai_api_key}"
}
data = {
"model": "gpt-4-vision-preview",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "What’s in this image?"},
{
"type": "image_url",
"image_url": {
"url": "https://oaidalleapiprodscus.blob.core.windows.net/private/org-N3OBmeiA6wEpyoUVwrgkQRRi/user-uL9kKgdQjdtISTWOgDksR0fD/img-eU3OOI4UB4FG7CZ8admUcWch.png?st=2023-12-13T06%3A44%3A53Z&se=2023-12-13T08%3A44%3A53Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-12-12T08%3A30%3A31Z&ske=2023-12-13T08%3A30%3A31Z&sks=b&skv=2021-08-06&sig=/Cw/Dbo5pgf2eApqNZB8YHvcGCsk%2B2BIaktlZVWZDR8%3D",
},
},
],
}
],
"max_tokens": 300,
}
response = requests.post(url, headers=headers, data=json.dumps(data))
result = response.json()['choices'][0]['message']['content']
print(result)
But For my actual task requirements, I want to be able to use a local image, or even images extracted from a video, so the “url” parameter can not be a website link, instead, it should be a base64 image. However, when I use base64 Image, like above, it goes wrong.
Hope someone could help me, thanks!