File size: 2,773 Bytes
7364060
f6210c2
3b62419
7077928
 
3b62419
 
7364060
7077928
3b62419
 
 
7364060
 
 
 
f6210c2
7364060
f6210c2
 
 
7364060
 
 
 
 
7077928
3b62419
 
f6210c2
7364060
f6210c2
7077928
 
7364060
7077928
f6210c2
 
 
3b62419
7364060
f6210c2
 
7077928
3b62419
f6210c2
7077928
3b62419
 
 
 
7077928
3b62419
f6210c2
3b62419
 
7077928
3b62419
 
779723a
7364060
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7077928
f6210c2
3b62419
 
 
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
from fastapi import FastAPI, File, UploadFile, HTTPException
from transformers import SegformerForSemanticSegmentation, SegformerImageProcessor
import torch
from PIL import Image
import numpy as np
import io
import base64
import logging

# Inizializza l'app FastAPI
app = FastAPI()

# Configura il logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# Carica il modello e il processore SegFormer
try:
    logger.info("Caricamento del modello SegFormer...")
    model = SegformerForSemanticSegmentation.from_pretrained("sayeed99/segformer-b3-fashion")
    processor = SegformerImageProcessor.from_pretrained("sayeed99/segformer-b3-fashion")
    model.to("cpu")  # Usa CPU per il free tier
    logger.info("Modello caricato con successo.")
except Exception as e:
    logger.error(f"Errore nel caricamento del modello: {str(e)}")
    raise RuntimeError(f"Errore nel caricamento del modello: {str(e)}")

# Funzione per segmentare l'immagine
def segment_image(image: Image.Image):
    # Prepara l'input per SegFormer
    logger.info("Preparazione dell'immagine per l'inferenza...")
    inputs = processor(images=image, return_tensors="pt").to("cpu")
    
    # Inferenza
    logger.info("Esecuzione dell'inferenza...")
    with torch.no_grad():
        outputs = model(**inputs)
        logits = outputs.logits
        
    # Post-processa la maschera
    logger.info("Post-processing della maschera...")
    mask = torch.argmax(logits, dim=1)[0]
    mask = mask.cpu().numpy()
    
    # Converti la maschera in immagine
    mask_img = Image.fromarray((mask * 255 / mask.max()).astype(np.uint8))
    
    # Converti la maschera in base64 per la risposta
    buffered = io.BytesIO()
    mask_img.save(buffered, format="PNG")
    mask_base64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
    
    # Annotazioni
    annotations = {"mask": mask.tolist(), "label": "fashion"}
    
    return mask_base64, annotations

# Endpoint API
@app.post("/segment")
async def segment_endpoint(file: UploadFile = File(...)):
    try:
        logger.info("Ricezione del file...")
        image_data = await file.read()
        image = Image.open(io.BytesIO(image_data)).convert("RGB")
        
        logger.info("Segmentazione dell'immagine...")
        mask_base64, annotations = segment_image(image)
        
        return {
            "mask": f"data:image/png;base64,{mask_base64}",
            "annotations": annotations
        }
    except Exception as e:
        logger.error(f"Errore nell'endpoint: {str(e)}")
        raise HTTPException(status_code=500, detail=f"Errore nell'elaborazione: {str(e)}")

# Per compatibilità con Hugging Face Spaces
if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=7860)