|
import os |
|
import pandas as pd |
|
from PIL import Image |
|
import codecs |
|
import numpy as np |
|
import glob |
|
import io |
|
|
|
def create_dataset(): |
|
print("Starting dataset creation...") |
|
|
|
|
|
os.makedirs("processed_images", exist_ok=True) |
|
|
|
|
|
print("Finding label files...") |
|
label_files = glob.glob("./original/test/round3?_best2019.label") |
|
print(f"Found {len(label_files)} label files") |
|
|
|
|
|
data = [] |
|
for i, label_path in enumerate(label_files): |
|
print(f"\nProcessing label file {i+1}/{len(label_files)}: {label_path}") |
|
|
|
|
|
print("Reading label file...") |
|
with codecs.open(label_path, 'r', encoding='cp874') as f: |
|
lines = f.readlines() |
|
print(f"Found {len(lines)} entries") |
|
|
|
for j, line in enumerate(lines): |
|
if j % 100 == 0: |
|
print(f"Processing entry {j}/{len(lines)}") |
|
|
|
filename, caption = line.strip().split(' ', 1) |
|
|
|
|
|
folder = filename[:3] |
|
image_path = f"./original/test/{folder}/{filename}" |
|
|
|
if os.path.exists(image_path): |
|
try: |
|
|
|
img = Image.open(image_path) |
|
img_byte_arr = io.BytesIO() |
|
img.save(img_byte_arr, format='PNG') |
|
img_bytes = {"bytes":bytearray(img_byte_arr.getvalue())} |
|
|
|
data.append({ |
|
'image': img_bytes, |
|
'text': caption, |
|
'label_file': os.path.basename(label_path) |
|
}) |
|
except Exception as e: |
|
print(f"Error processing image {image_path}: {e}") |
|
print(f"\nProcessed {len(data)} total images successfully") |
|
|
|
|
|
print("Converting to dataframe...") |
|
df = pd.DataFrame(data) |
|
print("Saving to parquet file...") |
|
df.to_parquet("train-0000.parquet", index=False) |
|
print("Dataset creation complete!") |
|
|
|
if __name__ == "__main__": |
|
create_dataset() |
|
|