Dharini Baskaran commited on
Commit
aca5861
·
1 Parent(s): ce26e7b
Files changed (1) hide show
  1. app.py +29 -11
app.py CHANGED
@@ -22,9 +22,10 @@ UPLOAD_DIR = "/tmp/uploads/"
22
  JSON_DIR = "/tmp/results/"
23
  OUTPUT_DIR = "/tmp/output/"
24
  MODEL_DIR = os.path.join(BASE_DIR, "rcnn_model", "scripts")
25
- logo_path = os.path.join(BASE_DIR, "public", "logo.png") # Your Inovonics image
26
  model_path = os.path.join(OUTPUT_DIR, "model_final.pth")
27
 
 
28
  GOOGLE_DRIVE_FILE_ID = "1yr64AOgaYZPTcQzG6cxG6lWBENHR9qjW"
29
  GDRIVE_URL = f"https://drive.google.com/uc?id={GOOGLE_DRIVE_FILE_ID}"
30
 
@@ -55,55 +56,72 @@ cfg = write_config()
55
  def predict(uploaded_file_path):
56
  print("Inside Predict:" + uploaded_file_path)
57
  if uploaded_file_path is None:
58
- return None, None, None, "No file uploaded."
59
 
 
60
  uploaded_path = os.path.join(UPLOAD_DIR, "input_image.png")
61
  shutil.copy(uploaded_file_path, uploaded_path)
62
 
63
  input_filename = "input_image.png"
64
 
65
- output_json_name = input_filename.replace(".png", "_result.json")
66
- output_image_name = input_filename.replace(".png", "_result.png")
67
 
68
  output_json_path = os.path.join(JSON_DIR, output_json_name)
69
  output_image_path = os.path.join(JSON_DIR, output_image_name)
70
 
71
- main(cfg, uploaded_file_path, output_json_name, output_image_name)
 
72
 
 
73
  result_img = Image.open(output_image_path) if os.path.exists(output_image_path) else None
74
  result_json = {}
75
  if os.path.exists(output_json_path):
76
  with open(output_json_path, "r") as jf:
77
  result_json = json.load(jf)
78
 
79
- return uploaded_path, result_img, json.dumps(result_json, indent=2), output_json_path
 
 
 
 
 
80
 
81
  # ==================================
82
  # GRADIO UI
83
  # ==================================
84
 
85
  with gr.Blocks() as demo:
 
86
  with gr.Row():
87
- gr.Image(logo_path, height=80, width=80)
88
- gr.Markdown("<h1 style='text-align: center;'> Inovonics 2D Floorplan Vectorizer</h1>")
 
 
 
 
 
 
 
89
 
90
  with gr.Row():
91
  with gr.Column():
92
  uploaded_file = gr.File(label="Upload your Floorplan Image", type="filepath")
93
- uploaded_img_preview = gr.Image(label="📤 Uploaded Image", interactive=False)
94
  run_button = gr.Button("Run Vectorizer 🔥")
95
 
96
  with gr.Column():
97
  output_image = gr.Image(label="🖼 Output Vectorized Image")
98
  output_json = gr.JSON(label="🧾 Output JSON")
99
- download_button = gr.File(label="⬇️ Download JSON")
100
 
101
  error_output = gr.Textbox(label="Error Message", visible=False)
102
 
 
103
  run_button.click(
104
  predict,
105
  inputs=[uploaded_file],
106
- outputs=[uploaded_img_preview, output_image, output_json, download_button]
107
  )
108
 
109
  demo.launch(server_name="0.0.0.0", server_port=7860, share=True)
 
22
  JSON_DIR = "/tmp/results/"
23
  OUTPUT_DIR = "/tmp/output/"
24
  MODEL_DIR = os.path.join(BASE_DIR, "rcnn_model", "scripts")
25
+ logo_path = os.path.join(BASE_DIR, "public", "logo.png")
26
  model_path = os.path.join(OUTPUT_DIR, "model_final.pth")
27
 
28
+ # Google Drive model
29
  GOOGLE_DRIVE_FILE_ID = "1yr64AOgaYZPTcQzG6cxG6lWBENHR9qjW"
30
  GDRIVE_URL = f"https://drive.google.com/uc?id={GOOGLE_DRIVE_FILE_ID}"
31
 
 
56
  def predict(uploaded_file_path):
57
  print("Inside Predict:" + uploaded_file_path)
58
  if uploaded_file_path is None:
59
+ return None, None, "No file uploaded.", None
60
 
61
+ # Save uploaded file to temp
62
  uploaded_path = os.path.join(UPLOAD_DIR, "input_image.png")
63
  shutil.copy(uploaded_file_path, uploaded_path)
64
 
65
  input_filename = "input_image.png"
66
 
67
+ output_json_name = input_filename.replace(".png", "_result.json").replace(".jpg", "_result.json").replace(".jpeg", "_result.json")
68
+ output_image_name = input_filename.replace(".png", "_result.png").replace(".jpg", "_result.png").replace(".jpeg", "_result.png")
69
 
70
  output_json_path = os.path.join(JSON_DIR, output_json_name)
71
  output_image_path = os.path.join(JSON_DIR, output_image_name)
72
 
73
+ # Run model
74
+ main(cfg, uploaded_path, output_json_name, output_image_name)
75
 
76
+ # Read outputs
77
  result_img = Image.open(output_image_path) if os.path.exists(output_image_path) else None
78
  result_json = {}
79
  if os.path.exists(output_json_path):
80
  with open(output_json_path, "r") as jf:
81
  result_json = json.load(jf)
82
 
83
+ # Save JSON to file for download
84
+ download_json_path = os.path.join(JSON_DIR, "output_for_download.json")
85
+ with open(download_json_path, "w") as f:
86
+ json.dump(result_json, f, indent=2)
87
+
88
+ return result_img, json.dumps(result_json, indent=2), None, download_json_path, uploaded_path
89
 
90
  # ==================================
91
  # GRADIO UI
92
  # ==================================
93
 
94
  with gr.Blocks() as demo:
95
+ # Header
96
  with gr.Row():
97
+ gr.Markdown(
98
+ """
99
+ <div style='display: flex; align-items: center; justify-content: center;'>
100
+ <img src='file/public/logo.png' style='height: 50px; margin-right: 10px;'/>
101
+ <h1>Inovonics 2D Floorplan Vectorizer</h1>
102
+ </div>
103
+ """,
104
+ unsafe_allow_html=True,
105
+ )
106
 
107
  with gr.Row():
108
  with gr.Column():
109
  uploaded_file = gr.File(label="Upload your Floorplan Image", type="filepath")
110
+ uploaded_image_display = gr.Image(label="Uploaded Image", visible=True)
111
  run_button = gr.Button("Run Vectorizer 🔥")
112
 
113
  with gr.Column():
114
  output_image = gr.Image(label="🖼 Output Vectorized Image")
115
  output_json = gr.JSON(label="🧾 Output JSON")
116
+ download_button = gr.File(label="⬇️ Download JSON", visible=True)
117
 
118
  error_output = gr.Textbox(label="Error Message", visible=False)
119
 
120
+ # Logic binding
121
  run_button.click(
122
  predict,
123
  inputs=[uploaded_file],
124
+ outputs=[output_image, output_json, error_output, download_button, uploaded_image_display]
125
  )
126
 
127
  demo.launch(server_name="0.0.0.0", server_port=7860, share=True)