Repository navigation
Expand file tree
/
Copy pathsettingGUI.py
More file actions
167 lines (132 loc) · 5.92 KB
/
Copy pathsettingGUI.py
File metadata and controls
167 lines (132 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from asyncio import log
import pandas as pd
import streamlit as st
from deepforest import main
from deepforest import utilities
import os
import subprocess
import sys
from pyproj import CRS
import time
import csv
st.title("Deep Forest Model Settings")
base_dir = "D:\Thesis2026\ProjectCode\Data" # Base directory containing folders with .tif files
col_left, col_right = st.columns(2)
#--- Model Settings 1---
with col_left:
st.header("Select 1st Tiff File")
#--- Choose File from Folder ---
# (2cm) D:\Thesis2026\ProjectCode\Data\TreeAOIWGS84.tif
tif_files = [f for f in os.listdir(base_dir) if f.endswith('.tif')]
if tif_files:
selected_file = st.selectbox("Select 1st TIFF file:", tif_files)
image_path_1 = os.path.join(base_dir, selected_file)
if st.button("Load Image 1"):
st.success(f"✅ Loaded {selected_file}")
st.write(f"Image Path: {image_path_1}")
else:
st.error("No .tif files found in this folder.")
st.header("Settings for 1st Run")
patch_size_1 = st.slider("Patch Size (1st Run)", min_value=400, max_value=2000, value=1200, step=100)
patch_overlap_1 = st.slider("Patch Overlap (1st Run)", min_value=0.0, max_value=1.0, value=0.25, step=0.05)
score_threshold_1 = st.slider("Score Threshold (1st Run)", min_value=0.0, max_value=1.0, value=0.4, step=0.05)
iou_threshold_1 = st.slider("NMS IOU Threshold (1st Run)", min_value=0.0, max_value=1.0, value=0.15, step=0.05)
batch_size_1= st.slider("Batch Size (1st Run)", min_value=1, max_value=32, value=4)
#--- Model Settings 2---
with col_right:
st.header("Select 2nd Tiff File")
#--- Choose File from Folder ---
# (30cm) D:\Thesis2026\ProjectCode\Data\Extract_TreeLINZ_03m.tif
tif_files = [f for f in os.listdir(base_dir) if f.endswith('.tif')]
if tif_files:
selected_file = st.selectbox("Select 2nd TIFF file:", tif_files)
image_path_2 = os.path.join(base_dir, selected_file)
if st.button("Load Image 2"):
st.success(f"✅ Loaded {selected_file}")
st.write(f"Image Path: {image_path_2}")
else:
st.error("No .tif files found in this folder.")
st.header("Settings for 2nd Run")
patch_size_2 = st.slider("Patch Size (2nd Run)", min_value=400, max_value=2000, value=1200, step=100)
patch_overlap_2 = st.slider("Patch Overlap (2nd Run)", min_value=0.0, max_value=1.0, value=0.25, step=0.05)
score_threshold_2 = st.slider("Score Threshold (2nd Run)", min_value=0.0, max_value=1.0, value=0.4, step=0.05)
iou_threshold_2 = st.slider("NMS IOU Threshold (2nd Run)", min_value=0.0, max_value=1.0, value=0.15, step=0.05)
batch_size_2 = st.slider("Batch Size (2nd Run)", min_value=1, max_value=32, value=4)
run_button = st.button("Run Both Settings")
#Define settings dictionaries for both runs
settings_1 = {
'patch_size': patch_size_1,
'patch_overlap': patch_overlap_1,
'score_threshold': score_threshold_1,
'iou_threshold': iou_threshold_1,
'batch_size': batch_size_1,
'image_path': image_path_1
}
settings_2 = {
'patch_size': patch_size_2,
'patch_overlap': patch_overlap_2,
'score_threshold': score_threshold_2,
'iou_threshold': iou_threshold_2,
'batch_size': batch_size_2,
'image_path': image_path_2
}
#Create settings.csv file to store settings for both runs
df = pd.DataFrame([settings_1, settings_2], index=["Run 1", "Run 2"])
df.to_csv("Output/settings.csv", index=True)
#Define model running progress bar function
def run_deepforest_with_progress(image_path,settings,output_gdf_name):
st.write("⏳ Running Model...")
progress_box = st.empty()
progress_bar = st.progress(0)
# Build DeepForest command
cmd = [
sys.executable, "-m", "run_tile",
"--image_path",image_path,
"--patch_size", str(settings['patch_size']),
"--patch_overlap", str(settings['patch_overlap']),
"--score_threshold", str(settings['score_threshold']),
"--iou_threshold", str(settings['iou_threshold']),
"--batch_size", str(settings['batch_size']),
"--output_gdf", output_gdf_name
]
# Run DeepForest as subprocess so we can capture its progress
process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True,bufsize=1)
# Stream DeepForest progress into Streamlit
logs = []
replace_next = False
total_lines = 122
processed_lines = 0
for line in iter(process.stdout.readline, ''):
if not line:
break
line = line.strip()
is_predicting = "predicting" in line.lower()
if is_predicting:
if replace_next:
#Replace the last line
if logs:
logs[-1] = line
else:
logs.append(line)
replace_next = True
else:
logs.append(line)
#If this line contains "predicting", mark for replacement
replace_next = False
#Update progress box
with progress_box.container(height=200):
st.code("\n".join(logs), language="bash") # Show last 10 lines of log
#Update progress bar
processed_lines += 1
progress = int((processed_lines / total_lines) * 100)
progress_bar.progress(min(progress, 100)) # Cap at 100%
time.sleep(0.1) # Small delay to allow UI to update
process.wait()
progress_bar.progress(100)
return output_gdf_name
#--- Run Model & Create settings relational table---
if run_button and image_path_1 and image_path_2 is not None:
gdf1 = run_deepforest_with_progress(image_path_1,settings_1, "Output/run1_predictions.csv")
st.success("✅ 1st run completed! Predictions saved as run1_predictions.csv")
gdf2 = run_deepforest_with_progress(image_path_2,settings_2, "Output/run2_predictions.csv")
st.success("✅ 2nd run completed! Predictions saved as run2_predictions.csv")