File size: 10,385 Bytes
90cd92a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
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)