Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ 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'])
|
@@ -22,12 +23,28 @@ dimension_pattern = r"(\d+'\s?\d+\")|(\d+\.?\d*\s?[x×]\s?\d+\.?\d*)" # Pattern
|
|
22 |
|
23 |
# Function to extract room data with counts and measurements
|
24 |
def extract_room_data(results, room_patterns, dimension_pattern):
|
25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
# Function to process the uploaded image
|
28 |
def process_image(image):
|
29 |
# Convert the Gradio image to PIL Image
|
30 |
-
|
31 |
|
32 |
# Perform OCR
|
33 |
results = reader.readtext(np.array(image), detail=0)
|
@@ -40,7 +57,7 @@ def process_image(image):
|
|
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 |
|
@@ -54,4 +71,4 @@ iface = gr.Interface(
|
|
54 |
)
|
55 |
|
56 |
# Launch the interface
|
57 |
-
iface.launch(share=True) # Set share=True to get a shareable link
|
|
|
4 |
import cv2
|
5 |
import re
|
6 |
import gradio as gr
|
7 |
+
import numpy as np # Ensure numpy is imported
|
8 |
|
9 |
# Initialize the OCR reader
|
10 |
reader = easyocr.Reader(['en'])
|
|
|
23 |
|
24 |
# Function to extract room data with counts and measurements
|
25 |
def extract_room_data(results, room_patterns, dimension_pattern):
|
26 |
+
room_data = {}
|
27 |
+
|
28 |
+
for result in results:
|
29 |
+
text = result[1].lower() # Extract the text from the OCR result
|
30 |
+
for room_type, pattern in room_patterns.items():
|
31 |
+
if re.search(pattern, text):
|
32 |
+
# Check if room type is already in the dictionary
|
33 |
+
if room_type not in room_data:
|
34 |
+
room_data[room_type] = {"count": 0, "measurements": []}
|
35 |
+
room_data[room_type]["count"] += 1
|
36 |
+
|
37 |
+
# Find dimensions in the text
|
38 |
+
dimensions = re.findall(dimension_pattern, text)
|
39 |
+
if dimensions:
|
40 |
+
room_data[room_type]["measurements"].extend(dimensions)
|
41 |
+
|
42 |
+
return room_data
|
43 |
|
44 |
# Function to process the uploaded image
|
45 |
def process_image(image):
|
46 |
# Convert the Gradio image to PIL Image
|
47 |
+
image = Image.fromarray(image)
|
48 |
|
49 |
# Perform OCR
|
50 |
results = reader.readtext(np.array(image), detail=0)
|
|
|
57 |
for room_type, data in room_data_with_counts.items():
|
58 |
output_text += f"- {room_type.capitalize()}:\n"
|
59 |
output_text += f" Count: {data['count']}\n"
|
60 |
+
output_text += f" Measurements: {', '.join(data['measurements'])}\n"
|
61 |
|
62 |
return output_text
|
63 |
|
|
|
71 |
)
|
72 |
|
73 |
# Launch the interface
|
74 |
+
iface.launch(share=True) # Set share=True to get a shareable link
|