utility scripts
Browse files- dataframe.py +61 -0
- json-to-annotation.py +39 -0
dataframe.py
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from PIL import Image
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
import io
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
# Path to the folders containing images and captions
|
| 9 |
+
image_folder = "images"
|
| 10 |
+
ocr_folder = "ocr-text"
|
| 11 |
+
|
| 12 |
+
# List to store data
|
| 13 |
+
data = []
|
| 14 |
+
count = 0
|
| 15 |
+
|
| 16 |
+
# Iterate through images and captions
|
| 17 |
+
for index, image_file in enumerate(os.listdir(image_folder)):
|
| 18 |
+
if index >= 1000:
|
| 19 |
+
break
|
| 20 |
+
count += 1
|
| 21 |
+
print("adding image: " + image_file + " to dataframe" + " count: " + str(count))
|
| 22 |
+
image_path = os.path.join(image_folder, image_file)
|
| 23 |
+
|
| 24 |
+
# Assuming caption file names match image file names
|
| 25 |
+
ocr_path = os.path.join(ocr_folder, image_file.replace(".jpg", "_words.txt"))
|
| 26 |
+
|
| 27 |
+
# Read image and get image dimensions
|
| 28 |
+
image = Image.open(image_path)
|
| 29 |
+
image_width, image_height = image.size
|
| 30 |
+
|
| 31 |
+
# Read caption
|
| 32 |
+
with open(ocr_path, "r", encoding="utf-8") as ocr_file:
|
| 33 |
+
caption = ocr_file.read().strip()
|
| 34 |
+
|
| 35 |
+
# Convert image to byte array
|
| 36 |
+
img = Image.open(image_path)
|
| 37 |
+
img_byte_arr = io.BytesIO()
|
| 38 |
+
img.save(img_byte_arr, format=img.format)
|
| 39 |
+
img_byte_arr = img_byte_arr.getvalue()
|
| 40 |
+
|
| 41 |
+
# Append data to the list
|
| 42 |
+
data.append({
|
| 43 |
+
'image': img_byte_arr,
|
| 44 |
+
'ocr_annotation_texts': caption,
|
| 45 |
+
'image_height': image_height,
|
| 46 |
+
'image_width': image_width
|
| 47 |
+
})
|
| 48 |
+
|
| 49 |
+
# Create DataFrame
|
| 50 |
+
df = pd.DataFrame(data)
|
| 51 |
+
|
| 52 |
+
current_directory = os.getcwd()
|
| 53 |
+
|
| 54 |
+
parquet_file_name = "my_dataframe.parquet"
|
| 55 |
+
|
| 56 |
+
# Combine the current directory and the filename to create the full path
|
| 57 |
+
parquet_file_path = os.path.join(current_directory, parquet_file_name)
|
| 58 |
+
df.to_parquet(parquet_file_path, index=False)
|
| 59 |
+
|
| 60 |
+
# Display the DataFrame
|
| 61 |
+
print(df)
|
json-to-annotation.py
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
|
| 4 |
+
input_folder = "words"
|
| 5 |
+
output_folder = "ocr-text"
|
| 6 |
+
|
| 7 |
+
json_files = [file for file in os.listdir(input_folder) if file.endswith('.json')]
|
| 8 |
+
|
| 9 |
+
for json_file in json_files:
|
| 10 |
+
print(f'Processing {json_file}')
|
| 11 |
+
input_file_path = os.path.join(input_folder, json_file)
|
| 12 |
+
output_file_path = os.path.join(output_folder, os.path.splitext(json_file)[0] + '.txt')
|
| 13 |
+
|
| 14 |
+
with open(input_file_path, 'r') as file:
|
| 15 |
+
data = json.load(file)
|
| 16 |
+
|
| 17 |
+
words_list = data.get("words", [])
|
| 18 |
+
|
| 19 |
+
#calculate width and height of the image
|
| 20 |
+
width = data["image_rect"][2]
|
| 21 |
+
height = data["image_rect"][3]
|
| 22 |
+
|
| 23 |
+
result_strings = []
|
| 24 |
+
for word in words_list:
|
| 25 |
+
bbox_values = word["bbox"]
|
| 26 |
+
|
| 27 |
+
#calculate (x,y,w,h)
|
| 28 |
+
x = int((bbox_values[0])*100/width)
|
| 29 |
+
y = int((bbox_values[1])*100/height)
|
| 30 |
+
box_width = int((abs(bbox_values[2]-bbox_values[0]))*100/width)
|
| 31 |
+
box_height = int((abs(bbox_values[3]-bbox_values[1]))*100/height)
|
| 32 |
+
#convert everything to a string with appropriate spacing and text at the end
|
| 33 |
+
result_string = f"{x} {y} {box_width} {box_height} {word['text']}"
|
| 34 |
+
result_strings.append(result_string)
|
| 35 |
+
|
| 36 |
+
result_string = ' '.join(result_strings)
|
| 37 |
+
|
| 38 |
+
with open(output_file_path, 'w', encoding='utf-8') as file:
|
| 39 |
+
file.write(result_string)
|