Spaces:
Sleeping
Sleeping
muhammadhamza-stack commited on
Commit ·
f902c85
1
Parent(s): e04d1d0
initial commit
Browse files- .gitattributes +2 -0
- app.py +114 -0
- data/1.png +3 -0
- data/2.png +3 -0
- data/211.png +3 -0
- data/3.png +3 -0
- requirements.txt +8 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
*.jpg filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
*.png filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import SegformerForSemanticSegmentation, SegformerImageProcessor
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import torch
|
| 5 |
+
import numpy as np
|
| 6 |
+
|
| 7 |
+
# --- Documentation Strings ---
|
| 8 |
+
|
| 9 |
+
USAGE_GUIDELINES = """
|
| 10 |
+
## 1. Quick Start Guide: CellVision AI (Grayscale Mask)
|
| 11 |
+
|
| 12 |
+
CellVision AI generates a grayscale segmentation mask for microscopy images.
|
| 13 |
+
|
| 14 |
+
Steps:
|
| 15 |
+
1. Upload your image.
|
| 16 |
+
2. Click "Analyze Image".
|
| 17 |
+
3. Review the gray-white mask result.
|
| 18 |
+
"""
|
| 19 |
+
|
| 20 |
+
INPUT_EXPLANATION = """
|
| 21 |
+
## 2. Input Requirements
|
| 22 |
+
|
| 23 |
+
| Field | Format |
|
| 24 |
+
|-------|--------|
|
| 25 |
+
| Image Upload | JPG / PNG |
|
| 26 |
+
|
| 27 |
+
Image is resized to 512×512 before inference.
|
| 28 |
+
"""
|
| 29 |
+
|
| 30 |
+
OUTPUT_EXPLANATION = """
|
| 31 |
+
## 3. Output Description (Gray & White Mask)
|
| 32 |
+
|
| 33 |
+
• Background = White
|
| 34 |
+
• Segmented Objects = Gray
|
| 35 |
+
• Enlarged by 300% (3×)
|
| 36 |
+
• Subtle grayscale research-style output
|
| 37 |
+
"""
|
| 38 |
+
|
| 39 |
+
# --------------------
|
| 40 |
+
# Model
|
| 41 |
+
# --------------------
|
| 42 |
+
processor = SegformerImageProcessor(do_reduce_labels=False)
|
| 43 |
+
model = SegformerForSemanticSegmentation.from_pretrained(
|
| 44 |
+
"nvidia/segformer-b0-finetuned-ade-512-512"
|
| 45 |
+
)
|
| 46 |
+
model.eval()
|
| 47 |
+
|
| 48 |
+
def segment_image(input_image):
|
| 49 |
+
if input_image is None:
|
| 50 |
+
gr.Warning("Upload an image first.")
|
| 51 |
+
return None
|
| 52 |
+
|
| 53 |
+
inputs = processor(images=input_image, return_tensors="pt")
|
| 54 |
+
|
| 55 |
+
with torch.no_grad():
|
| 56 |
+
outputs = model(**inputs)
|
| 57 |
+
|
| 58 |
+
logits = outputs.logits
|
| 59 |
+
pred_mask = torch.argmax(logits, dim=1)[0].cpu().numpy()
|
| 60 |
+
|
| 61 |
+
# Gray & White mask
|
| 62 |
+
gray_mask = np.where(pred_mask == 0, 255, 128).astype(np.uint8)
|
| 63 |
+
|
| 64 |
+
output_image = Image.fromarray(gray_mask)
|
| 65 |
+
|
| 66 |
+
# Scale 3x
|
| 67 |
+
scale_factor = 3
|
| 68 |
+
new_size = (output_image.width * scale_factor, output_image.height * scale_factor)
|
| 69 |
+
return output_image.resize(new_size, resample=Image.NEAREST)
|
| 70 |
+
|
| 71 |
+
# --------------------
|
| 72 |
+
# UI
|
| 73 |
+
# --------------------
|
| 74 |
+
with gr.Blocks(title="CellVision AI - Segment the Malaria Cells from Blood smeers", theme=gr.themes.Soft()) as demo:
|
| 75 |
+
gr.Markdown("<h1 style='text-align:center; background-color:#1a1a1a; color:#00ffcc; padding:12px;'>CellVision AI - Grayscale Segmentation</h1>")
|
| 76 |
+
|
| 77 |
+
with gr.Accordion(" Documentation", open=False):
|
| 78 |
+
gr.Markdown(USAGE_GUIDELINES)
|
| 79 |
+
gr.Markdown("---")
|
| 80 |
+
gr.Markdown(INPUT_EXPLANATION)
|
| 81 |
+
gr.Markdown("---")
|
| 82 |
+
gr.Markdown(OUTPUT_EXPLANATION)
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
with gr.Row():
|
| 86 |
+
with gr.Column(scale=1):
|
| 87 |
+
gr.Markdown("## Step 1: Upload Blood Smear Image")
|
| 88 |
+
# Define Input component directly inside the column (No .render() needed)
|
| 89 |
+
input_image = gr.Image(type="pil", label="Upload Microscopy Image", width=600, height=600)
|
| 90 |
+
|
| 91 |
+
gr.Markdown("## Step 2: Click Submit for Segmentation")
|
| 92 |
+
with gr.Row():
|
| 93 |
+
submit_button = gr.Button("Analyze Image", variant="Secondary")
|
| 94 |
+
with gr.Column(scale=1):
|
| 95 |
+
gr.Markdown("## Output")
|
| 96 |
+
# Define Output component directly inside the column (No .render() needed)
|
| 97 |
+
output_image = gr.Image(type="pil", label="Gray & White Mask (3x)", width=600, height=600)
|
| 98 |
+
|
| 99 |
+
|
| 100 |
+
gr.Markdown("---")
|
| 101 |
+
gr.Markdown("## Example Images")
|
| 102 |
+
gr.Examples(
|
| 103 |
+
examples=["data/1.png", "data/2.png", "data/3.png"],
|
| 104 |
+
inputs=input_image,
|
| 105 |
+
outputs=output_image,
|
| 106 |
+
fn=segment_image,
|
| 107 |
+
cache_examples=False,
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
submit_button.click(segment_image, input_image, output_image)
|
| 112 |
+
|
| 113 |
+
if __name__ == "__main__":
|
| 114 |
+
demo.launch()
|
data/1.png
ADDED
|
Git LFS Details
|
data/2.png
ADDED
|
Git LFS Details
|
data/211.png
ADDED
|
Git LFS Details
|
data/3.png
ADDED
|
Git LFS Details
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
torchvision
|
| 3 |
+
transformers
|
| 4 |
+
gradio
|
| 5 |
+
Pillow
|
| 6 |
+
numpy<2
|
| 7 |
+
# gradio==3.50.2
|
| 8 |
+
# gradio-client==0.6.1
|