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()