File size: 1,647 Bytes
6e90ebb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import pandas as pd
from PIL import Image
from io import BytesIO
import io


# Path to the folders containing images and captions
image_folder = "images"
ocr_folder = "ocr-text"

# List to store data
data = []
count = 0

# Iterate through images and captions
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)
    
    # Assuming caption file names match image file names
    ocr_path = os.path.join(ocr_folder, image_file.replace(".jpg", "_words.txt"))

    # Read image and get image dimensions
    image = Image.open(image_path)
    image_width, image_height = image.size

    # Read caption
    with open(ocr_path, "r", encoding="utf-8") as ocr_file:
        caption = ocr_file.read().strip()

    # Convert image to byte array
    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()

    # Append data to the list
    data.append({
        'image': img_byte_arr,
        'ocr_annotation_texts': caption,
        'image_height': image_height,
        'image_width': image_width
    })

# Create DataFrame
df = pd.DataFrame(data)

current_directory = os.getcwd()

parquet_file_name = "my_dataframe.parquet"

# Combine the current directory and the filename to create the full path
parquet_file_path = os.path.join(current_directory, parquet_file_name)
df.to_parquet(parquet_file_path, index=False)

# Display the DataFrame
print(df)