dnth commited on
Commit
8e0293d
·
verified ·
1 Parent(s): 608709a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+
3
+ import gradio as gr
4
+
5
+
6
+ def process_metadata(file):
7
+ # Read the uploaded JSON file
8
+ with open(file.name, "r") as f:
9
+ data = json.load(f)
10
+
11
+ results = []
12
+ # Process each media item
13
+ for item in data["media_items"]:
14
+ # Initialize lists for bboxes and categories
15
+ bboxes = []
16
+ categories = []
17
+
18
+ # Process each metadata item
19
+ for metadata in item["metadata_items"]:
20
+ if metadata["type"] == "object_label":
21
+ bbox = metadata["properties"]["bbox"]
22
+ bboxes.append(bbox)
23
+
24
+ category_mapping = {
25
+ "vest": 0,
26
+ # Add other categories as needed
27
+ }
28
+ category = category_mapping.get(
29
+ metadata["properties"]["category_name"], 0
30
+ )
31
+ categories.append(category)
32
+
33
+ # Create output format
34
+ output = {
35
+ "file_name": item["file_name"],
36
+ "objects": {"bbox": bboxes, "category": categories},
37
+ }
38
+ results.append(json.dumps(output))
39
+
40
+ # Return all results joined by newlines
41
+ return "\n".join(results)
42
+
43
+
44
+ def save_text(text):
45
+ # Create a temporary file to save the output
46
+ temp_file = "metadata.jsonl"
47
+ with open(temp_file, "w") as f:
48
+ f.write(text)
49
+ return temp_file # Return only the file path
50
+
51
+
52
+ # Create Gradio interface
53
+ with gr.Blocks(title="Metadata Converter") as iface:
54
+ gr.Markdown("# Metadata Converter")
55
+ gr.Markdown("Upload a JSON metadata file to convert it to the desired format.")
56
+
57
+ with gr.Row():
58
+ input_file = gr.File(label="Upload JSON metadata file")
59
+
60
+ with gr.Row():
61
+ output_text = gr.Textbox(label="Converted Output", lines=10)
62
+ download_btn = gr.DownloadButton(label="Download Output", interactive=True)
63
+
64
+ # Connect the components
65
+ input_file.change(fn=process_metadata, inputs=[input_file], outputs=[output_text])
66
+
67
+ # Connect download button directly to output text
68
+ download_btn.click(fn=save_text, inputs=[output_text], outputs=[download_btn])
69
+
70
+ if __name__ == "__main__":
71
+ iface.launch()