Create preprocess.py
Browse files- preprocess.py +38 -0
preprocess.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from transformers import AutoProcessor, BlipForConditionalGeneration
|
| 4 |
+
|
| 5 |
+
def process_dataset(zip_path, output_dir, generate_captions=True):
|
| 6 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 7 |
+
|
| 8 |
+
# Descompacta dataset
|
| 9 |
+
import zipfile
|
| 10 |
+
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
| 11 |
+
zip_ref.extractall(output_dir)
|
| 12 |
+
|
| 13 |
+
# Gera captions com BLIP multilíngue
|
| 14 |
+
if generate_captions:
|
| 15 |
+
processor = AutoProcessor.from_pretrained("microsoft/blip-image-captioning-base")
|
| 16 |
+
model = BlipForConditionalGeneration.from_pretrained("microsoft/blip-image-captioning-base")
|
| 17 |
+
|
| 18 |
+
for img_name in os.listdir(output_dir):
|
| 19 |
+
if img_name.lower().endswith(('.png', '.jpg', '.jpeg')):
|
| 20 |
+
img_path = os.path.join(output_dir, img_name)
|
| 21 |
+
image = Image.open(img_path).convert('RGB')
|
| 22 |
+
|
| 23 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 24 |
+
outputs = model.generate(**inputs, max_new_tokens=50)
|
| 25 |
+
caption = processor.decode(outputs[0], skip_special_tokens=True)
|
| 26 |
+
|
| 27 |
+
with open(img_path.replace('.jpg', '.txt').replace('.png', '.txt'), 'w') as f:
|
| 28 |
+
f.write(caption)
|
| 29 |
+
|
| 30 |
+
# Redimensiona imagens
|
| 31 |
+
for img_name in os.listdir(output_dir):
|
| 32 |
+
if img_name.lower().endswith(('.png', '.jpg', '.jpeg')):
|
| 33 |
+
img_path = os.path.join(output_dir, img_name)
|
| 34 |
+
image = Image.open(img_path).convert('RGB')
|
| 35 |
+
image = image.resize((512, 512), Image.LANCZOS)
|
| 36 |
+
image.save(img_path)
|
| 37 |
+
|
| 38 |
+
return output_dir
|