OttoYu commited on
Commit
14e4d7d
1 Parent(s): dc2f2f8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import numpy as np
3
+ import laspy
4
+ from sklearn.cluster import DBSCAN
5
+ from sklearn.metrics import accuracy_score
6
+ from scipy.spatial import ConvexHull
7
+ from skimage.measure import profile_line
8
+
9
+ st.title("Tree Analysis App")
10
+
11
+ # Upload LAS file
12
+ uploaded_file = st.file_uploader("Upload a LAS file", type=["las", "laz"])
13
+
14
+ if uploaded_file is not None:
15
+ # Load the LAS file
16
+ las_file = laspy.read(uploaded_file)
17
+ height_filter = np.logical_and(las_file.z > 1, las_file.z < 30)
18
+ las_file = las_file[height_filter]
19
+ # Extract the x and y coordinates from the LAS file
20
+ x = las_file.x
21
+ y = las_file.y
22
+
23
+ # Combine the x and y coordinates into a feature matrix
24
+ feature_matrix = np.column_stack((x, y))
25
+
26
+ # Segment the trees using DBSCAN clustering with a specified distance threshold (e.g., 2 meters)
27
+ tree_labels = DBSCAN(eps=2, min_samples=10).fit_predict(feature_matrix)
28
+
29
+ # Count the number of trees
30
+ num_trees = len(set(tree_labels)) - (1 if -1 in tree_labels else 0)
31
+ st.write(f"Number of trees: {num_trees}")
32
+ for i in range(num_trees):
33
+ indices = np.where(tree_labels == i)[0]
34
+
35
+ tree_x = x[indices]
36
+ tree_y = y[indices]
37
+
38
+ tree_mid_x = np.mean(tree_x)
39
+ tree_mid_y = np.mean(tree_y)
40
+
41
+ st.write(f"Tree {i+1} middle point: ({tree_mid_x:.3f}, {tree_mid_y:.3f})")
42
+
43
+ def calculate_tree_data(points):
44
+ height = np.max(points.z) - np.min(points.z)
45
+ xy_points = np.column_stack((points.X, points.Y))
46
+ hull = ConvexHull(xy_points)
47
+ crown_spread = np.sqrt(hull.area / np.pi)/10
48
+ z_trunk = np.percentile(points.z, 20) # assume trunk is the lowest 20% of the points
49
+ trunk_points = points[points.z < z_trunk]
50
+ dbh = 2 * np.mean(np.sqrt((trunk_points.X - np.mean(trunk_points.X)) ** 2 + (trunk_points.Y - np.mean(trunk_points.Y)) ** 2))
51
+ return height, crown_spread, dbh
52
+
53
+ tree_data = []
54
+ for tree_label in range(num_trees):
55
+ # Extract points for the current tree
56
+ tree_points = las_file.points[tree_labels == tree_label]
57
+ # Calculate tree data
58
+ data = calculate_tree_data(tree_points)
59
+ # Append data to list
60
+ tree_data.append(data)
61
+
62
+ # Print the data for each tree
63
+ for i, data in enumerate(tree_data):
64
+ st.write(f"Tree {i + 1} - Height: {data[0]:.3f} m, Crown Spread: {data[1]:.3f} m, DBH: {data[2]:.3f} mm")