|
|
import os |
|
|
import json |
|
|
import sys |
|
|
import uuid |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
modifications = [ |
|
|
"eye. ", |
|
|
"eyebrow. ", |
|
|
"hair. ", |
|
|
"nose. ", |
|
|
"lip. " |
|
|
] |
|
|
|
|
|
|
|
|
def get_image_files(directory): |
|
|
image_files = [] |
|
|
for filename in os.listdir(directory): |
|
|
if filename.endswith(".jpg") or filename.endswith(".png"): |
|
|
image_files.append(filename) |
|
|
return image_files |
|
|
|
|
|
def get_idx(string): |
|
|
|
|
|
deepfakes = [ |
|
|
"eye", |
|
|
"eyebrow", |
|
|
"hair", |
|
|
"nose", |
|
|
"lip", |
|
|
] |
|
|
for i in range(len(deepfakes)): |
|
|
if (deepfakes[i]==string): |
|
|
return i |
|
|
|
|
|
|
|
|
def get_answer(directory): |
|
|
tag = directory[32:len(directory)-1] |
|
|
print(tag) |
|
|
|
|
|
ans=[] |
|
|
count = 0 |
|
|
while (len(tag)>0): |
|
|
count+=1 |
|
|
if (count>5): break |
|
|
x=tag.find('-') |
|
|
|
|
|
if (x==-1): |
|
|
ans.append(get_idx(tag)) |
|
|
break |
|
|
str=tag[0:x] |
|
|
tag=tag[x+1:] |
|
|
ans.append(get_idx(str)) |
|
|
|
|
|
return ans |
|
|
|
|
|
|
|
|
|
|
|
def create_json(image_dir, json_file_path): |
|
|
|
|
|
image_files = get_image_files(image_dir) |
|
|
|
|
|
|
|
|
image_entries = [] |
|
|
with open(json_file_path, "r") as json_file: |
|
|
image_entries=json.load(json_file) |
|
|
|
|
|
|
|
|
answer="" |
|
|
bruh = get_answer(image_dir) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for i in bruh: |
|
|
answer=answer+modifications[i] |
|
|
|
|
|
for image_file in image_files: |
|
|
image_path = os.path.join(image_dir, image_file) |
|
|
entry_id = str(uuid.uuid4()) |
|
|
entry = { |
|
|
"id": entry_id, |
|
|
"image": image_path, |
|
|
"conversations": [ |
|
|
{ |
|
|
"from": "human", |
|
|
"value": "This image could possibly have deepfake modifications made to it." + " ".join(modifications) + "Thus your job is to figure out all the modifications. Extraneous guesses will be penalized. There is a chance that no modifications were made." |
|
|
}, |
|
|
{ |
|
|
"from": "gpt", |
|
|
"value": answer |
|
|
} |
|
|
] |
|
|
} |
|
|
image_entries.append(entry) |
|
|
|
|
|
|
|
|
with open(json_file_path, "w") as json_file: |
|
|
json.dump(image_entries, json_file, indent=4) |
|
|
|
|
|
print("JSON file created successfully at", json_file_path) |
|
|
|
|
|
|
|
|
if len(sys.argv) != 3: |
|
|
print("Usage: python script.py <image_directory> <json_output_file>") |
|
|
sys.exit(1) |
|
|
|
|
|
|
|
|
image_dir = sys.argv[1] |
|
|
json_file_path = sys.argv[2] |
|
|
|
|
|
|
|
|
if not os.path.isdir(image_dir): |
|
|
print("Error: The specified directory does not exist.") |
|
|
sys.exit(1) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
create_json(image_dir, json_file_path) |