2024-08-10 16:10:25 -07:00
import base64
2024-08-11 14:28:00 -07:00
import os
2024-08-10 16:10:25 -07:00
import shutil
from openai import OpenAI
2024-08-16 23:40:50 -07:00
from wand . image import Image
2024-08-10 16:10:25 -07:00
client = OpenAI ( )
# List all files and directories in the specified path
path = ' data/RAW/SC1/BR1 '
2024-08-16 23:40:50 -07:00
pending = os . path . join ( path , ' pending ' )
2024-08-10 16:10:25 -07:00
bitting = None
for file in sorted ( os . listdir ( pending ) ) :
if file . startswith ( ' . ' ) :
continue
print ( file )
filename = os . path . join ( pending , file )
2024-08-16 23:40:50 -07:00
with Image ( filename = filename ) as img :
img . format = ' jpg '
img . resize ( 512 , 384 )
blob = img . make_blob ( )
blob_b64 = base64 . b64encode ( blob ) . decode ( ' utf-8 ' )
2024-08-10 16:10:25 -07:00
2024-08-11 14:28:00 -07:00
chat_completion = client . chat . completions . create (
messages = [
{
' role ' : ' user ' ,
' content ' : [
{
' type ' : ' text ' ,
' text ' : ' If this is a picture of a white card with black lettering on it, respond with just the 5 digit number written on the card. If this is a picture of a key, respond with " front " if you can see the engravings " SC1 " & " USA " , and " back " if you cannot. '
} ,
{
' type ' : ' image_url ' ,
' image_url ' : {
' url ' : f ' data:image/jpeg;base64, { blob_b64 } '
2024-08-10 16:10:25 -07:00
}
2024-08-11 14:28:00 -07:00
}
]
}
] ,
model = ' gpt-4o ' ,
)
2024-08-10 16:10:25 -07:00
2024-08-11 14:28:00 -07:00
content = chat_completion . choices [ 0 ] . message . content
2024-08-10 16:10:25 -07:00
2024-08-11 14:28:00 -07:00
print ( content )
2024-08-10 16:10:25 -07:00
2024-08-11 14:28:00 -07:00
match content :
case ' front ' :
assert bitting is not None , ' key image before card image '
dest = os . path . join ( path , bitting , ' front.orf ' )
assert not os . path . exists ( dest ) , ' key image already exists '
shutil . move ( filename , dest )
2024-08-10 16:10:25 -07:00
2024-08-11 14:28:00 -07:00
case ' back ' :
assert bitting is not None , ' key image before card image '
dest = os . path . join ( path , bitting , ' back.orf ' )
assert not os . path . exists ( dest ) , ' key image already exists '
shutil . move ( filename , dest )
2024-08-10 16:10:25 -07:00
2024-08-11 14:28:00 -07:00
case _ :
bitting = content
os . makedirs ( os . path . join ( path , bitting ) , exist_ok = True )
dest = os . path . join ( path , bitting , ' card.orf ' )
assert not os . path . exists ( dest ) , ' card image already exists '
shutil . move ( filename , dest )