generated from KemingHe/agentic-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamlit_app.py
361 lines (307 loc) · 10.9 KB
/
streamlit_app.py
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# Standard library imports
import json
import os
from typing import Dict, List, TypedDict, Optional
import uuid
# Third-party library imports
import numpy as np
from PIL import Image
import streamlit as st
from streamlit_image_annotation import pointdet
# Local application imports
from strain_analysis_core import normalize_points_by_scale, calculate_strain_tensor
from strain_analysis_data import (
StrainData,
RegressionResult,
AnalysisResults,
analyze_strain_data,
create_strain_plot,
create_strain_dataframe,
export_raw_data,
export_analysis_results,
format_scientific,
)
from strain_analysis_ui import (
Point,
FileData,
TEMP_IMAGE_PATH,
setup_page_config,
initialize_session_state,
validate_annotation,
save_annotation,
display_files_table,
display_point_coordinates,
display_analysis_results,
display_export_section,
cleanup_temp_files,
)
# Configure Streamlit page
setup_page_config()
# Initialize session state
initialize_session_state()
# Privacy notice
st.info(
"🔒 Your work is saved locally in this browser tab - remember to export your data before closing"
)
# Title and description
st.title("👁️ Strain Seer - 2D Strain Analysis Tool")
# Main description
st.markdown("""
## 📊 What is 2D Strain Analysis?
2D strain analysis measures material deformation using 5 fiducial markers and 2 scale markers to calculate:
- εxx: Stretching/compression in x-direction
- εyy: Stretching/compression in y-direction
- εxy: Shearing deformation
### 🔄 How It Works
1. Mark 5 fiducial points (red) and 2 scale points (green)
2. Enter scale length to convert pixels to real-world units
3. Input deformation distance for each image
4. View strain analysis and regression results
### 📍 Point Labeling & Scale
```text
0 ------- 1
| \\ / |
| 4 |
| / \\ |
3 ------- 2
```
- Points 0-3: Corner points (clockwise from top-left)
- Point 4: Center point
- Scale points: Calibration markers (green)
Scale points ensure consistent measurements across images and accurate pixel-to-real-world conversion. For example:
```text
Image 1 (1000px) Image 2 (2000px)
+----------------+ +------------------------+
| | | |
| [10mm] | | [10mm] |
| |----| | | |----| |
| | | |
+----------------+ +------------------------+
Without Scale: With Scale:
- 100px = ?mm - 100px = 1mm
- 200px = ?mm - 200px = 1mm
```
""")
# Create two columns for Quick Start Guide and Tips
col1, col2 = st.columns(2)
with col1:
st.markdown("""
### 🚀 Quick Start Guide
1. **Upload Images** (PNG, JPG, JPEG, GIF, BMP)
- Upload files in order (minimum 3 for reliable analysis)
- Order determines sequence in analysis and plots
2. **Annotate Points**
- Mark 5 red fiducial points and 2 green scale points
- Enter deformation distance for each image
3. **Run Analysis**
- Set scale length
- View results
- Export data
""")
with col2:
st.markdown("""
### 💡 Tips for Best Results
- Clear fiducial markers and known scale distance
- Well-lit, focused images with correct point order (0-4)
- Upload 3+ images in sequence for reliable analysis
""")
# Define label list with colors
label_list = ["Fiducial", "Scale"]
# File uploader
uploaded_files = st.file_uploader(
"Choose image files",
type=["png", "jpg", "jpeg", "gif", "bmp"],
accept_multiple_files=True,
)
# Save uploaded files and initialize their data
for i, uploaded_file in enumerate(uploaded_files):
# Check if file exists in our data
file_exists = any(
data["filename"] == uploaded_file.name
for data in st.session_state.files_data.values()
)
if not file_exists:
file_id = str(uuid.uuid4())
save_annotation(file_id, uploaded_file.name, i)
# Display files table with status
if st.session_state.files_data:
display_files_table(st.session_state.files_data)
# File selection and annotation section
st.markdown("### 🎯 Image Annotation")
selected_filename = st.selectbox(
"Select image to annotate",
options=[
data["filename"]
for data in sorted(
st.session_state.files_data.values(), key=lambda x: x["order"]
)
],
)
if selected_filename:
# Find the file data by filename
file_data = next(
data
for data in st.session_state.files_data.values()
if data["filename"] == selected_filename
)
# Save the image temporarily
for uploaded_file in uploaded_files:
if uploaded_file.name == selected_filename:
with open(TEMP_IMAGE_PATH, "wb") as f:
f.write(uploaded_file.getvalue())
break
# Get image dimensions
with Image.open(TEMP_IMAGE_PATH) as img:
width, height = img.size
# Calculate dimensions maintaining aspect ratio with minimum size of 512
scale = max(512 / width, 512 / height)
display_width = int(width * scale)
display_height = int(height * scale)
# Point detection annotation
points = pointdet(
TEMP_IMAGE_PATH,
label_list=label_list,
points=[[p["point"][0], p["point"][1]] for p in file_data["points"]],
labels=[0 if p["label"] == "Fiducial" else 1 for p in file_data["points"]],
height=display_height,
width=display_width,
point_width=3,
use_space=True,
)
# Display point coordinates
display_point_coordinates(file_data["points"])
# Add deformation distance input after annotation display
st.markdown("#### Deformation Distance")
deformation_distance = st.number_input(
"Enter deformation distance (mm)",
min_value=0.0,
value=float(file_data["deformation_distance"])
if file_data["deformation_distance"] is not None
else 0.0,
step=0.1,
help="Press Enter or click outside to save the value",
)
save_annotation(
file_data["id"],
file_data["filename"],
file_data["order"],
points,
deformation_distance,
)
# Analysis Section
st.markdown("---") # Horizontal rule
st.markdown("## 🔬 Strain Analysis")
# Check if all files are complete and minimum number of images
all_complete = all(
data["status"][0] for data in st.session_state.files_data.values()
)
num_images = len(st.session_state.files_data)
if not all_complete:
st.warning("""
⚠️ **Analysis Disabled**
Complete these steps:
1. Mark all points (5 fiducial + 2 scale)
2. Set deformation distance
3. Verify "Complete" status
""")
elif num_images < 3:
st.warning(f"""
⚠️ **Analysis Disabled**
Need more images for reliable analysis:
- Current: {num_images} image{"s" if num_images != 1 else ""}
- Required: Minimum 3 images
- More images provide better regression results
""")
else:
st.success("""
✅ **Ready for Analysis**
Next steps:
1. Set scale length
2. View results
3. Export data
""")
# Scale length input
st.markdown("### 📏 Scale Calibration")
st.markdown("Enter physical length of scale markers (mm)")
scale_length = st.number_input(
"Scale Length (mm)",
min_value=0.0,
value=st.session_state.last_scale_length,
step=0.1,
)
# Store scale length
st.session_state.last_scale_length = scale_length
# Run analysis if scale length changed or no analysis exists
if (
st.session_state.analysis_results is None
or st.session_state.analysis_results["scale_length"] != scale_length
):
# Process all files and calculate strain tensors
strain_data = []
for data in sorted(
st.session_state.files_data.values(), key=lambda x: x["order"]
):
# Extract points
fiducial_points = []
scale_points = []
for point in data["points"]:
if point["label"] == "Fiducial":
fiducial_points.append(point["point"])
else:
scale_points.append(point["point"])
# Convert to numpy arrays and validate
fiducial_points = np.array(fiducial_points)
scale_points = np.array(scale_points)
# Validate number of points
if len(fiducial_points) != 5:
st.error(
f"File {data['filename']} must have exactly 5 fiducial points"
)
continue
if len(scale_points) != 2:
st.error(
f"File {data['filename']} must have exactly 2 scale points"
)
continue
# Normalize points
normalized_points = normalize_points_by_scale(
fiducial_points, scale_points, scale_length
)
# Calculate strain tensor
strain_tensor = calculate_strain_tensor(
normalized_points, normalized_points
) # Using same points for now
strain_data.append(
{
"filename": data["filename"],
"deformation_distance": data["deformation_distance"],
"strain_tensor": strain_tensor,
}
)
# Analyze strain data
st.session_state.analysis_results = analyze_strain_data(
strain_data, scale_length
)
# Display analysis results
display_analysis_results(
st.session_state.analysis_results["strain_data"],
st.session_state.analysis_results["regression_results"],
)
# Display export section
display_export_section(
st.session_state.files_data,
scale_length,
st.session_state.analysis_results["strain_data"],
st.session_state.analysis_results["regression_results"],
)
# Important notice
st.markdown("---") # Horizontal rule
st.markdown("### 🔄 Restart Analysis")
st.info("""
To start a new analysis:
1. Export your data if needed
2. Refresh your browser tab
3. All data will be cleared and you can start fresh
""")
# Clean up temporary files
cleanup_temp_files()