Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -1,37 +1,57 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import Colony_Analyzer_AI2_HF as analyzer
|
| 3 |
from PIL import Image
|
| 4 |
-
import cv2
|
| 5 |
-
import numpy as np
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
# Analysis function adapted from your Tkinter app
|
| 10 |
def analyze_image(image, min_size, circularity):
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
return Image.fromarray(processed_img), picname, excelname
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
)
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from PIL import Image
|
|
|
|
|
|
|
| 3 |
|
| 4 |
+
# Single image analysis function (your existing logic)
|
|
|
|
|
|
|
| 5 |
def analyze_image(image, min_size, circularity):
|
| 6 |
+
import Colony_Analyzer_AI2_HF as analyzer
|
| 7 |
+
processed_img, picname, excelname = analyzer.main([image, min_size, circularity])
|
| 8 |
+
return Image.fromarray(processed_img), picname, excelname
|
| 9 |
+
|
| 10 |
+
# Z-stack analysis function (adapt with your own logic)
|
| 11 |
+
def analyze_zstack(images, min_size, circularity):
|
| 12 |
+
# images: list of PIL images
|
| 13 |
+
# Plug in your own z-stack segmentation logic here
|
| 14 |
+
# Example stub: pass images as a list to your analyzer
|
| 15 |
+
import Colony_Analyzer_AI_zstack2_HF as analyzer
|
| 16 |
+
images = [Image.open(f.name) for f in images]
|
| 17 |
+
processed_img, picname, excelname = analyzer.main([images, min_size, circularity])
|
| 18 |
return Image.fromarray(processed_img), picname, excelname
|
| 19 |
|
| 20 |
+
with gr.Blocks() as demo:
|
| 21 |
+
gr.Markdown("# AI Colony Analyzer\nUpload an image (or Z-Stack) to run colony analysis.")
|
| 22 |
+
|
| 23 |
+
z_stack_checkbox = gr.Checkbox(label="Enable Z-Stack", value=False)
|
| 24 |
+
image_input_single = gr.Image(type="pil", label="Upload Image", visible=True)
|
| 25 |
+
image_input_multi = gr.File(file_count="multiple", type="file", label="Upload Z-Stack Images", visible=False)
|
| 26 |
+
min_size_input = gr.Number(label="Minimum Colony Size (pixels)", value=1000)
|
| 27 |
+
circularity_input = gr.Number(label="Minimum Circularity", value=0.25)
|
| 28 |
+
output_image = gr.Image(type="pil", label="Analyzed Image")
|
| 29 |
+
output_file_img = gr.File(label="Download Image")
|
| 30 |
+
output_file_excel = gr.File(label="Download results (Excel)")
|
| 31 |
+
process_btn = gr.Button("Process")
|
| 32 |
+
|
| 33 |
+
def toggle_inputs(z_stack_enabled):
|
| 34 |
+
return (
|
| 35 |
+
gr.update(visible=not z_stack_enabled), # single input
|
| 36 |
+
gr.update(visible=z_stack_enabled) # multi input
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
z_stack_checkbox.change(
|
| 40 |
+
toggle_inputs,
|
| 41 |
+
inputs=z_stack_checkbox,
|
| 42 |
+
outputs=[image_input_single, image_input_multi]
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
def conditional_analyze(z_stack, single_image, multi_images, min_size, circularity):
|
| 46 |
+
if z_stack:
|
| 47 |
+
return analyze_zstack(multi_images, min_size, circularity)
|
| 48 |
+
else:
|
| 49 |
+
return analyze_image(single_image, min_size, circularity)
|
| 50 |
+
|
| 51 |
+
process_btn.click(
|
| 52 |
+
conditional_analyze,
|
| 53 |
+
inputs=[z_stack_checkbox, image_input_single, image_input_multi, min_size_input, circularity_input],
|
| 54 |
+
outputs=[output_image, output_file_img, output_file_excel]
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
demo.launch()
|