coin-identification-ai / utils /image_tools.py
dbeck22's picture
the coin expert initial setup and build
821310d
import openai
import requests
# Use OpenAI Vision to compare a user-uploaded image to reference images
def compare_images(user_image_path, reference_image_urls):
with open(user_image_path, "rb") as user_img:
user_bytes = user_img.read()
# Download the first reference image for comparison
if not reference_image_urls:
return {"confidence": 0.0, "reason": "No reference images available."}
ref_response = requests.get(reference_image_urls[0])
if ref_response.status_code != 200:
return {"confidence": 0.0, "reason": "Failed to fetch reference image."}
ref_bytes = ref_response.content
# Use GPT-4 Vision to compare both images
response = openai.chat.completions.create(
model="gpt-4-vision-preview",
messages=[
{"role": "system", "content": "You are an expert in identifying error coins."},
{
"role": "user",
"content": [
{"type": "text", "text": "Do these images look like the same error coin?"},
{"type": "image", "image": user_bytes},
{"type": "image", "image": ref_bytes}
]
}
],
max_tokens=300
)
result_text = response.choices[0].message.content
# Very simple scoring logic based on confidence language
confidence = 0.8 if "yes" in result_text.lower() else 0.3
return {"confidence": confidence, "summary": result_text}