Spaces:
Sleeping
Sleeping
muhammadhamza-stack commited on
Commit ·
e69f86c
1
Parent(s): 24a77a2
initial commit
Browse files- .gitattributes +2 -0
- app.py +72 -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,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
import os
|
| 7 |
+
|
| 8 |
+
# --- Documentation Strings ---
|
| 9 |
+
|
| 10 |
+
USAGE_GUIDELINES = """
|
| 11 |
+
## Quick Start: HemaScan Segmentation
|
| 12 |
+
Upload an image of a blood smear to generate a segmentation mask.
|
| 13 |
+
"""
|
| 14 |
+
|
| 15 |
+
INPUT_EXPLANATION = "Upload a JPG or PNG blood smear image (512x512 auto-resized)."
|
| 16 |
+
OUTPUT_EXPLANATION = "Predicted grayscale mask highlighting detected objects (scaled 4× for clarity)."
|
| 17 |
+
|
| 18 |
+
# --------------------
|
| 19 |
+
# Core Pipeline Functions
|
| 20 |
+
# --------------------
|
| 21 |
+
processor = SegformerImageProcessor(do_reduce_labels=False)
|
| 22 |
+
model = SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512")
|
| 23 |
+
model.eval()
|
| 24 |
+
|
| 25 |
+
def segment_image(input_image):
|
| 26 |
+
if input_image is None:
|
| 27 |
+
gr.Warning("Upload an image.")
|
| 28 |
+
return None
|
| 29 |
+
|
| 30 |
+
inputs = processor(images=input_image, return_tensors="pt")
|
| 31 |
+
with torch.no_grad():
|
| 32 |
+
outputs = model(**inputs)
|
| 33 |
+
logits = outputs.logits
|
| 34 |
+
|
| 35 |
+
pred_mask = torch.argmax(logits, dim=1)[0].cpu().numpy()
|
| 36 |
+
num_classes = logits.shape[1]
|
| 37 |
+
normalized_mask = (pred_mask * (255 // num_classes)).astype(np.uint8)
|
| 38 |
+
output_image = Image.fromarray(normalized_mask)
|
| 39 |
+
|
| 40 |
+
# Bigger mask (4x)
|
| 41 |
+
scale_factor = 4
|
| 42 |
+
new_size = (output_image.width * scale_factor, output_image.height * scale_factor)
|
| 43 |
+
return output_image.resize(new_size, resample=Image.NEAREST)
|
| 44 |
+
|
| 45 |
+
# --------------------
|
| 46 |
+
# Gradio UI
|
| 47 |
+
# --------------------
|
| 48 |
+
with gr.Blocks(title="HemaScan Segmentation Tool") as demo:
|
| 49 |
+
gr.Markdown("<h1 style='text-align:center; background: linear-gradient(90deg, #4facfe 0%, #00f2fe 100%); padding: 10px; color:white;'>HemaScan Segmentation Tool</h1>")
|
| 50 |
+
gr.Markdown("Analyze blood smear images and generate segmentation masks.")
|
| 51 |
+
|
| 52 |
+
with gr.Accordion("Tips & Guidelines", open=False):
|
| 53 |
+
gr.Markdown(USAGE_GUIDELINES)
|
| 54 |
+
gr.Markdown(INPUT_EXPLANATION)
|
| 55 |
+
gr.Markdown(OUTPUT_EXPLANATION)
|
| 56 |
+
|
| 57 |
+
input_image = gr.Image(type="pil", label="Upload Blood Smear Image")
|
| 58 |
+
submit_button = gr.Button("Segment Image", variant="primary")
|
| 59 |
+
output_image = gr.Image(type="pil", label="Predicted Mask (4x)")
|
| 60 |
+
|
| 61 |
+
gr.Examples(
|
| 62 |
+
examples=["data/sample_a.png", "data/sample_b.png", "data/sample_c.png"],
|
| 63 |
+
inputs=[input_image],
|
| 64 |
+
outputs=[output_image],
|
| 65 |
+
fn=segment_image,
|
| 66 |
+
cache_examples=False
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
submit_button.click(fn=segment_image, inputs=input_image, outputs=output_image)
|
| 70 |
+
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
+
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
|