File size: 2,693 Bytes
0c8d55e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
from datasets import Dataset, load_dataset
import math, os

# Dataset info structure:
# - task_type: string - Type of the task
# - key: string - Unique identifier for the sample
# - instruction: string - Task instruction/prompt
# - instruction_language: string - Language of the instruction
# - input_image: Image - Original input image
# - input_image_raw: Image - Raw/unprocessed input image
# - Intersection_exist: bool - Whether intersection exists

def calculate_dimensions(target_area, ratio):
    width = math.sqrt(target_area * ratio)
    height = width / ratio
    
    width = round(width / 32) * 32
    height = round(height / 32) * 32

    new_area = width * height
    if new_area < target_area:
        width += 32
        new_area = width * height
    elif new_area > target_area:
        width -= 32
        new_area = width * height
    
    return width, height, new_area

# Load dataset
dataset = load_dataset("stepfun-ai/GEdit-Bench")
save_path = "/path/to/save/directory"

# Dictionary to store instruction and image paths
instruction_image_paths = {}

for item in dataset['train']:

    task_type = item['task_type']
    key = item['key']
    instruction = item['instruction']
    instruction_language = item['instruction_language']
    input_image = item['input_image']
    input_image_raw = item['input_image_raw']
    intersection_exist = item['Intersection_exist']

    target_width, target_height, new_area = calculate_dimensions(512 * 512, input_image_raw.width / input_image_raw.height)
    resize_input_image = input_image_raw.resize((target_width, target_height))

    

    save_path_fullset_source_image = f"{save_path}/fullset/{task_type}/{instruction_language}/{key}_SRCIMG.png"
    save_path_fullset = f"{save_path}/fullset/{task_type}/{instruction_language}/{key}.png"
    
    relative_path = f"fullset/{task_type}/{instruction_language}/{key}.png"

    # Create directories if they don't exist
    os.makedirs(os.path.dirname(save_path_fullset_source_image), exist_ok=True)
    os.makedirs(os.path.dirname(save_path_fullset), exist_ok=True)

    # Save the images
    input_image.save(save_path_fullset_source_image)
    resize_input_image.save(save_path_fullset)

    # Store instruction and corresponding image path in the dictionary
    instruction_image_paths[key] = {
        'prompt': instruction,
        'id': relative_path,
        'edit_type':  task_type,
    }

# Save the dictionary to a JSON file
json_file_path = "/path/to/save/instruction_image_paths.json"
with open(json_file_path, 'w') as json_file:
    json.dump(instruction_image_paths, json_file, indent=4)

print(f"Instruction and image paths saved to {json_file_path}")