Spaces:
Sleeping
Sleeping
Commit
·
1de1417
1
Parent(s):
f12deda
Update app.py
Browse files
app.py
CHANGED
@@ -7,7 +7,6 @@ import cv2
|
|
7 |
import numpy as np
|
8 |
import openpyxl
|
9 |
import os
|
10 |
-
from tkinter import filedialog
|
11 |
|
12 |
# Load the pre-trained EfficientNet-B7 model
|
13 |
model = models.efficientnet_b7(pretrained=True)
|
@@ -20,119 +19,78 @@ transform = transforms.Compose([
|
|
20 |
transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
21 |
std=[0.229, 0.224, 0.225])
|
22 |
])
|
23 |
-
|
|
|
24 |
total_area_sqm = 0
|
25 |
predicted_areas = []
|
|
|
|
|
|
|
26 |
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
# Write the headers to the worksheet
|
38 |
-
worksheet.cell(row=1, column=1).value = "Room ID"
|
39 |
-
worksheet.cell(row=1, column=2).value = "Image File"
|
40 |
-
worksheet.cell(row=1, column=3).value = "Predicted Area (sqm)"
|
41 |
-
|
42 |
-
# Get the last row index to append new data
|
43 |
-
last_row_index = worksheet.max_row if worksheet.max_row else 1
|
44 |
-
|
45 |
-
# Loop over all the images
|
46 |
-
for i, image_file in enumerate(image_files):
|
47 |
-
# Load the input image
|
48 |
-
img = Image.open(image_file.name)
|
49 |
-
# Extract the image file name from the path
|
50 |
-
image_file_name = os.path.basename(image_file.name)
|
51 |
-
# Check if the image is PNG and convert to JPEG if it is
|
52 |
-
if img.format == "PNG":
|
53 |
-
# Convert the image to RGB format
|
54 |
-
img = img.convert("RGB")
|
55 |
-
|
56 |
-
# Apply the transformations to the input image
|
57 |
-
img_transformed = transform(img)
|
58 |
-
|
59 |
-
# Add a batch dimension to the transformed image tensor
|
60 |
-
img_transformed_batch = torch.unsqueeze(img_transformed, 0)
|
61 |
-
|
62 |
-
# Use the pre-trained model to make a prediction on the input image
|
63 |
-
with torch.no_grad():
|
64 |
-
output = model(img_transformed_batch)
|
65 |
-
|
66 |
-
# Convert the output tensor to a probability distribution using softmax
|
67 |
-
softmax = torch.nn.Softmax(dim=1)
|
68 |
-
output_probs = softmax(output)
|
69 |
-
|
70 |
-
# Extract the predicted class (house square footage) from the output probabilities
|
71 |
-
predicted_class = torch.argmax(output_probs)
|
72 |
-
|
73 |
-
# Calculate the predicted area based on the predicted class
|
74 |
-
predicted_area_sqm = 0
|
75 |
-
if predicted_class in [861, 648, 594, 894, 799, 896, 454]:
|
76 |
-
# Convert to grayscale and apply adaptive thresholding
|
77 |
-
gray = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2GRAY)
|
78 |
-
gray = cv2.GaussianBlur(gray, (5, 5), 0)
|
79 |
-
mask = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 11, 2)
|
80 |
-
|
81 |
-
# Apply Canny edge detection to the binary mask
|
82 |
-
edges = cv2.Canny(mask, 30, 100)
|
83 |
-
|
84 |
-
# Apply dilation to fill gaps in the contour
|
85 |
-
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
|
86 |
-
dilated = cv2.dilate(edges, kernel, iterations=2)
|
87 |
-
eroded = cv2.erode(dilated, kernel, iterations=1)
|
88 |
-
|
89 |
-
# Find contours in binary mask
|
90 |
-
contours, _ = cv2.findContours(eroded, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
91 |
-
|
92 |
-
# Find largest contour and calculate area
|
93 |
-
max_area = 0
|
94 |
-
for c in contours:
|
95 |
-
area = cv2.contourArea(c)
|
96 |
-
if area > max_area:
|
97 |
-
max_area = area
|
98 |
-
|
99 |
-
# Convert pixel area to square meters
|
100 |
-
pixels_per_meter = 300 # adjust this value based on your image resolution and actual room dimensions
|
101 |
-
predicted_area_sqm = (max_area + 10) / (2 * pixels_per_meter ** 2)
|
102 |
-
else:
|
103 |
-
predicted_area_sqft = predicted_class.item()
|
104 |
-
predicted_area_sqm = predicted_area_sqft * 0.092903 / 4.2
|
105 |
-
|
106 |
-
# Add the predicted area to the sum
|
107 |
-
total_area_sqm += predicted_area_sqm
|
108 |
-
|
109 |
-
# Add the predicted area to the list of predicted areas
|
110 |
-
predicted_areas.append(predicted_area_sqm)
|
111 |
-
|
112 |
-
# Write the room ID, image file name, and predicted area to the worksheet
|
113 |
-
worksheet.cell(row=last_row_index + i + 1, column=1).value = room_id
|
114 |
-
worksheet.cell(row=last_row_index + i + 1, column=2).value = image_file_name
|
115 |
-
worksheet.cell(row=last_row_index + i + 1, column=3).value = predicted_area_sqm
|
116 |
-
|
117 |
-
# Save the workbook to a temporary file
|
118 |
-
temp_file = "predicted_areas.xlsx"
|
119 |
-
workbook.save(temp_file)
|
120 |
|
121 |
-
|
122 |
-
|
|
|
123 |
|
124 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
|
|
|
126 |
|
127 |
inputs = [
|
128 |
-
gr.inputs.Textbox(label
|
129 |
-
gr.inputs.
|
130 |
]
|
131 |
|
132 |
outputs = [
|
133 |
-
gr.outputs.Textbox(label="
|
134 |
gr.outputs.File(label="Excel Result"),
|
135 |
-
gr.outputs.Image(type="pil", label="Uploaded Image")
|
136 |
]
|
137 |
|
138 |
interface = gr.Interface(
|
@@ -140,8 +98,7 @@ interface = gr.Interface(
|
|
140 |
inputs=inputs,
|
141 |
outputs=outputs,
|
142 |
title="House Predictor",
|
143 |
-
allow_flagging="never"
|
144 |
-
examples=["Capture.PNG","Capture1.PNG","Capture2.PNG"]# Disable flag button
|
145 |
)
|
146 |
|
147 |
if __name__ == "__main__":
|
|
|
7 |
import numpy as np
|
8 |
import openpyxl
|
9 |
import os
|
|
|
10 |
|
11 |
# Load the pre-trained EfficientNet-B7 model
|
12 |
model = models.efficientnet_b7(pretrained=True)
|
|
|
19 |
transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
20 |
std=[0.229, 0.224, 0.225])
|
21 |
])
|
22 |
+
|
23 |
+
def predict_house_area(room_id, image_file):
|
24 |
total_area_sqm = 0
|
25 |
predicted_areas = []
|
26 |
+
|
27 |
+
# Load the input image from the Gradio Image component
|
28 |
+
img = Image.fromarray(image_file)
|
29 |
|
30 |
+
image_file_name = "single_image.jpg"
|
31 |
+
|
32 |
+
if img.format == "PNG":
|
33 |
+
img = img.convert("RGB")
|
34 |
+
|
35 |
+
img_transformed = transform(img)
|
36 |
+
img_transformed_batch = torch.unsqueeze(img_transformed, 0)
|
37 |
+
|
38 |
+
with torch.no_grad():
|
39 |
+
output = model(img_transformed_batch)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
41 |
+
softmax = torch.nn.Softmax(dim=1)
|
42 |
+
output_probs = softmax(output)
|
43 |
+
predicted_class = torch.argmax(output_probs)
|
44 |
|
45 |
+
predicted_area_sqm = 0
|
46 |
+
if predicted_class in [861, 648, 594, 894, 799, 896, 454]:
|
47 |
+
gray = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2GRAY)
|
48 |
+
gray = cv2.GaussianBlur(gray, (5, 5), 0)
|
49 |
+
mask = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY_INV, 11, 2)
|
50 |
+
|
51 |
+
edges = cv2.Canny(mask, 30, 100)
|
52 |
+
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
|
53 |
+
dilated = cv2.dilate(edges, kernel, iterations=2)
|
54 |
+
eroded = cv2.erode(dilated, kernel, iterations=1)
|
55 |
+
contours, _ = cv2.findContours(eroded, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
56 |
+
|
57 |
+
max_area = 0
|
58 |
+
for c in contours:
|
59 |
+
area = cv2.contourArea(c)
|
60 |
+
if area > max_area:
|
61 |
+
max_area = area
|
62 |
+
|
63 |
+
pixels_per_meter = 300
|
64 |
+
predicted_area_sqm = (max_area + 10) / (2 * pixels_per_meter ** 2)
|
65 |
+
else:
|
66 |
+
predicted_area_sqft = predicted_class.item()
|
67 |
+
predicted_area_sqm = predicted_area_sqft * 0.092903 / 4.2
|
68 |
+
|
69 |
+
total_area_sqm += predicted_area_sqm
|
70 |
+
predicted_areas.append(predicted_area_sqm)
|
71 |
+
|
72 |
+
workbook = openpyxl.Workbook()
|
73 |
+
worksheet = workbook.active
|
74 |
+
worksheet.cell(row=1, column=1).value = "Room ID"
|
75 |
+
worksheet.cell(row=1, column=2).value = "Image File"
|
76 |
+
worksheet.cell(row=1, column=3).value = "Predicted Area (sqm)"
|
77 |
+
worksheet.cell(row=2, column=1).value = room_id
|
78 |
+
worksheet.cell(row=2, column=2).value = image_file_name
|
79 |
+
worksheet.cell(row=2, column=3).value = predicted_area_sqm
|
80 |
+
|
81 |
+
temp_file = f"predicted_area_{room_id}.xlsx"
|
82 |
+
workbook.save(temp_file)
|
83 |
|
84 |
+
return f"Predicted house square footage: {predicted_area_sqm:.2f} square meters", temp_file
|
85 |
|
86 |
inputs = [
|
87 |
+
gr.inputs.Textbox(label="Mã Phòng", type="text"),
|
88 |
+
gr.inputs.Image(label="Image")
|
89 |
]
|
90 |
|
91 |
outputs = [
|
92 |
+
gr.outputs.Textbox(label="Predicted House Square Footage"),
|
93 |
gr.outputs.File(label="Excel Result"),
|
|
|
94 |
]
|
95 |
|
96 |
interface = gr.Interface(
|
|
|
98 |
inputs=inputs,
|
99 |
outputs=outputs,
|
100 |
title="House Predictor",
|
101 |
+
allow_flagging="never" # Disable flag button
|
|
|
102 |
)
|
103 |
|
104 |
if __name__ == "__main__":
|