|
import os |
|
import pandas as pd |
|
from PIL import Image |
|
from io import BytesIO |
|
import io |
|
|
|
|
|
|
|
image_folder = "images" |
|
ocr_folder = "ocr-text" |
|
|
|
|
|
data = [] |
|
count = 0 |
|
|
|
|
|
for index, image_file in enumerate(os.listdir(image_folder)): |
|
if index >= 1000: |
|
break |
|
count += 1 |
|
print("adding image: " + image_file + " to dataframe" + " count: " + str(count)) |
|
image_path = os.path.join(image_folder, image_file) |
|
|
|
|
|
ocr_path = os.path.join(ocr_folder, image_file.replace(".jpg", "_words.txt")) |
|
|
|
|
|
image = Image.open(image_path) |
|
image_width, image_height = image.size |
|
|
|
|
|
with open(ocr_path, "r", encoding="utf-8") as ocr_file: |
|
caption = ocr_file.read().strip() |
|
|
|
|
|
img = Image.open(image_path) |
|
img_byte_arr = io.BytesIO() |
|
img.save(img_byte_arr, format=img.format) |
|
img_byte_arr = img_byte_arr.getvalue() |
|
|
|
|
|
data.append({ |
|
'image': img_byte_arr, |
|
'ocr_annotation_texts': caption, |
|
'image_height': image_height, |
|
'image_width': image_width |
|
}) |
|
|
|
|
|
df = pd.DataFrame(data) |
|
|
|
current_directory = os.getcwd() |
|
|
|
parquet_file_name = "my_dataframe.parquet" |
|
|
|
|
|
parquet_file_path = os.path.join(current_directory, parquet_file_name) |
|
df.to_parquet(parquet_file_path, index=False) |
|
|
|
|
|
print(df) |
|
|