File size: 5,248 Bytes
873ac58
1d64201
 
873ac58
d1af37b
1d64201
873ac58
 
30bfac2
1d64201
d1af37b
 
 
1d64201
873ac58
1d64201
 
 
873ac58
d1af37b
 
 
873ac58
1d64201
 
 
873ac58
1d64201
 
 
873ac58
1d64201
 
 
 
873ac58
1d64201
873ac58
1d64201
63642bb
 
d1af37b
1d64201
d1af37b
 
873ac58
1d64201
63642bb
873ac58
 
1d64201
d1af37b
873ac58
d1af37b
 
2f7fddd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
873ac58
74cd29b
2f7fddd
 
 
74cd29b
 
873ac58
 
 
d1af37b
873ac58
 
d1af37b
873ac58
 
1d64201
873ac58
 
 
 
 
 
 
 
1d64201
d1af37b
873ac58
d1af37b
 
873ac58
 
1d64201
873ac58
 
74cd29b
 
873ac58
 
 
 
 
 
 
 
 
 
 
 
 
d1af37b
47e55ae
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import gradio as gr
import os
import sys
import json
import shutil
import gdown
import time
from PIL import Image
from io import BytesIO

# ==================================
# SETUP
# ==================================

print("๐Ÿš€ Gradio App Starting...")

BASE_DIR = os.path.dirname(os.path.abspath(__file__))

# Paths
UPLOAD_DIR = "/tmp/uploads/"
JSON_DIR = "/tmp/results/"
OUTPUT_DIR = "/tmp/output/"
MODEL_DIR = os.path.join(BASE_DIR, "rcnn_model", "scripts")
logo_path = os.path.join(BASE_DIR, "public", "logo.png")
model_path = os.path.join(OUTPUT_DIR, "model_final.pth")

# Google Drive model
GOOGLE_DRIVE_FILE_ID = "1yr64AOgaYZPTcQzG6cxG6lWBENHR9qjW"
GDRIVE_URL = f"https://drive.google.com/uc?id={GOOGLE_DRIVE_FILE_ID}"

# Create folders
os.makedirs(UPLOAD_DIR, exist_ok=True)
os.makedirs(JSON_DIR, exist_ok=True)
os.makedirs(OUTPUT_DIR, exist_ok=True)

# Download model if missing
if not os.path.exists(model_path):
    print("๐Ÿš€ Model file not found! Downloading...")
    try:
        # gdown.download(GDRIVE_URL, model_path, quiet=False)
        gdown.download(GDRIVE_URL, model_path, quiet=False, use_cookies=False)
        print("โœ… Model downloaded successfully.")
    except Exception as e:
        print(f"โŒ Failed to download model: {e}")

# Import model
sys.path.append(MODEL_DIR)
from rcnn_model.scripts.rcnn_run import main, write_config

cfg = write_config()

# ==================================
# MAIN PREDICTION FUNCTION
# ==================================

# def predict(uploaded_file):
#     if uploaded_file is None:
#         return None, None, "No file uploaded."
#     # uploaded_path = uploaded_file
#     # input_filename = os.path.basename(uploaded_path)
#     # print(f"โœ… Image received at {uploaded_path}")

#     uploaded_path = os.path.join(UPLOAD_DIR, uploaded_file.name)
#     with open(uploaded_path, "wb") as f:
#         f.write(uploaded_file.read())
#     print(f"โœ… Image saved to {uploaded_path}")


#     # Save uploaded image
#     # input_bytes = uploaded_file.read()
#     # img = Image.open(BytesIO(input_bytes)).convert("RGB")
#     # input_filename = uploaded_file.name
#     # uploaded_path = os.path.join(UPLOAD_DIR, input_filename)
#     # img.save(uploaded_path)
#     # print(f"โœ… Image saved to {uploaded_path}")

#     # Prepare output paths
#     input_filename = uploaded_file.name
#     output_json_name = input_filename.replace(".png", "_result.json").replace(".jpg", "_result.json").replace(".jpeg", "_result.json")
#     output_image_name = input_filename.replace(".png", "_result.png").replace(".jpg", "_result.png").replace(".jpeg", "_result.png")

#     output_json_path = os.path.join(JSON_DIR, output_json_name)
#     output_image_path = os.path.join(JSON_DIR, output_image_name)

#     # Run model
#     main(cfg, uploaded_path, output_json_path, output_image_path)

#     # Read outputs
#     result_img = Image.open(output_image_path) if os.path.exists(output_image_path) else None
#     result_json = {}
#     if os.path.exists(output_json_path):
#         with open(output_json_path, "r") as jf:
#             result_json = json.load(jf)

#     return result_img, json.dumps(result_json, indent=2), None

def predict(uploaded_file_path):
    if uploaded_file_path is None:
        return None, None, "No file uploaded."

    input_filename = os.path.basename(uploaded_file_path)
    uploaded_path = os.path.join(UPLOAD_DIR, input_filename)
    shutil.copy(uploaded_file_path, uploaded_path)
    print(f"โœ… Image saved to {uploaded_path}")

    # Prepare output paths
    output_json_name = input_filename.replace(".png", "_result.json").replace(".jpg", "_result.json").replace(".jpeg", "_result.json")
    output_image_name = input_filename.replace(".png", "_result.png").replace(".jpg", "_result.png").replace(".jpeg", "_result.png")

    output_json_path = os.path.join(JSON_DIR, output_json_name)
    output_image_path = os.path.join(JSON_DIR, output_image_name)

    # Run model
    main(cfg, uploaded_path, output_json_path, output_image_path)

    # Read outputs
    result_img = Image.open(output_image_path) if os.path.exists(output_image_path) else None
    result_json = {}
    if os.path.exists(output_json_path):
        with open(output_json_path, "r") as jf:
            result_json = json.load(jf)

    return result_img, json.dumps(result_json, indent=2), None

# ==================================
# GRADIO UI
# ==================================

with gr.Blocks() as demo:
    gr.Markdown("<h1 style='text-align: center;'>๐Ÿ  Inovonics 2D Floorplan Vectorizer</h1>")

    with gr.Row():
        with gr.Column():
            # uploaded_file = gr.File(label="Upload your Floorplan Image", type="filepath")
            uploaded_file = gr.File(label="Upload your Floorplan Image", type="file")
            run_button = gr.Button("Run Vectorizer ๐Ÿ”ฅ")

        with gr.Column():
            output_image = gr.Image(label="๐Ÿ–ผ Output Vectorized Image")
            output_json = gr.JSON(label="๐Ÿงพ Output JSON")

    error_output = gr.Textbox(label="Error Message", visible=False)

    run_button.click(
        predict,
        inputs=[uploaded_file],
        outputs=[output_image, output_json, error_output]
    )

demo.launch(share=True)