File size: 1,575 Bytes
101ec35
beaf59e
f312a7b
 
 
beaf59e
 
 
 
 
 
101ec35
 
5ec5751
 
101ec35
 
 
5ec5751
 
8cc2f16
5ec5751
8cc2f16
5ec5751
0d524f5
5ec5751
 
 
 
 
 
519b727
5ec5751
beaf59e
5ec5751
 
beaf59e
5ec5751
 
 
 
8cc2f16
13124d8
beaf59e
 
13124d8
 
 
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
from PIL import Image
import tempfile
import gradio as gr

# Resto do código aqui...

def preprocess_image(image):
    temp_file = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
    temp_file.write(image.read())
    temp_file.close()
    return temp_file.name

def classify_image(image_path, model_name):
    model_config = next(m for m in models if m["name"] == model_name)
    model = tf.keras.models.load_model(model_name)
    image = Image.open(image_path).convert("RGB")
    image = image.resize((model_config["size"], model_config["size"]))
    image = np.array(image) / 255.0
    input_image = np.expand_dims(image, axis=0)
    prediction = model.predict(input_image).flatten()
    if len(prediction) > 1:
        probability = 100 * np.exp(prediction[0]) / (np.exp(prediction[0]) + np.exp(prediction[1]))
    else:
        probability = round(100. / (1 + np.exp(-prediction[0])), 2)
    if probability > 45:
        label = "Glaucoma"
    elif probability > 25:
        label = "Unclear"
    else:
        label = "Not glaucoma"
    return label, probability

inputs = [
    gr.inputs.Image(label="Eye image"),
    gr.inputs.Dropdown(choices=[m["name"] for m in models], label="Model"),
]

outputs = [
    gr.outputs.Textbox(label="Predicted label"),
    gr.outputs.Textbox(label="Probability of glaucoma (0-100)"),
]

examples = [
    [preprocess_image(open("example_image.jpg", "rb")), "my_model.h5"],
    [preprocess_image(open("example_image_2.jpg", "rb")), "my_model_2.h5"]
]

gr.Interface(classify_image, inputs, outputs, examples=examples).launch()