Spaces:
Sleeping
Sleeping
Mehmet Batuhan Duman
commited on
Commit
·
6461af3
1
Parent(s):
bc3e686
Changes on scan
Browse files
app.py
CHANGED
@@ -133,20 +133,34 @@ else:
|
|
133 |
print("Model file not found at", model_path)
|
134 |
|
135 |
|
136 |
-
def
|
137 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
138 |
window_size = (80, 80)
|
139 |
stride = 10
|
140 |
|
141 |
-
height, width, channels =
|
|
|
142 |
probabilities_map = []
|
143 |
|
144 |
for y in range(0, height - window_size[1] + 1, stride):
|
145 |
row_probabilities = []
|
146 |
for x in range(0, width - window_size[0] + 1, stride):
|
147 |
-
cropped_window =
|
148 |
-
cropped_window_torch =
|
149 |
-
0).cpu()
|
150 |
|
151 |
with torch.no_grad():
|
152 |
probabilities = model(cropped_window_torch)
|
@@ -158,26 +172,18 @@ def scanmap(satellite_image, model):
|
|
158 |
probabilities_map = np.array(probabilities_map)
|
159 |
return probabilities_map
|
160 |
|
161 |
-
|
162 |
def gradio_process_image(input_image):
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
if isinstance(input_image, np.ndarray):
|
167 |
-
input_image = Image.fromarray(input_image)
|
168 |
-
|
169 |
-
image_np = np.array(input_image.convert("RGB"))
|
170 |
-
heatmap = scanmap(image_np, model)
|
171 |
-
heatmap_normalized = (heatmap - np.min(heatmap)) / (np.max(heatmap) - np.min(heatmap))
|
172 |
-
heatmap_img = Image.fromarray(np.uint8(plt.cm.hot(heatmap_normalized) * 255)).convert('RGB')
|
173 |
-
return input_image, heatmap_img
|
174 |
-
|
175 |
|
176 |
inputs = gr.Image(label="Upload Image")
|
177 |
outputs = [
|
178 |
gr.Image(label="Original Image"),
|
179 |
gr.Image(label="Heatmap"),
|
|
|
180 |
]
|
181 |
|
182 |
iface = gr.Interface(fn=gradio_process_image, inputs=inputs, outputs=outputs)
|
183 |
iface.launch()
|
|
|
|
|
|
133 |
print("Model file not found at", model_path)
|
134 |
|
135 |
|
136 |
+
def process_image(input_image):
|
137 |
+
image = Image.fromarray(input_image).convert("RGB")
|
138 |
+
|
139 |
+
start_time = time.time()
|
140 |
+
heatmap = scanmap(np.array(image), model)
|
141 |
+
elapsed_time = time.time() - start_time
|
142 |
+
heatmap_img = Image.fromarray(np.uint8(plt.cm.hot(heatmap) * 255)).convert('RGB')
|
143 |
+
|
144 |
+
heatmap_img = heatmap_img.resize(image.size)
|
145 |
+
|
146 |
+
return image, heatmap_img, int(elapsed_time)
|
147 |
+
|
148 |
+
|
149 |
+
def scanmap(image_np, model):
|
150 |
+
image_np = image_np.astype(np.float32) / 255.0
|
151 |
+
|
152 |
window_size = (80, 80)
|
153 |
stride = 10
|
154 |
|
155 |
+
height, width, channels = image_np.shape
|
156 |
+
|
157 |
probabilities_map = []
|
158 |
|
159 |
for y in range(0, height - window_size[1] + 1, stride):
|
160 |
row_probabilities = []
|
161 |
for x in range(0, width - window_size[0] + 1, stride):
|
162 |
+
cropped_window = image_np[y:y + window_size[1], x:x + window_size[0]]
|
163 |
+
cropped_window_torch = transforms.ToTensor()(cropped_window).unsqueeze(0)
|
|
|
164 |
|
165 |
with torch.no_grad():
|
166 |
probabilities = model(cropped_window_torch)
|
|
|
172 |
probabilities_map = np.array(probabilities_map)
|
173 |
return probabilities_map
|
174 |
|
|
|
175 |
def gradio_process_image(input_image):
|
176 |
+
original, heatmap, elapsed_time = process_image(input_image)
|
177 |
+
return original, heatmap, f"Elapsed Time (seconds): {elapsed_time}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
178 |
|
179 |
inputs = gr.Image(label="Upload Image")
|
180 |
outputs = [
|
181 |
gr.Image(label="Original Image"),
|
182 |
gr.Image(label="Heatmap"),
|
183 |
+
gr.Textbox(label="Elapsed Time")
|
184 |
]
|
185 |
|
186 |
iface = gr.Interface(fn=gradio_process_image, inputs=inputs, outputs=outputs)
|
187 |
iface.launch()
|
188 |
+
|
189 |
+
|