File size: 6,193 Bytes
dd5f88d |
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 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Dependencies"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"pip install pillow datasets pandas pypng uuid\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Preproccessing"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import uuid\n",
"import shutil\n",
"\n",
"def rename_and_move_images(source_dir, target_dir):\n",
" # Create the target directory if it doesn't exist\n",
" os.makedirs(target_dir, exist_ok=True)\n",
"\n",
" # List of common image file extensions\n",
" image_extensions = ('.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff')\n",
"\n",
" # Walk through the source directory and its subdirectories\n",
" for root, dirs, files in os.walk(source_dir):\n",
" for file in files:\n",
" # Check if the file has an image extension\n",
" if file.lower().endswith(image_extensions):\n",
" # Generate a new filename with UUID\n",
" new_filename = str(uuid.uuid4()) + os.path.splitext(file)[1]\n",
" \n",
" # Construct full file paths\n",
" old_path = os.path.join(root, file)\n",
" new_path = os.path.join(target_dir, new_filename)\n",
" \n",
" # Move and rename the file\n",
" shutil.move(old_path, new_path)\n",
" print(f\"Moved and renamed: {old_path} -> {new_path}\")\n",
"\n",
"# Usage\n",
"source_directory = \"images\"\n",
"target_directory = \"train\"\n",
"\n",
"rename_and_move_images(source_directory, target_directory)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Extract the Metadata"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import json\n",
"import png\n",
"import pandas as pd\n",
"\n",
"# Directory containing images\n",
"image_dir = 'train'\n",
"metadata_list = []\n",
"\n",
"# Function to extract the JSON data from the tEXt chunk in PNG images\n",
"def extract_metadata_from_png(image_path):\n",
" with open(image_path, 'rb') as f:\n",
" reader = png.Reader(file=f)\n",
" chunks = reader.chunks()\n",
" for chunk_type, chunk_data in chunks:\n",
" if chunk_type == b'tEXt':\n",
" # Convert bytes to string\n",
" chunk_text = chunk_data.decode('latin1')\n",
" if 'prompt' in chunk_text:\n",
" try:\n",
" # Extract JSON string after \"prompt\\0\"\n",
" json_str = chunk_text.split('prompt\\0', 1)[1]\n",
" json_data = json.loads(json_str)\n",
" inputs = json_data.get('3', {}).get('inputs', {})\n",
" seed = inputs.get('seed', 'N/A')\n",
" positive_prompt = json_data.get('6', {}).get('inputs', {}).get('text', 'N/A')\n",
" negative_prompt = json_data.get('7', {}).get('inputs', {}).get('text', 'N/A')\n",
" model = json_data.get('4', {}).get('inputs', {}).get('ckpt_name', 'N/A')\n",
" steps = inputs.get('steps', 'N/A')\n",
" cfg = inputs.get('cfg', 'N/A')\n",
" sampler_name = inputs.get('sampler_name', 'N/A')\n",
" scheduler = inputs.get('scheduler', 'N/A')\n",
" denoise = inputs.get('denoise', 'N/A')\n",
" return {\n",
" 'seed': seed,\n",
" 'positive_prompt': positive_prompt,\n",
" 'negative_prompt': negative_prompt,\n",
" 'model': model,\n",
" 'steps': steps,\n",
" 'cfg': cfg,\n",
" 'sampler_name': sampler_name,\n",
" 'scheduler': scheduler,\n",
" 'denoise': denoise\n",
" }\n",
" except json.JSONDecodeError:\n",
" pass\n",
" return {}\n",
"\n",
"# Loop through all images in the directory\n",
"for file_name in os.listdir(image_dir):\n",
" if file_name.endswith('.png'):\n",
" image_path = os.path.join(image_dir, file_name)\n",
" metadata = extract_metadata_from_png(image_path)\n",
" metadata['file_name'] = file_name\n",
" metadata_list.append(metadata)\n",
"\n",
"# Convert metadata to DataFrame\n",
"metadata_df = pd.DataFrame(metadata_list)\n",
"\n",
"# Ensure 'file_name' is the first column\n",
"columns_order = ['file_name', 'seed', 'positive_prompt', 'negative_prompt', 'model', 'steps', 'cfg', 'sampler_name', 'scheduler', 'denoise']\n",
"metadata_df = metadata_df[columns_order]\n",
"\n",
"# Save metadata to a CSV file\n",
"metadata_csv_path = 'train/metadata.csv'\n",
"metadata_df.to_csv(metadata_csv_path, index=False)\n",
"\n",
"print(\"Metadata extraction complete. Metadata saved to:\", metadata_csv_path)\n",
"\n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.14"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|