ui
Browse files
main.py
CHANGED
@@ -4,6 +4,15 @@ from generate_tree_images.generate_tree_images import generate_tree_images
|
|
4 |
from classification.classification_predict import classify
|
5 |
import os
|
6 |
import json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
def row_to_feature(row):
|
9 |
feature = {
|
@@ -40,40 +49,122 @@ tif_file_name: the file name of the tif input. tif_input is the folder in which
|
|
40 |
output_directory: the directory were all in-between and final files are stored
|
41 |
|
42 |
generate_tree_images stores the cutout tree images in a separate folder
|
|
|
43 |
"""
|
44 |
|
45 |
-
tif_file_name = "TreeCrownVectorDataset_761588_9673769_20_20_32720.tif"
|
46 |
-
tif_input = "/Users/jonathanseele/ETH/Hackathons/EcoHackathon/WeCanopy/test/" + tif_file_name
|
47 |
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
-
|
51 |
-
if not os.path.exists(output_directory):
|
52 |
-
os.makedirs(output_directory)
|
53 |
|
54 |
-
|
55 |
|
56 |
-
|
57 |
|
58 |
-
processed_geojson
|
59 |
|
60 |
-
|
61 |
-
output_folder = './tree_images'
|
62 |
|
63 |
-
all_top_3_list = [] # Initialize an empty list to accumulate all top_3 lists
|
64 |
|
65 |
-
for file_name in os.listdir(output_folder):
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
|
71 |
-
|
72 |
-
|
73 |
|
74 |
# Assign the accumulated top_3_list to the 'species' column of the dataframe
|
75 |
-
processed_output_df['species'] = all_top_3_list
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
|
77 |
-
final_output_path = 'result'
|
78 |
-
export_geojson(processed_output_df, final_output_path)
|
79 |
|
|
|
4 |
from classification.classification_predict import classify
|
5 |
import os
|
6 |
import json
|
7 |
+
import gradio as gr
|
8 |
+
import rasterio
|
9 |
+
from rasterio.plot import show
|
10 |
+
import geopandas as gpd
|
11 |
+
import matplotlib.pyplot as plt
|
12 |
+
from shapely.geometry import Point
|
13 |
+
|
14 |
+
|
15 |
+
|
16 |
|
17 |
def row_to_feature(row):
|
18 |
feature = {
|
|
|
49 |
output_directory: the directory were all in-between and final files are stored
|
50 |
|
51 |
generate_tree_images stores the cutout tree images in a separate folder
|
52 |
+
|
53 |
"""
|
54 |
|
|
|
|
|
55 |
|
56 |
+
def greet(image_path: str):
|
57 |
+
current_directory = os.getcwd()
|
58 |
+
|
59 |
+
output_directory = os.path.join(current_directory, "outputs")
|
60 |
+
if not os.path.exists(output_directory):
|
61 |
+
os.makedirs(output_directory)
|
62 |
|
63 |
+
run_detectree2(image_path, store_path=output_directory)
|
|
|
|
|
64 |
|
65 |
+
processed_output_df = postprocess(output_directory + '/detectree2_delin.geojson', output_directory + '/processed_delin')
|
66 |
|
67 |
+
processed_geojson = output_directory + '/processed_delin.geojson'
|
68 |
|
69 |
+
generate_tree_images(processed_geojson, image_path)
|
70 |
|
71 |
+
output_folder = './tree_images'
|
|
|
72 |
|
73 |
+
all_top_3_list = [] # Initialize an empty list to accumulate all top_3 lists
|
74 |
|
75 |
+
for file_name in os.listdir(output_folder):
|
76 |
+
file_path = os.path.join(output_folder, file_name)
|
77 |
+
probs = classify(file_path)
|
78 |
+
top_3 = probs.head(3)
|
79 |
+
top_3_list = [[cls, prob] for cls, prob in top_3.items()]
|
80 |
|
81 |
+
# Accumulate the top_3_list for each file
|
82 |
+
all_top_3_list.append(top_3_list)
|
83 |
|
84 |
# Assign the accumulated top_3_list to the 'species' column of the dataframe
|
85 |
+
processed_output_df['species'] = all_top_3_list
|
86 |
+
|
87 |
+
final_output_path = 'result'
|
88 |
+
export_geojson(processed_output_df, final_output_path)
|
89 |
+
|
90 |
+
with rasterio.open(image_path) as src:
|
91 |
+
tif_image = src.read([1, 2, 3]) # Read the first three bands (RGB)
|
92 |
+
tif_transform = src.transform
|
93 |
+
|
94 |
+
# Read the GeoJSON file
|
95 |
+
geojson_data = gpd.read_file(final_output_path)
|
96 |
+
|
97 |
+
# Set the interactive backend to Qt5Agg
|
98 |
+
plt.switch_backend('Qt5Agg') # You have to install PyQt5
|
99 |
+
|
100 |
+
# Enable interactive mode
|
101 |
+
plt.ion()
|
102 |
+
|
103 |
+
# Plotting
|
104 |
+
fig, ax = plt.subplots(figsize=(10, 10))
|
105 |
+
|
106 |
+
# Plot the RGB TIF image
|
107 |
+
show(tif_image, transform=tif_transform, ax=ax)
|
108 |
+
|
109 |
+
# Plot the GeoJSON polygons
|
110 |
+
geojson_data.plot(ax=ax, facecolor='none', edgecolor='red')
|
111 |
+
|
112 |
+
# Set plot title
|
113 |
+
ax.set_title('TIF Image with Tree Crowns Overlay')
|
114 |
+
|
115 |
+
# Create an annotation box
|
116 |
+
annot = ax.annotate("", xy=(0, 0), xytext=(20, 20),
|
117 |
+
textcoords="offset points",
|
118 |
+
bbox=dict(boxstyle="round", fc="w"),
|
119 |
+
arrowprops=dict(arrowstyle="->"))
|
120 |
+
annot.set_visible(False)
|
121 |
+
|
122 |
+
# Create a function to handle mouse clicks
|
123 |
+
def on_click(event):
|
124 |
+
if event.inaxes is not None:
|
125 |
+
# Get the coordinates of the click
|
126 |
+
click_point = Point(event.xdata, event.ydata)
|
127 |
+
# Check if the click is within any of the polygons
|
128 |
+
for idx, row in geojson_data.iterrows():
|
129 |
+
if row['geometry'].contains(click_point):
|
130 |
+
# Access the properties dictionarㅋ
|
131 |
+
# Extract species and confidence score
|
132 |
+
species_info = row['species']
|
133 |
+
confidence_score = row['Confidence_score']
|
134 |
+
# Display information about the clicked polygon
|
135 |
+
annot.xy = (event.xdata, event.ydata)
|
136 |
+
text = f"Polygon {idx}\n\nConfidence:\n{confidence_score}\n\nSpecies and their probability:\n{species_info}"
|
137 |
+
annot.set_text(text)
|
138 |
+
annot.set_visible(True)
|
139 |
+
fig.canvas.draw()
|
140 |
+
break
|
141 |
+
|
142 |
+
# Connect the click event to the handler function
|
143 |
+
cid = fig.canvas.mpl_connect('button_press_event', on_click)
|
144 |
+
|
145 |
+
figure = plt.figure()
|
146 |
+
|
147 |
+
return figure
|
148 |
+
|
149 |
+
#tif_file_name = "TreeCrownVectorDataset_761588_9673769_20_20_32720.tif"
|
150 |
+
#tif_input = "/Users/jonathanseele/ETH/Hackathons/EcoHackathon/WeCanopy/test/" + tif_file_name
|
151 |
+
|
152 |
+
# File paths
|
153 |
+
#tif_file_path = '/Users/taekim/ecohackathon/WeCanopy/test/TreeCrownVectorDataset_761588_9673769_20_20_32720.tif'
|
154 |
+
#geojson_file_path = '/Users/taekim/ecohackathon/WeCanopy/test/result.geojson'
|
155 |
+
|
156 |
+
# Read the TIF file
|
157 |
+
|
158 |
+
|
159 |
+
demo = gr.Interface(
|
160 |
+
fn=greet,
|
161 |
+
inputs=gr.File(type='filepath'),
|
162 |
+
outputs=gr.Plot(label="Tree Crowns")
|
163 |
+
)
|
164 |
+
|
165 |
+
demo.launch()
|
166 |
+
|
167 |
+
|
168 |
+
|
169 |
|
|
|
|
|
170 |
|