Created
October 1, 2024 13:48
-
-
Save dmd/becfde0d264338a885968f7d5fae1964 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import os | |
import anthropic | |
from anthropic import Anthropic | |
from base64 import b64encode | |
from PIL import Image, ExifTags | |
import io | |
def fix_orientation(image): | |
try: | |
for orientation in ExifTags.TAGS.keys(): | |
if ExifTags.TAGS[orientation] == 'Orientation': | |
break | |
exif = dict(image._getexif().items()) | |
if exif[orientation] == 3: | |
image = image.rotate(180, expand=True) | |
elif exif[orientation] == 6: | |
image = image.rotate(270, expand=True) | |
elif exif[orientation] == 8: | |
image = image.rotate(90, expand=True) | |
except (AttributeError, KeyError, IndexError): | |
# Cases: image don't have getexif | |
pass | |
return image | |
def resize_image(image_path, scale_factor=1, debug=False): | |
try: | |
with Image.open(image_path) as img: | |
img = fix_orientation(img) | |
width, height = img.size | |
new_width = int(width * scale_factor) | |
new_height = int(height * scale_factor) | |
resized_img = img.resize((new_width, new_height), Image.LANCZOS) | |
if debug: | |
resized_img.save("testfile.jpg", "JPEG") | |
print(f"Resized image saved as testfile.jpg") | |
print(f"Original size: {width}x{height}, Resized: {new_width}x{new_height}") | |
buffered = io.BytesIO() | |
resized_img.save(buffered, format="JPEG") | |
return b64encode(buffered.getvalue()).decode('utf-8') | |
except Exception as e: | |
print(f"Error processing image: {e}") | |
return None | |
def classify_image(image_path): | |
client = Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY")) | |
base64_image = resize_image(image_path, scale_factor=0.2) | |
if base64_image is None: | |
return "Unable to process image" | |
try: | |
response = client.messages.create( | |
model="claude-3-sonnet-20240229", | |
max_tokens=1000, | |
messages=[ | |
{ | |
"role": "user", | |
"content": [ | |
{ | |
"type": "image", | |
"source": { | |
"type": "base64", | |
"media_type": "image/jpeg", | |
"data": base64_image | |
} | |
}, | |
{ | |
"type": "text", | |
"text": "Is this person an adult male, adult female, child with glasses, or child without glasses? Reply with only exactly one of those options or 'unable to classify'. Do not reply with any other text whatsoever." | |
} | |
] | |
} | |
] | |
) | |
classification = response.content[0].text.strip().lower() | |
# print(f"Raw Claude response: {classification}") # Debug print | |
if classification == "adult male": | |
return "dada" | |
elif classification == "adult female": | |
return "mama" | |
elif classification == "child with glasses": | |
return "capy" | |
elif classification == "child without glasses": | |
return "platy" | |
else: | |
return "Unable to classify" | |
except Exception as e: | |
print(f"Error calling Anthropic API: {e}") | |
return "Error in classification process" | |
if __name__ == "__main__": | |
if len(sys.argv) != 2: | |
print("Usage: python script.py <image_filename>") | |
sys.exit(1) | |
image_path = sys.argv[1] | |
if not os.path.exists(image_path): | |
print(f"Error: File '{image_path}' does not exist.") | |
sys.exit(1) | |
result = classify_image(image_path) | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment