Spaces:
Paused
Paused
throaway2854
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -93,6 +93,25 @@ class DatasetBuilder:
|
|
93 |
with open(dataset_file, 'w') as f:
|
94 |
json.dump(self.dataset, f)
|
95 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
96 |
def create_downloadable_dataset(self):
|
97 |
if not self.dataset:
|
98 |
return None, "Dataset is empty. Add some images first."
|
@@ -232,6 +251,15 @@ def download_dataset(dataset_name):
|
|
232 |
zip_path, message = builder.create_downloadable_dataset()
|
233 |
return zip_path, message
|
234 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
235 |
# Create Gradio interface
|
236 |
with gr.Blocks(theme="huggingface") as iface:
|
237 |
gr.Markdown("# Image Dataset Builder")
|
@@ -276,5 +304,24 @@ with gr.Blocks(theme="huggingface") as iface:
|
|
276 |
outputs=[download_output, download_message]
|
277 |
)
|
278 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
279 |
# Launch the interface
|
280 |
iface.launch()
|
|
|
93 |
with open(dataset_file, 'w') as f:
|
94 |
json.dump(self.dataset, f)
|
95 |
|
96 |
+
def resize_images(self, min_size=512, max_size=768):
|
97 |
+
for item in self.dataset:
|
98 |
+
image_path = os.path.join(IMAGES_DIR, item['image'])
|
99 |
+
image = Image.open(image_path)
|
100 |
+
|
101 |
+
# Resize the image while maintaining the aspect ratio
|
102 |
+
image.thumbnail((max_size, max_size), resample=Image.BICUBIC)
|
103 |
+
|
104 |
+
# Save the resized image
|
105 |
+
image.save(image_path)
|
106 |
+
|
107 |
+
def resize_dataset(self):
|
108 |
+
resized_dataset_name = f"{self.dataset_name} (resized)"
|
109 |
+
resized_dataset_builder = DatasetBuilder(resized_dataset_name)
|
110 |
+
resized_dataset_builder.dataset = self.dataset
|
111 |
+
resized_dataset_builder.resize_images()
|
112 |
+
resized_dataset_builder.save_dataset()
|
113 |
+
return f"Resized dataset '{self.dataset_name}' to '{resized_dataset_name}'."
|
114 |
+
|
115 |
def create_downloadable_dataset(self):
|
116 |
if not self.dataset:
|
117 |
return None, "Dataset is empty. Add some images first."
|
|
|
251 |
zip_path, message = builder.create_downloadable_dataset()
|
252 |
return zip_path, message
|
253 |
|
254 |
+
def resize_dataset(dataset_name):
|
255 |
+
builder = DatasetBuilder(dataset_name)
|
256 |
+
return builder.resize_dataset()
|
257 |
+
|
258 |
+
def download_resized_dataset(dataset_name):
|
259 |
+
builder = DatasetBuilder(f"{dataset_name} (resized)")
|
260 |
+
zip_path, message = builder.create_downloadable_dataset()
|
261 |
+
return zip_path, message
|
262 |
+
|
263 |
# Create Gradio interface
|
264 |
with gr.Blocks(theme="huggingface") as iface:
|
265 |
gr.Markdown("# Image Dataset Builder")
|
|
|
304 |
outputs=[download_output, download_message]
|
305 |
)
|
306 |
|
307 |
+
gr.Markdown("## Resize Dataset")
|
308 |
+
resize_button = gr.Button("Resize Dataset")
|
309 |
+
resize_result = gr.Textbox(label="Resize Result")
|
310 |
+
resize_button.click(
|
311 |
+
resize_dataset,
|
312 |
+
inputs=[dataset_name_input],
|
313 |
+
outputs=resize_result
|
314 |
+
)
|
315 |
+
|
316 |
+
gr.Markdown("## Download Resized Dataset")
|
317 |
+
download_resized_button = gr.Button("Download Resized Dataset")
|
318 |
+
download_resized_output = gr.File(label="Download Resized")
|
319 |
+
download_resized_message = gr.Textbox(label="Resized Download Status")
|
320 |
+
download_resized_button.click(
|
321 |
+
download_resized_dataset,
|
322 |
+
inputs=[dataset_name_input],
|
323 |
+
outputs=[download_resized_output, download_resized_message]
|
324 |
+
)
|
325 |
+
|
326 |
# Launch the interface
|
327 |
iface.launch()
|