File size: 2,650 Bytes
f9ecaae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import cv2
import tempfile

from utils.preprocessor import (
    TextTargetPreprocessor,
    DoorTargetPreprocessor
)

from utils.extractor import (
    TextDetector,
    DoorDetector
)


# LOAD COMPONENTS

text_preprocessor = TextTargetPreprocessor()
door_preprocessor = DoorTargetPreprocessor()
text_detector = TextDetector(gpu=False)
door_detector = DoorDetector()


# MAIN PIPELINE

def process_floorplan(image, operation, query):

    # save uploaded image temporarily
    temp_path = tempfile.NamedTemporaryFile(
        suffix=".png",
        delete=False
    ).name

    cv2.imwrite(
        temp_path,
        cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
    )


    # DOOR DETECTION

    if operation == "Count Doors":

        processed = door_preprocessor.run(temp_path)

        processed_path = tempfile.NamedTemporaryFile(
            suffix=".png",
            delete=False
        ).name

        cv2.imwrite(processed_path, processed)

        result = door_detector.predict(
            image_path=processed_path,
            confidence_threshold=0.5
        )

        final_img = result["output_image"]

        final_img = cv2.cvtColor(
            final_img,
            cv2.COLOR_BGR2RGB
        )

        return (
            final_img,
            f"Total Doors Detected: {result['door_count']}"
        )


    # TEXT SEARCH

    elif operation == "Search Text":

        processed = text_preprocessor.run(temp_path)

        result_img = text_detector.extract_and_search(
            processed,
            query
        )

        result_img = cv2.cvtColor(
            result_img,
            cv2.COLOR_BGR2RGB
        )

        return (
            result_img,
            f"Searched Text: {query}"
        )

# GRADIO UI

demo = gr.Interface(

    fn=process_floorplan,

    inputs=[

        gr.Image(
            type="numpy",
            label="Upload Floorplan"
        ),

        gr.Radio(
            ["Count Doors", "Search Text"],
            label="Select Operation"
        ),

        gr.Textbox(
            label="Text to Search",
            placeholder="Example: Lounge"
        )

    ],

    outputs=[

        gr.Image(
            type="numpy",
            label="Output Image"
        ),

        gr.Textbox(
            label="Result"
        )

    ],

    title="Floorplan Analyzer",

    description="""

    Upload a floorplan image and either:

    - count door symbols

    - search for architectural text labels

    """

)

demo.launch()