deepfaketar / python /llavatestprep.py
blorg469's picture
Upload folder using huggingface_hub
90cd92a verified
import jsonlines
import random
import os
import json
# if len(sys.argv) != 2:
# print("Usage: python script.py <image_directory>")
# sys.exit(1)
# # Get command-line arguments
# IMG_PATH = sys.argv[1] #directory path
#print(IMG_PATH)
# Configuration
IMG_PATHS = [
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Young-Smiling-Eyeglasses/042200.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Young-Smiling-Eyeglasses/033209.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Young-Smiling-Eyeglasses/018822.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Beard-Young/023024.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Beard-Young/052001.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Beard-Young/058170.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Smiling-Eyeglasses-Young-Beard-Bangs/011362.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Smiling-Eyeglasses-Young-Beard-Bangs/046696.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Smiling-Eyeglasses-Young-Beard-Bangs/059202.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Beard-Eyeglasses-Bangs/057969.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Beard-Eyeglasses-Bangs/058132.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Beard-Eyeglasses-Bangs/022461.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Bangs/063625.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Bangs/063630.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Bangs/063654.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Eyeglasses-Bangs/015686.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Eyeglasses-Bangs/040011.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Eyeglasses-Bangs/055742.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Beard-Bangs-Eyeglasses-Young/029877.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Beard-Bangs-Eyeglasses-Young/046520.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Beard-Bangs-Eyeglasses-Young/052232.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Bangs-Smiling/005454.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Bangs-Smiling/033267.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Bangs-Smiling/058542.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Young-Eyeglasses/045280.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Young-Eyeglasses/063402.jpg",
"/mnt/user/myang/OneDrive_1_9-6-2023/facial_attributes/images/train/Young-Eyeglasses/011625.jpg",
]
preset_prompt = "Tell me the probability of this image being real. Answer a probability score between 0 and 100."
output_file = "/fsx/ivan.laptev/Deepfake/deepfakehtml/my_code/json/llavatestquestionsdffd5.jsonl"
def create_jsonl_file(paths, prompt, output_file):
with jsonlines.open(output_file, mode='w') as writer:
for i, path in enumerate(paths):
filename = path.split('/')[-1]
question = {
"question_id": i,
"image": filename,
"text": prompt,
"category": "conversation"
}
writer.write(question)
# def create_jsonl_file_from_subdirs(root_dir, prompt, output_file):
# with jsonlines.open(output_file, mode='w') as writer:
# question_id = 0
# for subdir, _, files in os.walk(root_dir):
# # Filter out non-image files (you can customize the extensions based on your needs)
# image_files = [f for f in files if f.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp'))]
# if len(image_files) > 5:
# selected_images = random.sample(image_files, 5)
# else:
# selected_images = image_files
# for filename in selected_images:
# question = {
# "question_id": question_id,
# "image": os.path.join(subdir, filename),
# "text": prompt,
# "category": "detail"
# }
# writer.write(question)
# question_id += 1
# def create_jsonl_file_from_subdirs(root_dir, prompt, output_file):
# questions = []
# question_id = 0
# for subdir, _, files in os.walk(root_dir):
# # Filter out non-image files (you can customize the extensions based on your needs)
# image_files = [f for f in files if f.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp'))]
# if len(image_files) > 5:
# selected_images = random.sample(image_files, 5)
# else:
# selected_images = image_files
# for filename in selected_images:
# question = {
# "question_id": question_id,
# "image": os.path.join(subdir, filename),
# "text": prompt,
# "category": "detail"
# }
# questions.append(question)
# question_id += 1
# # Write the list of questions to a JSONL file
# with jsonlines.open(output_file, mode='w') as writer:
# writer.write_all(questions)
def create_json_array_file_from_subdirs(root_dir, prompt, output_file):
questions = []
question_id = 0
for subdir, _, files in os.walk(root_dir):
# Filter out non-image files (you can customize the extensions based on your needs)
image_files = [f for f in files if f.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp'))]
if len(image_files) > 5:
selected_images = random.sample(image_files, 5)
else:
selected_images = image_files
for filename in selected_images:
question = {
"question_id": question_id,
"image": os.path.join(subdir, filename),
"text": prompt,
"category": "detail"
}
questions.append(question)
question_id += 1
# Write the list of questions to a JSON file as an array
with open(output_file, 'w') as f:
json.dump(questions, f, indent=4)
def create_jsonl_file_from_subdirs(root_dir, prompt, output_file):
questions = []
question_id = 0
for subdir, _, files in os.walk(root_dir):
# Filter out non-image files (you can customize the extensions based on your needs)
image_files = [f for f in files if f.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp'))]
# if len(image_files) > 5:
# selected_images = random.sample(image_files, 5)
# else:
# selected_images = image_files
selected_images = image_files
for filename in selected_images:
question = {
"question_id": question_id,
"image": os.path.join(subdir, filename),
"text": prompt,
"category": "detail"
}
questions.append(question)
question_id += 1
# Write the list of questions to a JSONL file
with jsonlines.open(output_file, mode='w') as writer:
writer.write_all(questions)
def create_jsonl_file_from_dir(root_dir, prompt, output_file):
questions = []
question_id = 0
for subdir, _, files in os.walk(root_dir):
# Filter out non-image files (you can customize the extensions based on your needs)
image_files = [f for f in files if f.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp'))]
# if len(image_files) > 5:
# selected_images = random.sample(image_files, 5)
# else:
# selected_images = image_files
selected_images = image_files
for filename in selected_images:
question = {
"question_id": question_id,
"image": os.path.join(subdir, filename),
"text": prompt,
"category": "detail"
}
questions.append(question)
question_id += 1
# Write the list of questions to a JSONL file
with jsonlines.open(output_file, mode='w') as writer:
writer.write_all(questions)
def create_jsonl_file_from_dir(root_dir, prompt, output_file):
questions = []
question_id = 0
all_image_files = []
# Traverse the directory tree and collect all image files
for subdir, _, files in os.walk(root_dir):
image_files = [os.path.join(subdir, f) for f in files if f.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp'))]
all_image_files.extend(image_files)
# Select 25 random images from the collected image files
selected_images = random.sample(all_image_files, min(1000, len(all_image_files)))
print(len(selected_images))
for image_path in selected_images:
question = {
"question_id": question_id,
"image": image_path,
"text": prompt,
"category": "detail"
}
questions.append(question)
question_id += 1
# Write the list of questions to a JSONL file
with jsonlines.open(output_file, mode='a') as writer:
writer.write_all(questions)
# create_jsonl_file(IMG_PATHS, preset_prompt, output_file)
# create_jsonl_file_from_subdirs("/mnt/user/myang/OneDrive_1_9-6-2023/facial_components/images/test/", preset_prompt, output_file)
create_jsonl_file_from_dir("/fsx/ivan.laptev/Deepfake/dffd/ffhq/test", preset_prompt, output_file)
# create_jsonl_file_from_dir("/fsx/ivan.laptev/Deepfake/dffd/stargan/test", preset_prompt, output_file)
create_jsonl_file_from_dir("/fsx/ivan.laptev/Deepfake/dffd/stylegan_ffhq/test", preset_prompt, output_file)
# create_jsonl_file_from_dir("/fsx/ivan.laptev/Deepfake/dffdtestt/face2face", preset_prompt, output_file)