-
Notifications
You must be signed in to change notification settings - Fork 0
/
recognize.py
74 lines (61 loc) · 2.01 KB
/
recognize.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import vertexai
from vertexai.vision_models import ImageTextModel, Image
import base64
import requests
import sys
import os
PROJECT_ID = 'eminent-tesla-402318'
OPENAI_KEY = os.getenv("OPENAI_API_KEY")
def caption_image_old(image_path):
vertexai.init(project=PROJECT_ID)
model = ImageTextModel.from_pretrained("imagetext@001")
source_image = Image.load_from_file(location=image_path)
captions = model.get_captions(
image=source_image,
# Optional:
number_of_results=1,
language="en",
)
return captions[0]
# Function to encode the image
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode('utf-8')
def caption_image(image_path):
# Getting the base64 string
base64_image = encode_image(image_path)
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {OPENAI_KEY}"
}
payload = {
"model": "gpt-4o",
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "What are the main objects in this image? Do not describe the surroundings. If the object is an image or figurine or toy, only describe what the object portrays. Do not mention that it's an image, or toy, or figurine. Do give some details around colors, shape, etc."
},
{
"type": "image_url",
"image_url": {
"url": f"data:image/jpeg;base64,{base64_image}"
}
}
]
}
],
"max_tokens": 200
}
response = requests.post("https://api.openai.com/v1/chat/completions", headers=headers, json=payload)
response_json = response.json()
return response_json['choices'][0]['message']['content']
if __name__ == "__main__":
if len(sys.argv) >= 2:
image_path = sys.argv[1]
else:
image_path = 'camera.png'
caption = caption_image(image_path)
print(caption)