dnth's picture
Update app.py
4fa50e8 verified
raw
history blame
2.14 kB
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()