Update app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from transformers import ViTFeatureExtractor, ViTForImageClassification
|
4 |
-
from PIL import Image, ImageDraw, ImageFont
|
5 |
-
import io
|
6 |
import numpy as np
|
7 |
|
8 |
# Carregue o modelo ViT
|
@@ -10,18 +8,6 @@ model_name = "mrm8488/vit-base-patch16-224_finetuned-kvasirv2-colonoscopy"
|
|
10 |
feature_extractor = ViTFeatureExtractor.from_pretrained(model_name)
|
11 |
model = ViTForImageClassification.from_pretrained(model_name)
|
12 |
|
13 |
-
# Mapeamento de classe ID para rótulo
|
14 |
-
id2label = {
|
15 |
-
"0": "dyed-lifted-polyps",
|
16 |
-
"1": "dyed-resection-margins",
|
17 |
-
"2": "esophagitis",
|
18 |
-
"3": "normal-cecum",
|
19 |
-
"4": "normal-pylorus",
|
20 |
-
"5": "normal-z-line",
|
21 |
-
"6": "polyps",
|
22 |
-
"7": "ulcerative-colitis"
|
23 |
-
}
|
24 |
-
|
25 |
# Função para classificar a imagem
|
26 |
def classify_image(input_image):
|
27 |
# Pré-processar a imagem usando o extrator de características
|
@@ -30,28 +16,19 @@ def classify_image(input_image):
|
|
30 |
outputs = model(**inputs)
|
31 |
# Obter a classe prevista
|
32 |
predicted_class_id = torch.argmax(outputs.logits, dim=1).item()
|
33 |
-
#
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
text_width, text_height = draw.textsize(text, font=font)
|
47 |
-
|
48 |
-
x = (width - text_width) // 2
|
49 |
-
y = (height - text_height) // 2
|
50 |
-
draw.text((x, y), text, fill='white', font=font)
|
51 |
-
|
52 |
-
# Converter a imagem resultante de volta para numpy
|
53 |
-
result_image = np.array(image)
|
54 |
-
return result_image
|
55 |
|
56 |
# Criar uma interface Gradio
|
57 |
interface = gr.Interface(
|
@@ -59,7 +36,7 @@ interface = gr.Interface(
|
|
59 |
inputs=gr.inputs.Image(type="numpy", label="Carregar uma imagem"),
|
60 |
outputs=gr.outputs.Image(type="numpy", label="Resultado"),
|
61 |
title="Classificador de Imagem ViT",
|
62 |
-
description="Esta aplicação Gradio permite classificar imagens usando um modelo Vision Transformer (ViT).
|
63 |
)
|
64 |
|
65 |
# Iniciar a aplicação Gradio
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from transformers import ViTFeatureExtractor, ViTForImageClassification
|
|
|
|
|
4 |
import numpy as np
|
5 |
|
6 |
# Carregue o modelo ViT
|
|
|
8 |
feature_extractor = ViTFeatureExtractor.from_pretrained(model_name)
|
9 |
model = ViTForImageClassification.from_pretrained(model_name)
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
# Função para classificar a imagem
|
12 |
def classify_image(input_image):
|
13 |
# Pré-processar a imagem usando o extrator de características
|
|
|
16 |
outputs = model(**inputs)
|
17 |
# Obter a classe prevista
|
18 |
predicted_class_id = torch.argmax(outputs.logits, dim=1).item()
|
19 |
+
# Mapeamento de classe ID para rótulo
|
20 |
+
id2label = {
|
21 |
+
0: "dyed-lifted-polyps",
|
22 |
+
1: "dyed-resection-margins",
|
23 |
+
2: "esophagitis",
|
24 |
+
3: "normal-cecum",
|
25 |
+
4: "normal-pylorus",
|
26 |
+
5: "normal-z-line",
|
27 |
+
6: "polyps",
|
28 |
+
7: "ulcerative-colitis"
|
29 |
+
}
|
30 |
+
predicted_class_label = id2label.get(predicted_class_id, "Desconhecido")
|
31 |
+
return input_image
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
# Criar uma interface Gradio
|
34 |
interface = gr.Interface(
|
|
|
36 |
inputs=gr.inputs.Image(type="numpy", label="Carregar uma imagem"),
|
37 |
outputs=gr.outputs.Image(type="numpy", label="Resultado"),
|
38 |
title="Classificador de Imagem ViT",
|
39 |
+
description="Esta aplicação Gradio permite classificar imagens usando um modelo Vision Transformer (ViT). Nenhuma sobreposição de texto é aplicada."
|
40 |
)
|
41 |
|
42 |
# Iniciar a aplicação Gradio
|