Spaces:
Sleeping
Sleeping
File size: 2,143 Bytes
8e0293d 4fa50e8 8e0293d |
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 63 64 65 66 67 68 69 70 71 72 |
import json
import gradio as gr
def process_metadata(file):
# Read the uploaded JSON file
with open(file.name, "r") as f:
data = json.load(f)
results = []
# Process each media item
for item in data["media_items"]:
# Initialize lists for bboxes and categories
bboxes = []
categories = []
# Process each metadata item
for metadata in item["metadata_items"]:
if metadata["type"] == "object_label":
bbox = metadata["properties"]["bbox"]
bboxes.append(bbox)
category_mapping = {
"vest": 0,
# Add other categories as needed
}
category = category_mapping.get(
metadata["properties"]["category_name"], 0
)
categories.append(category)
# Create output format
output = {
"file_name": item["file_name"],
"objects": {"bbox": bboxes, "category": categories},
}
results.append(json.dumps(output))
# Return all results joined by newlines
return "\n".join(results)
def save_text(text):
# Create a temporary file to save the output
temp_file = "metadata.jsonl"
with open(temp_file, "w") as f:
f.write(text)
return temp_file # Return only the file path
# Create Gradio interface
with gr.Blocks(title="Metadata Converter") as iface:
gr.Markdown("# Metadata Converter")
gr.Markdown("Upload a JSON annotations file exported from Visual Layer.")
with gr.Row():
input_file = gr.File(label="Upload JSON metadata file")
with gr.Row():
output_text = gr.Textbox(label="Converted Output", lines=10)
download_btn = gr.DownloadButton(label="Download Output", interactive=True)
# Connect the components
input_file.change(fn=process_metadata, inputs=[input_file], outputs=[output_text])
# Connect download button directly to output text
download_btn.click(fn=save_text, inputs=[output_text], outputs=[download_btn])
if __name__ == "__main__":
iface.launch()
|