Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
|
| 4 |
+
# -----------------------------
|
| 5 |
+
# Image paths
|
| 6 |
+
# -----------------------------
|
| 7 |
+
BASE_DIR = Path(__file__).parent
|
| 8 |
+
IMAGE_DIR = BASE_DIR / "images"
|
| 9 |
+
|
| 10 |
+
PRELOADED_INPUT = IMAGE_DIR / "input.jpg"
|
| 11 |
+
|
| 12 |
+
OUTPUT_IMAGES = [
|
| 13 |
+
IMAGE_DIR / "out1.png",
|
| 14 |
+
IMAGE_DIR / "out2.png",
|
| 15 |
+
IMAGE_DIR / "out3.png",
|
| 16 |
+
IMAGE_DIR / "out4.png",
|
| 17 |
+
IMAGE_DIR / "out5.png",
|
| 18 |
+
]
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# -----------------------------
|
| 22 |
+
# Analyze function
|
| 23 |
+
# -----------------------------
|
| 24 |
+
def analyze_image(input_image):
|
| 25 |
+
"""
|
| 26 |
+
This function receives the input image from the user.
|
| 27 |
+
When Analyze is clicked, it returns the five preloaded output images.
|
| 28 |
+
"""
|
| 29 |
+
|
| 30 |
+
if input_image is None:
|
| 31 |
+
raise gr.Error("Please upload or select an input image before clicking Analyze.")
|
| 32 |
+
|
| 33 |
+
results = []
|
| 34 |
+
|
| 35 |
+
for img_path in OUTPUT_IMAGES:
|
| 36 |
+
if img_path.exists():
|
| 37 |
+
results.append(str(img_path))
|
| 38 |
+
else:
|
| 39 |
+
raise gr.Error(f"Missing image file: {img_path.name}")
|
| 40 |
+
|
| 41 |
+
return results
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
# -----------------------------
|
| 45 |
+
# Gradio Interface
|
| 46 |
+
# -----------------------------
|
| 47 |
+
with gr.Blocks(title="Cell Image Analysis") as demo:
|
| 48 |
+
|
| 49 |
+
gr.Markdown(
|
| 50 |
+
"""
|
| 51 |
+
# Cell Image Analysis Demo
|
| 52 |
+
|
| 53 |
+
Upload or use the sample input image, then click **Analyze**
|
| 54 |
+
to display the five preloaded output images.
|
| 55 |
+
"""
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
with gr.Row():
|
| 59 |
+
with gr.Column():
|
| 60 |
+
input_image = gr.Image(
|
| 61 |
+
label="Input Image",
|
| 62 |
+
type="pil",
|
| 63 |
+
value=str(PRELOADED_INPUT) if PRELOADED_INPUT.exists() else None,
|
| 64 |
+
height=400
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
analyze_button = gr.Button("Analyze", variant="primary")
|
| 68 |
+
|
| 69 |
+
with gr.Column():
|
| 70 |
+
output_gallery = gr.Gallery(
|
| 71 |
+
label="Analysis Output Images",
|
| 72 |
+
columns=2,
|
| 73 |
+
rows=3,
|
| 74 |
+
height="auto",
|
| 75 |
+
object_fit="contain"
|
| 76 |
+
)
|
| 77 |
+
|
| 78 |
+
analyze_button.click(
|
| 79 |
+
fn=analyze_image,
|
| 80 |
+
inputs=input_image,
|
| 81 |
+
outputs=output_gallery
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
# -----------------------------
|
| 86 |
+
# Launch app
|
| 87 |
+
# -----------------------------
|
| 88 |
+
if __name__ == "__main__":
|
| 89 |
+
demo.launch()
|