Spaces:
Sleeping
Sleeping
muhammadhamza-stack commited on
Commit Β·
d49a4fc
1
Parent(s): 45b4aee
update app
Browse files
app.py
CHANGED
|
@@ -7,37 +7,32 @@ import numpy as np
|
|
| 7 |
# --- Documentation Strings ---
|
| 8 |
|
| 9 |
USAGE_GUIDELINES = """
|
| 10 |
-
## 1. Quick Start Guide: HemaScan Pro
|
| 11 |
|
| 12 |
-
HemaScan Pro
|
| 13 |
|
| 14 |
-
1.
|
| 15 |
-
2.
|
| 16 |
-
3.
|
| 17 |
-
4. **View Results** β A high-visibility color segmentation mask will appear.
|
| 18 |
"""
|
| 19 |
|
| 20 |
INPUT_EXPLANATION = """
|
| 21 |
## 2. Expected Inputs
|
| 22 |
|
| 23 |
-
|
|
| 24 |
-
|-------
|
| 25 |
-
| Upload Image |
|
| 26 |
|
| 27 |
-
β
|
| 28 |
"""
|
| 29 |
|
| 30 |
OUTPUT_EXPLANATION = """
|
| 31 |
-
## 3.
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
β’
|
| 36 |
-
β’
|
| 37 |
-
β’ Mask is enlarged by **400% (4Γ)** for improved visibility.
|
| 38 |
-
|
| 39 |
-
### Example Testing
|
| 40 |
-
Click any example image below to automatically run segmentation.
|
| 41 |
"""
|
| 42 |
|
| 43 |
# --------------------
|
|
@@ -49,15 +44,6 @@ model = SegformerForSemanticSegmentation.from_pretrained(
|
|
| 49 |
)
|
| 50 |
model.eval()
|
| 51 |
|
| 52 |
-
# Create vibrant color palette
|
| 53 |
-
def create_color_palette(num_classes=150):
|
| 54 |
-
np.random.seed(42)
|
| 55 |
-
palette = np.random.randint(0, 255, size=(num_classes, 3))
|
| 56 |
-
palette[0] = [0, 0, 0]
|
| 57 |
-
return palette
|
| 58 |
-
|
| 59 |
-
palette = create_color_palette()
|
| 60 |
-
|
| 61 |
def segment_image(input_image):
|
| 62 |
if input_image is None:
|
| 63 |
gr.Warning("Please upload an image.")
|
|
@@ -71,11 +57,10 @@ def segment_image(input_image):
|
|
| 71 |
logits = outputs.logits
|
| 72 |
pred_mask = torch.argmax(logits, dim=1)[0].cpu().numpy()
|
| 73 |
|
| 74 |
-
#
|
| 75 |
-
|
| 76 |
-
colored_mask = colored_mask.astype(np.uint8)
|
| 77 |
|
| 78 |
-
output_image = Image.fromarray(
|
| 79 |
|
| 80 |
# Scale 4x
|
| 81 |
scale_factor = 4
|
|
@@ -86,21 +71,31 @@ def segment_image(input_image):
|
|
| 86 |
# UI
|
| 87 |
# --------------------
|
| 88 |
with gr.Blocks(title="HemaScan Pro") as demo:
|
| 89 |
-
gr.Markdown("<h1 style='text-align:center; background:linear-gradient(90deg,#4facfe,#00f2fe); color:white; padding:10px;'>HemaScan Pro -
|
| 90 |
|
| 91 |
-
with gr.Accordion("
|
| 92 |
gr.Markdown(USAGE_GUIDELINES)
|
| 93 |
gr.Markdown("---")
|
| 94 |
gr.Markdown(INPUT_EXPLANATION)
|
| 95 |
gr.Markdown("---")
|
| 96 |
gr.Markdown(OUTPUT_EXPLANATION)
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
gr.Examples(
|
| 103 |
-
examples=["data/1.
|
| 104 |
inputs=input_image,
|
| 105 |
outputs=output_image,
|
| 106 |
fn=segment_image,
|
|
|
|
| 7 |
# --- Documentation Strings ---
|
| 8 |
|
| 9 |
USAGE_GUIDELINES = """
|
| 10 |
+
## 1. Quick Start Guide: HemaScan Pro (Binary Mask)
|
| 11 |
|
| 12 |
+
HemaScan Pro generates a high-contrast black & white segmentation mask.
|
| 13 |
|
| 14 |
+
1. Upload a blood smear image (JPG/PNG).
|
| 15 |
+
2. Click "Run Segmentation".
|
| 16 |
+
3. View the generated binary mask.
|
|
|
|
| 17 |
"""
|
| 18 |
|
| 19 |
INPUT_EXPLANATION = """
|
| 20 |
## 2. Expected Inputs
|
| 21 |
|
| 22 |
+
| Field | Requirement |
|
| 23 |
+
|-------|------------|
|
| 24 |
+
| Upload Image | JPG / PNG blood smear image |
|
| 25 |
|
| 26 |
+
β Automatically resized to 512Γ512.
|
| 27 |
"""
|
| 28 |
|
| 29 |
OUTPUT_EXPLANATION = """
|
| 30 |
+
## 3. Output Description (Black & White Mask)
|
| 31 |
|
| 32 |
+
β’ Background = White
|
| 33 |
+
β’ Detected Regions = Black
|
| 34 |
+
β’ Enlarged by 400% (4Γ) for clarity
|
| 35 |
+
β’ Clean binary medical-style visualization
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
"""
|
| 37 |
|
| 38 |
# --------------------
|
|
|
|
| 44 |
)
|
| 45 |
model.eval()
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
def segment_image(input_image):
|
| 48 |
if input_image is None:
|
| 49 |
gr.Warning("Please upload an image.")
|
|
|
|
| 57 |
logits = outputs.logits
|
| 58 |
pred_mask = torch.argmax(logits, dim=1)[0].cpu().numpy()
|
| 59 |
|
| 60 |
+
# Convert to binary mask (object vs background)
|
| 61 |
+
binary_mask = np.where(pred_mask == 0, 255, 0).astype(np.uint8)
|
|
|
|
| 62 |
|
| 63 |
+
output_image = Image.fromarray(binary_mask)
|
| 64 |
|
| 65 |
# Scale 4x
|
| 66 |
scale_factor = 4
|
|
|
|
| 71 |
# UI
|
| 72 |
# --------------------
|
| 73 |
with gr.Blocks(title="HemaScan Pro") as demo:
|
| 74 |
+
gr.Markdown("<h1 style='text-align:center; background:linear-gradient(90deg,#4facfe,#00f2fe); color:white; padding:10px;'>HemaScan Pro - Binary Segmentation</h1>")
|
| 75 |
|
| 76 |
+
with gr.Accordion(" Documentation", open=False):
|
| 77 |
gr.Markdown(USAGE_GUIDELINES)
|
| 78 |
gr.Markdown("---")
|
| 79 |
gr.Markdown(INPUT_EXPLANATION)
|
| 80 |
gr.Markdown("---")
|
| 81 |
gr.Markdown(OUTPUT_EXPLANATION)
|
| 82 |
|
| 83 |
+
gr.Markdown("## Step 1: Upload Blood Smear Image")
|
| 84 |
+
with gr.Row():
|
| 85 |
+
with gr.Column(scale=1):
|
| 86 |
+
# Define Input component directly inside the column (No .render() needed)
|
| 87 |
+
input_image = gr.Image(type="pil", label="Step 1: Upload Blood Smear Image", width=600, height=600)
|
| 88 |
+
|
| 89 |
+
gr.Markdown("## Step 2: Click Submit for Segmentation")
|
| 90 |
+
with gr.Row():
|
| 91 |
+
submit_button = gr.Button("Submit for Segmentation", variant="primary")
|
| 92 |
+
gr.Markdown("## Output")
|
| 93 |
+
with gr.Row(scale=2):
|
| 94 |
+
# Define Output component directly inside the column (No .render() needed)
|
| 95 |
+
output_image = gr.Image(type="pil", label="Step 3: Predicted Masks", width=600, height=600)
|
| 96 |
|
| 97 |
gr.Examples(
|
| 98 |
+
examples=["data/1.jpg", "data/2.jpg", "data/3.jpg"],
|
| 99 |
inputs=input_image,
|
| 100 |
outputs=output_image,
|
| 101 |
fn=segment_image,
|