-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathlisting2.py
More file actions
106 lines (85 loc) · 3.19 KB
/
Copy pathlisting2.py
File metadata and controls
106 lines (85 loc) · 3.19 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
'''
Created on Nov 10, 2023
@author: immanueltrummer
'''
import argparse
import base64
import os
import requests
import shutil
def load_images(in_dir):
""" Loads images from a directory.
Args:
in_dir: path of input directory.
Returns:
directory mapping file names to PNG images.
"""
name_to_image = {}
file_names = os.listdir(in_dir)
for file_name in file_names:
if file_name.endswith('.png'):
image_path = os.path.join(in_dir, file_name)
with open(image_path, 'rb') as image_file:
encoded = base64.b64encode(image_file.read())
image = encoded.decode('utf-8')
name_to_image[file_name] = image
return name_to_image
def create_prompt(person_image, image_to_label):
""" Create prompt to compare images.
Args:
person_image: image showing a person.
image_to_label: image to assign to a label.
Returns:
prompt to verify if the same person appears in both images.
"""
task = {'type':'text',
'text':'Do the images show the same person ("Yes"/"No")?'}
prompt = [task]
for image in [person_image, image_to_label]:
image_url = {'url':f'data:image/png;base64,{image}'}
image_msg = {'type':'image_url', 'image_url':image_url}
prompt += [image_msg]
return prompt
def call_llm(ai_key, prompt):
""" Call language model to process prompt with local images.
Args:
ai_key: key to access OpenAI.
prompt: a prompt merging text and local images.
Returns:
answer by the language model.
"""
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {ai_key}'
}
payload = {
'model': 'gpt-4o',
'messages': [
{'role': 'user', 'content': prompt}
],
'max_tokens':1
}
response = requests.post(
'https://api.openai.com/v1/chat/completions',
headers=headers, json=payload)
return response.json()['choices'][0]['message']['content']
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('peopledir', type=str, help='Contains images of people')
parser.add_argument('picsdir', type=str, help='Contains images to label')
parser.add_argument('outdir', type=str, help='Contains processing result')
args = parser.parse_args()
people_images = load_images(args.peopledir)
unlabeled_images = load_images(args.picsdir)
for person_name, person_image in people_images.items():
for un_name, un_image in unlabeled_images.items():
prompt = create_prompt(person_image, un_image)
ai_key = os.getenv('OPENAI_API_KEY')
response = call_llm(ai_key, prompt)
description = f'{un_name} versus {person_name}?'
print(f'{description} -> {response}')
if response == 'Yes':
labeled_name = f'{person_name[:-4]}{un_name}'
source_path = os.path.join(args.picsdir, un_name)
target_path = os.path.join(args.outdir, labeled_name)
shutil.copy(source_path, target_path)