Py.Cafe

panel-org/

basic-image-classifier

A basic interface for image classification

DocsPricing
  • app.py
  • requirements.txt
app.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import random
from io import BytesIO
from time import sleep

import hvplot.pandas
import pandas as pd
import requests
from PIL import Image

import panel as pn

IMAGE_DIM = 350

pn.extension(design="material", sizing_mode="stretch_width")

## Transformations


@pn.cache
def get_pil_image(url):
    response = requests.get(url)
    return Image.open(BytesIO(response.content))


@pn.cache
def get_plot(label):
    data = pd.Series(label).sort_values()
    return data.hvplot.barh(title="Prediction", ylim=(0, 100)).opts(default_tools=[])


## Custom Components


def get_label_view(fn, image: Image):
    return get_plot(fn(image))


def get_image_button(url, image_pane):
    button = pn.widgets.Button(
        width=100,
        height=100,
        stylesheets=[
            f"button {{background-image: url({url});background-size: cover;}}"
        ],
    )
    pn.bind(handle_example_click, button, url, image_pane, watch=True)
    return button


## Event Handlers


def handle_example_click(event, url, image_pane):
    image_pane.object = get_pil_image(url)


def handle_file_upload(value, image_pane):
    file = BytesIO(value)
    image_pane.object = Image.open(file)


def image_classification_interface(fn, examples):

    ## State

    image_view = pn.pane.Image(
        get_pil_image(examples[0]),
        height=IMAGE_DIM,
        width=IMAGE_DIM,
        fixed_aspect=False,
        margin=0,
    )

    label_view = pn.pane.JSON()

    ## Inputs

    file_input = pn.widgets.FileInput(
        accept=".png,.jpeg",
    )
    pn.bind(handle_file_upload, file_input, image_view, watch=True)
    file_input_component = pn.Column("### Upload Image", file_input)

    examples_input_component = pn.Column(
        "### Examples", pn.Row(*(get_image_button(url, image_view) for url in examples))
    )

    ## Views

    label_view = pn.Row(
        pn.panel(
            pn.bind(get_label_view, fn=fn, image=image_view.param.object),
            defer_load=True,
            loading_indicator=True,
            height=IMAGE_DIM,
            width=IMAGE_DIM,
        )
    )

    ## Layouts

    input_component = pn.Column(
        "# Input",
        image_view,
        file_input_component,
        examples_input_component,
        width=IMAGE_DIM,
        margin=10,
    )

    output_component = pn.Column(
        "# Output",
        label_view,
        width=IMAGE_DIM,
        margin=10,
    )
    return pn.FlexBox(input_component, output_component)


def predict(image: Image):
    # Replace with your own image classification model
    sleep(1.5)
    a = random.uniform(0, 100)
    b = random.uniform(0, 100 - a)
    c = 100 - a - b
    return {
        "Wind Turbine": a,
        "Solar Panel": b,
        "Battery Storage": c,
    }


EXAMPLES = [
    # Replace with your own examples
    "https://assets.holoviz.org/panel/tutorials/wind_turbine.png",
    "https://assets.holoviz.org/panel/tutorials/solar_panel.png",
    "https://assets.holoviz.org/panel/tutorials/battery_storage.png",
]

image_classification_interface(fn=predict, examples=EXAMPLES).servable()