Mehmet Batuhan Duman commited on
Commit
f3ca93c
·
1 Parent(s): 521227f

Changes on scan

Browse files
Files changed (1) hide show
  1. app.py +14 -26
app.py CHANGED
@@ -133,34 +133,20 @@ else:
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,18 +158,20 @@ def scanmap(image_np, model):
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
-
 
133
  print("Model file not found at", model_path)
134
 
135
 
136
+ def scanmap(satellite_image, model):
137
+ satellite_image = satellite_image.astype(np.float32) / 255.0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
138
  window_size = (80, 80)
139
  stride = 10
140
 
141
+ height, width, channels = satellite_image.shape
 
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 = satellite_image[y:y + window_size[1], x:x + window_size[0]]
148
+ cropped_window_torch = torch.tensor(cropped_window.transpose(2, 0, 1), dtype=torch.float32).unsqueeze(
149
+ 0).cpu()
150
 
151
  with torch.no_grad():
152
  probabilities = model(cropped_window_torch)
 
158
  probabilities_map = np.array(probabilities_map)
159
  return probabilities_map
160
 
161
+
162
  def gradio_process_image(input_image):
163
+ image_np = np.array(input_image.convert("RGB"))
164
+ heatmap = scanmap(image_np, model)
165
+ heatmap_normalized = (heatmap - np.min(heatmap)) / (np.max(heatmap) - np.min(heatmap))
166
+ heatmap_img = Image.fromarray(np.uint8(plt.cm.hot(heatmap_normalized) * 255)).convert('RGB')
167
+ return input_image, heatmap_img
168
+
169
 
170
  inputs = gr.Image(label="Upload Image")
171
  outputs = [
172
  gr.Image(label="Original Image"),
173
  gr.Image(label="Heatmap"),
 
174
  ]
175
 
176
  iface = gr.Interface(fn=gradio_process_image, inputs=inputs, outputs=outputs)
177
+ iface.launch()