Spaces:
Sleeping
Sleeping
Create data_mapping.py
Browse files- data_mapping.py +54 -0
data_mapping.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import uuid
|
| 3 |
+
|
| 4 |
+
def generate_unique_id():
|
| 5 |
+
"""Generates a unique ID for each object."""
|
| 6 |
+
return str(uuid.uuid4())
|
| 7 |
+
|
| 8 |
+
def map_object_data(object_id, description, extracted_text, summary):
|
| 9 |
+
"""
|
| 10 |
+
Maps data to a single object.
|
| 11 |
+
|
| 12 |
+
Args:
|
| 13 |
+
object_id (str): Unique ID of the object.
|
| 14 |
+
description (str): Description of the object.
|
| 15 |
+
extracted_text (list): List of extracted text items.
|
| 16 |
+
summary (str): Summary of the object's attributes.
|
| 17 |
+
|
| 18 |
+
Returns:
|
| 19 |
+
dict: A dictionary containing mapped data for the object.
|
| 20 |
+
"""
|
| 21 |
+
return {
|
| 22 |
+
"object_id": object_id,
|
| 23 |
+
"description": description,
|
| 24 |
+
"extracted_text": extracted_text,
|
| 25 |
+
"summary": summary
|
| 26 |
+
}
|
| 27 |
+
|
| 28 |
+
def create_summary_table(objects_data):
|
| 29 |
+
"""
|
| 30 |
+
Creates a data structure to store all objects' data along with the master image.
|
| 31 |
+
|
| 32 |
+
Args:
|
| 33 |
+
objects_data (list): List of dictionaries containing data for each object.
|
| 34 |
+
|
| 35 |
+
Returns:
|
| 36 |
+
dict: A dictionary containing the complete mapping of the master image and its objects.
|
| 37 |
+
"""
|
| 38 |
+
master_id = generate_unique_id()
|
| 39 |
+
data_mapping = {
|
| 40 |
+
"master_image_id": master_id,
|
| 41 |
+
"objects": objects_data
|
| 42 |
+
}
|
| 43 |
+
return data_mapping
|
| 44 |
+
|
| 45 |
+
def save_mapping_to_json(data_mapping, output_path):
|
| 46 |
+
"""
|
| 47 |
+
Saves the data mapping to a JSON file.
|
| 48 |
+
|
| 49 |
+
Args:
|
| 50 |
+
data_mapping (dict): The complete data mapping structure.
|
| 51 |
+
output_path (str): File path to save the JSON file.
|
| 52 |
+
"""
|
| 53 |
+
with open(output_path, 'w') as json_file:
|
| 54 |
+
json.dump(data_mapping, json_file, indent=4)
|