File size: 2,764 Bytes
ab0f059
89dcdea
ab0f059
 
 
 
 
1d4b8d9
 
 
 
 
 
ab0f059
 
1d4b8d9
 
 
ab0f059
1d4b8d9
ab0f059
 
1d4b8d9
ab0f059
 
 
 
 
 
 
 
 
1d4b8d9
ab0f059
1d4b8d9
ab0f059
 
1d4b8d9
 
ab0f059
89dcdea
1d4b8d9
 
ab0f059
 
1d4b8d9
ab0f059
1d4b8d9
ab0f059
89dcdea
ab0f059
 
1d4b8d9
ab0f059
1d4b8d9
ab0f059
 
 
 
 
 
89dcdea
ab0f059
1d4b8d9
ab0f059
 
1d4b8d9
 
ab0f059
 
 
 
1d4b8d9
ab0f059
 
 
1d4b8d9
ab0f059
 
1d4b8d9
 
 
 
 
ab0f059
 
 
 
 
 
1d4b8d9
ab0f059
 
 
89dcdea
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
import gradio as gr
from transformers import pipeline
import torch
from PIL import Image
import numpy as np

WOUND_TYPES = {
    "stage1": "Estágio 1 - Lesão Superficial",
    "stage2": "Estágio 2 - Lesão Parcial",
    "stage3": "Estágio 3 - Lesão Profunda",
    "stage4": "Estágio 4 - Lesão Grave",
    "unstageable": "Não Classificável",
    "healthy": "Pele Saudável"
}

def load_model():
    # Usando um modelo público do Hugging Face para classificação de imagens
    classifier = pipeline(
        "image-classification",
        model="google/vit-base-patch16-224",
        device=0 if torch.cuda.is_available() else -1
    )
    return classifier

def preprocess_image(image):
    if isinstance(image, np.ndarray):
        image = Image.fromarray(image)
    image = image.convert('RGB')
    return image

def classify_wound(image):
    if image is None:
        return None
    
    classifier = load_model()
    processed_image = preprocess_image(image)
    
    # Classificação da imagem
    results = classifier(processed_image)
    
    # Formatando resultados
    formatted_results = []
    for result in results:
        label = result['label'].replace('_', ' ').title()
        score = result['score']
        formatted_results.append((label, score))
    
    return formatted_results

# Interface Gradio
with gr.Blocks(theme=gr.themes.Soft()) as demo:
    gr.Markdown("""
    # 🏥 Classificador de Imagens Médicas
    
    Sistema de classificação de imagens usando Vision Transformer (ViT).
    """)
    
    with gr.Row():
        with gr.Column():
            input_image = gr.Image(
                label="Upload da Imagem",
                type="pil"
            )
            submit_btn = gr.Button("Analisar Imagem", variant="primary")
        
        with gr.Column():
            output = gr.Label(
                label="Classificação",
                num_top_classes=3
            )
    
    with gr.Row():
        with gr.Accordion("Informações", open=False):
            gr.Markdown("""
            ### Recomendações para Melhores Resultados:
            1. Use imagens bem iluminadas
            2. Capture a imagem em um ângulo perpendicular
            3. Mantenha um fundo neutro e limpo
            4. Evite sombras ou reflexos excessivos
            
            ### Observações:
            - Este é um modelo de classificação geral
            - Os resultados são aproximações e não substituem avaliação médica
            - Consulte sempre um profissional de saúde para diagnóstico
            """)
    
    # Configurando eventos
    submit_btn.click(
        fn=classify_wound,
        inputs=input_image,
        outputs=output
    )

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