Update app.py
Browse files
app.py
CHANGED
|
@@ -955,175 +955,7 @@ with gr.Blocks(
|
|
| 955 |
inputs=[blue_threshold3, masks_state, image_state],
|
| 956 |
outputs=[viability_overlay3, live_count_output3, dead_count_output3, viability_percent_output3, viability_info3]
|
| 957 |
)
|
| 958 |
-
|
| 959 |
-
gr.Markdown("Run segmentation")
|
| 960 |
-
|
| 961 |
-
with gr.Row():
|
| 962 |
-
with gr.Column():
|
| 963 |
-
img_input3 = gr.Image(
|
| 964 |
-
type="pil",
|
| 965 |
-
label="Microscopy image",
|
| 966 |
-
image_mode="RGB",
|
| 967 |
-
height=512
|
| 968 |
-
)
|
| 969 |
-
|
| 970 |
-
model_dropdown3 = gr.Dropdown(
|
| 971 |
-
choices=list(MODEL_OPTIONS.keys()),
|
| 972 |
-
label="Select Model",
|
| 973 |
-
value="Hemocytometer Model"
|
| 974 |
-
)
|
| 975 |
-
|
| 976 |
-
min_size_slider3 = gr.Slider(
|
| 977 |
-
minimum=0,
|
| 978 |
-
maximum=500,
|
| 979 |
-
value=0,
|
| 980 |
-
step=10,
|
| 981 |
-
label="Minimum Cell Size (pixels). Leave at zero for automated recommendation",
|
| 982 |
-
)
|
| 983 |
-
|
| 984 |
-
max_size_slider3 = gr.Slider(
|
| 985 |
-
minimum=0,
|
| 986 |
-
maximum=10000,
|
| 987 |
-
value=10000,
|
| 988 |
-
step=10,
|
| 989 |
-
label="Maximum Cell Size (pixels)",
|
| 990 |
-
)
|
| 991 |
-
|
| 992 |
-
# Stereological counting option
|
| 993 |
-
gr.Markdown("### Stereological Counting")
|
| 994 |
-
use_stereology_checkbox3 = gr.Checkbox(
|
| 995 |
-
label="Enable Stereological Counting",
|
| 996 |
-
value=False,
|
| 997 |
-
info="Use unbiased stereological rules for cell counting"
|
| 998 |
-
)
|
| 999 |
-
|
| 1000 |
-
# Stereological controls (initially hidden)
|
| 1001 |
-
with gr.Group(visible=False) as stereology_controls3:
|
| 1002 |
-
gr.Markdown("""
|
| 1003 |
-
**Stereological Counting Rules:**
|
| 1004 |
-
- Cells touching LEFT or TOP exclusion zones are EXCLUDED
|
| 1005 |
-
- Cells touching RIGHT or BOTTOM edges are INCLUDED
|
| 1006 |
-
- This provides unbiased counting for quantification
|
| 1007 |
-
""")
|
| 1008 |
-
|
| 1009 |
-
exclusion_preview3 = gr.Image(
|
| 1010 |
-
type="pil",
|
| 1011 |
-
label="Exclusion Zone Preview (Red = Excluded)",
|
| 1012 |
-
height=500
|
| 1013 |
-
)
|
| 1014 |
-
|
| 1015 |
-
left_exclusion_slider3 = gr.Slider(
|
| 1016 |
-
minimum=0,
|
| 1017 |
-
maximum=50,
|
| 1018 |
-
value=10,
|
| 1019 |
-
step=1,
|
| 1020 |
-
label="Left Exclusion Width (%)",
|
| 1021 |
-
info="Width of left exclusion zone"
|
| 1022 |
-
)
|
| 1023 |
-
|
| 1024 |
-
top_exclusion_slider3 = gr.Slider(
|
| 1025 |
-
minimum=0,
|
| 1026 |
-
maximum=50,
|
| 1027 |
-
value=10,
|
| 1028 |
-
step=1,
|
| 1029 |
-
label="Top Exclusion Width (%)",
|
| 1030 |
-
info="Width of top exclusion zone"
|
| 1031 |
-
)
|
| 1032 |
-
|
| 1033 |
-
segment_btn3 = gr.Button("🔬 Run Segmentation", variant="primary", size="lg")
|
| 1034 |
-
|
| 1035 |
-
with gr.Column():
|
| 1036 |
-
cell_count_output3 = gr.Number(label="Total Cells Detected", precision=0)
|
| 1037 |
-
confluency_output3 = gr.Number(label="Confluency (%)", precision=1)
|
| 1038 |
-
overlay_output3 = gr.Image(type="pil", label="Segmentation Result")
|
| 1039 |
-
info_output3 = gr.Textbox(label="Processing Info", lines=4)
|
| 1040 |
-
|
| 1041 |
-
# Viability Assessment Section
|
| 1042 |
-
with gr.Group(visible=False) as viability_section3:
|
| 1043 |
-
gr.Markdown("### Viability Assessment (Trypan Blue)")
|
| 1044 |
-
gr.Markdown("Adjust the threshold to classify cells as live (green) or dead (red).")
|
| 1045 |
-
|
| 1046 |
-
with gr.Row():
|
| 1047 |
-
with gr.Column():
|
| 1048 |
-
blue_threshold3 = gr.Slider(
|
| 1049 |
-
minimum=0,
|
| 1050 |
-
maximum=100,
|
| 1051 |
-
value=25,
|
| 1052 |
-
step=1,
|
| 1053 |
-
label="Blue Threshold (%)",
|
| 1054 |
-
info="Higher values = more selective for blue cells"
|
| 1055 |
-
)
|
| 1056 |
-
|
| 1057 |
-
with gr.Column():
|
| 1058 |
-
live_count_output3 = gr.Number(label="Live Cells (Green)", precision=0)
|
| 1059 |
-
dead_count_output3 = gr.Number(label="Dead Cells (Red)", precision=0)
|
| 1060 |
-
|
| 1061 |
-
viability_overlay3 = gr.Image(type="pil", label="Viability Assessment (Green=Live, Red=Dead)")
|
| 1062 |
-
viability_percent_output3 = gr.Number(label="Viability (%)", precision=1)
|
| 1063 |
-
viability_info3 = gr.Textbox(label="Analysis Results", lines=5)
|
| 1064 |
-
|
| 1065 |
-
# Event handlers
|
| 1066 |
-
|
| 1067 |
-
# Toggle stereological controls visibility
|
| 1068 |
-
use_stereology_checkbox3.change(
|
| 1069 |
-
fn=toggle_stereological_mode,
|
| 1070 |
-
inputs=[use_stereology_checkbox3],
|
| 1071 |
-
outputs=[stereology_controls3]
|
| 1072 |
-
)
|
| 1073 |
-
|
| 1074 |
-
# Update exclusion preview when image is uploaded or sliders change
|
| 1075 |
-
img_input3.change(
|
| 1076 |
-
fn=update_exclusion_preview,
|
| 1077 |
-
inputs=[img_input3, left_exclusion_slider3, top_exclusion_slider3],
|
| 1078 |
-
outputs=[exclusion_preview3]
|
| 1079 |
-
)
|
| 1080 |
-
|
| 1081 |
-
left_exclusion_slider3.change(
|
| 1082 |
-
fn=update_exclusion_preview,
|
| 1083 |
-
inputs=[img_input3, left_exclusion_slider3, top_exclusion_slider3],
|
| 1084 |
-
outputs=[exclusion_preview3]
|
| 1085 |
-
)
|
| 1086 |
-
|
| 1087 |
-
top_exclusion_slider3.change(
|
| 1088 |
-
fn=update_exclusion_preview,
|
| 1089 |
-
inputs=[img_input3, left_exclusion_slider3, top_exclusion_slider3],
|
| 1090 |
-
outputs=[exclusion_preview3]
|
| 1091 |
-
)
|
| 1092 |
-
|
| 1093 |
-
# Run segmentation
|
| 1094 |
-
segment_btn3.click(
|
| 1095 |
-
fn=run_segmentation,
|
| 1096 |
-
inputs=[
|
| 1097 |
-
img_input3,
|
| 1098 |
-
model_dropdown3,
|
| 1099 |
-
min_size_slider3,
|
| 1100 |
-
max_size_slider3,
|
| 1101 |
-
use_stereology_checkbox3,
|
| 1102 |
-
left_exclusion_slider3,
|
| 1103 |
-
top_exclusion_slider3
|
| 1104 |
-
],
|
| 1105 |
-
outputs=[
|
| 1106 |
-
cell_count_output3,
|
| 1107 |
-
overlay_output3,
|
| 1108 |
-
info_output3,
|
| 1109 |
-
viability_section3,
|
| 1110 |
-
masks_state,
|
| 1111 |
-
image_state,
|
| 1112 |
-
confluency_output3,
|
| 1113 |
-
min_size_slider3
|
| 1114 |
-
]
|
| 1115 |
-
).then( # Chain the initial viability assessment after segmentation
|
| 1116 |
-
fn=update_viability_realtime,
|
| 1117 |
-
inputs=[blue_threshold3, masks_state, image_state],
|
| 1118 |
-
outputs=[viability_overlay3, live_count_output3, dead_count_output3, viability_percent_output3, viability_info3]
|
| 1119 |
-
)
|
| 1120 |
-
|
| 1121 |
-
# Slider changes update viability in real-time
|
| 1122 |
-
blue_threshold3.change(
|
| 1123 |
-
fn=update_viability_realtime,
|
| 1124 |
-
inputs=[blue_threshold3, masks_state, image_state],
|
| 1125 |
-
outputs=[viability_overlay3, live_count_output3, dead_count_output3, viability_percent_output3, viability_info3]
|
| 1126 |
-
)
|
| 1127 |
with gr.Tab("Tab 4"):
|
| 1128 |
gr.Markdown("Run segmentation")
|
| 1129 |
|
|
|
|
| 955 |
inputs=[blue_threshold3, masks_state, image_state],
|
| 956 |
outputs=[viability_overlay3, live_count_output3, dead_count_output3, viability_percent_output3, viability_info3]
|
| 957 |
)
|
| 958 |
+
|
|
|
|
|
|
|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