Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import easyocr
|
3 |
+
from PIL import Image
|
4 |
+
import cv2
|
5 |
+
import re
|
6 |
+
import gradio as gr
|
7 |
+
|
8 |
+
# Initialize the OCR reader
|
9 |
+
reader = easyocr.Reader(['en'])
|
10 |
+
|
11 |
+
# Define patterns for different room types and dimensions
|
12 |
+
room_patterns = {
|
13 |
+
'bedroom': r'bedroom|bed\s?rm',
|
14 |
+
'bathroom': r'bathroom|bath\s?rm',
|
15 |
+
'kitchen': r'kitchen',
|
16 |
+
'living room': r'living\s?room|sitting\s?room',
|
17 |
+
'dining room': r'dining\s?room',
|
18 |
+
# Add more patterns as needed
|
19 |
+
}
|
20 |
+
|
21 |
+
dimension_pattern = r"(\d+'\s?\d+\")|(\d+\.?\d*\s?[x×]\s?\d+\.?\d*)" # Pattern to match dimensions like 10'6" or 10x12
|
22 |
+
|
23 |
+
# Function to extract room data with counts and measurements
|
24 |
+
def extract_room_data(results, room_patterns, dimension_pattern):
|
25 |
+
# ... (same as before) ...
|
26 |
+
|
27 |
+
# Function to process the uploaded image
|
28 |
+
def process_image(image):
|
29 |
+
# Convert the Gradio image to PIL Image
|
30 |
+
image = Image.fromarray(image)
|
31 |
+
|
32 |
+
# Perform OCR
|
33 |
+
results = reader.readtext(np.array(image), detail=0)
|
34 |
+
|
35 |
+
# Extract room information
|
36 |
+
room_data_with_counts = extract_room_data(results, room_patterns, dimension_pattern)
|
37 |
+
|
38 |
+
# Format output for display
|
39 |
+
output_text = "Extracted Room Data with Counts and Measurements:\n"
|
40 |
+
for room_type, data in room_data_with_counts.items():
|
41 |
+
output_text += f"- {room_type.capitalize()}:\n"
|
42 |
+
output_text += f" Count: {data['count']}\n"
|
43 |
+
output_text += f" Measurements: {data['measurements']}\n"
|
44 |
+
|
45 |
+
return output_text
|
46 |
+
|
47 |
+
# Create the Gradio interface
|
48 |
+
iface = gr.Interface(
|
49 |
+
fn=process_image,
|
50 |
+
inputs=gr.Image(type="pil"), # Use type="pil" for PIL Image input
|
51 |
+
outputs="text",
|
52 |
+
title="Floor Plan Room Detection",
|
53 |
+
description="Upload a floor plan image to extract room information."
|
54 |
+
)
|
55 |
+
|
56 |
+
# Launch the interface
|
57 |
+
iface.launch(share=True) # Set share=True to get a shareable link
|