File size: 2,109 Bytes
ae522ad
 
 
 
eb430dd
ae522ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
import sys

# NOTE: Deploy ONLY: Add the src directory to the Python path
sys.path.append(os.path.join(os.path.dirname(__file__), "src"))

from pathlib import Path
from PIL import Image
import gradio as gr

from mwm.utils.common import load_json
from mwm.components.inference import inference
from mwm.components.visualization import (
    colorize_label_map,
    colorize_mask_2d
)

def segment(image: Image.Image):

    # Dump image
    # TODO: add normalization before allow user to upload their own image
    image_savepath = "input_image.png"
    image.save(image_savepath)


    labels_pred, mask_pred = inference(
        config_path="config.json",
        image_path=image_savepath
    )

    mask_pred_colored = colorize_mask_2d(mask_pred) # TODO: show later
    labels_pred_colored = colorize_label_map(labels_pred)

    # Clean up
    os.remove(image_savepath)

    return Image.fromarray(labels_pred_colored)


# Load config
config = load_json(Path("config.json"))

# Gradio Interface
demo = gr.Interface(
    fn=segment,
    inputs=gr.Image(type="pil"),
    outputs=gr.Image(type="pil"),
    title="🔬 Microscopy Image Segmentation with Machine Learning",
    description="""
    ## Work-in-progress project exploring state-of-the-art (SOTA) ML models for nuclei (cell body) segmentation in microscopy images. 
    Check out full details in this repo: [microscopy-with-ml](https://github.com/Huangt19150/microscopy-with-ml)

    ## 📋 Quick Start Guide:
    1. Try one of the **examples** below to run nuclei segmentation.
    2. (Upload your own image is not yet supported. Coming soon!)

    ## 📖 Reference:
    1. Dataset: [BBBC039v1](https://bbbc.broadinstitute.org/BBBC039) Caicedo et al. 2018, available from the Broad Bioimage Benchmark Collection [Ljosa et al., Nature Methods, 2012](https://www.nature.com/articles/nmeth.2083).
    2. Method improved from [topcoders](https://www.kaggle.com/competitions/data-science-bowl-2018/discussion/54741)
    
    """,
    examples=config.example_images,
    allow_flagging="never"
)

if __name__ == "__main__":
    demo.launch()