Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,82 @@
|
|
| 1 |
-
import gradio
|
|
|
|
|
|
|
| 2 |
import cv2
|
| 3 |
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
# For Spaces usage, head to https://huggingface.co/docs/hub/spaces
|
| 12 |
-
iface = gradio.Interface(
|
| 13 |
-
fn=inference,
|
| 14 |
-
inputs='image',
|
| 15 |
-
outputs='image',
|
| 16 |
-
title='Hello World',
|
| 17 |
-
description='The simplest interface!',
|
| 18 |
-
examples=["llama.jpg"])
|
| 19 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
iface.launch()
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import math
|
| 3 |
+
import numpy as np
|
| 4 |
import cv2
|
| 5 |
|
| 6 |
+
def calculate_flow_direction(dtm_file):
|
| 7 |
+
# Step 1: Read the DTM data and convert it to a 2D array of elevation values
|
| 8 |
+
elevation_array = cv2.imread(dtm_file.name, cv2.IMREAD_GRAYSCALE).astype(float)
|
| 9 |
|
| 10 |
+
# Step 2: Compute the slope and aspect of each cell in the DTM
|
| 11 |
+
cell_size = 30 # Set the cell size in meters
|
| 12 |
+
dx = dy = cell_size
|
| 13 |
|
| 14 |
+
# Compute the x and y gradient values
|
| 15 |
+
dzdx, dzdy = np.gradient(elevation_array, dx, dy)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# Compute the slope angle in radians
|
| 18 |
+
slope = np.arctan(np.sqrt(dzdx**2 + dzdy**2))
|
| 19 |
+
|
| 20 |
+
# Compute the aspect angle in radians
|
| 21 |
+
aspect = np.arctan2(-dzdy, dzdx)
|
| 22 |
+
|
| 23 |
+
# Step 3: Compute the flow direction for each cell based on the slope and aspect values
|
| 24 |
+
# Compute the x and y components of the flow direction vector
|
| 25 |
+
flow_dir_x = np.sin(aspect)
|
| 26 |
+
flow_dir_y = np.cos(aspect)
|
| 27 |
+
|
| 28 |
+
# Normalize the flow direction vector to unit length
|
| 29 |
+
flow_dir_length = np.sqrt(flow_dir_x**2 + flow_dir_y**2)
|
| 30 |
+
flow_dir_x /= flow_dir_length
|
| 31 |
+
flow_dir_y /= flow_dir_length
|
| 32 |
+
|
| 33 |
+
# Step 4: Compute the flow accumulation for each cell based on the flow direction of neighboring cells
|
| 34 |
+
def compute_flow_accumulation(flow_dir_x, flow_dir_y):
|
| 35 |
+
# Initialize the flow accumulation array to zero
|
| 36 |
+
flow_accum = np.zeros_like(elevation_array)
|
| 37 |
+
|
| 38 |
+
# Find the cells with no upstream flow (i.e., cells with zero slope)
|
| 39 |
+
no_upstream_flow = (slope == 0)
|
| 40 |
+
flow_accum[no_upstream_flow] = 1
|
| 41 |
+
|
| 42 |
+
# Recursively accumulate flow downstream
|
| 43 |
+
rows, cols = elevation_array.shape
|
| 44 |
+
for i in range(1, rows-1):
|
| 45 |
+
for j in range(1, cols-1):
|
| 46 |
+
if no_upstream_flow[i,j]:
|
| 47 |
+
continue
|
| 48 |
+
|
| 49 |
+
# Find the downstream cell
|
| 50 |
+
di = int(round(flow_dir_y[i,j]))
|
| 51 |
+
dj = int(round(flow_dir_x[i,j]))
|
| 52 |
+
downstream = (i+di, j+dj)
|
| 53 |
+
|
| 54 |
+
# Add the flow accumulation from the downstream cell
|
| 55 |
+
flow_accum[i,j] = flow_accum[downstream] + 1
|
| 56 |
+
|
| 57 |
+
return flow_accum
|
| 58 |
+
|
| 59 |
+
# Step 5: Compute the flow accumulation and direction maps
|
| 60 |
+
flow_accum = compute_flow_accumulation(flow_dir_x, flow_dir_y)
|
| 61 |
+
flow_accum_norm = flow_accum / np.max(flow_accum) * 255
|
| 62 |
+
flow_accum_rgb = cv2.cvtColor(flow_accum_norm.astype(np.uint8), cv2.COLOR_GRAY2RGB)
|
| 63 |
+
|
| 64 |
+
flow_dir_x_norm = (flow_dir_x + 1) / 2 * 255
|
| 65 |
+
flow_dir_y_norm = (flow_dir_y + 1) / 2 * 255
|
| 66 |
+
flow_dir_rgb = np.zeros((elevation_array.shape[0], elevation_array.shape[1], 3), dtype=np.uint8)
|
| 67 |
+
flow_dir_rgb[:,:,0] = flow_dir_x_norm.astype(np.uint8)
|
| 68 |
+
flow_dir_rgb[:,:,1] = flow_dir_y_norm.astype(np.uint8)
|
| 69 |
+
|
| 70 |
+
return flow_dir_rgb, flow_accum_rgb
|
| 71 |
+
|
| 72 |
+
# Define the Gradio interface
|
| 73 |
+
iface = gr.Interface(
|
| 74 |
+
fn=calculate_flow_direction,
|
| 75 |
+
inputs="file",
|
| 76 |
+
outputs=["image", "image"],
|
| 77 |
+
title="DTM Flow Direction and Accumulation Calculator",
|
| 78 |
+
description="This app calculates the flow direction and accumulation maps for a digital terrain model (DTM) using the `math` module.",
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
# Launch the interface
|
| 82 |
iface.launch()
|