File size: 1,708 Bytes
0a1fc1e
 
 
 
 
 
 
 
 
 
fc0e609
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
title: SAM Server
emoji: 🖼️
colorFrom: blue
colorTo: purple
sdk: docker
app_port: 7860
pinned: false
---

# SAM Backend per Segmentazione Immagini

Server FastAPI per la segmentazione di immagini usando SAM (Segment Anything Model) di Meta.

## Requisiti

- Python 3.8+
- CUDA (opzionale, per GPU acceleration)

## Installazione

```bash
cd sam_backend

# Crea un ambiente virtuale (consigliato)
python -m venv venv
.\venv\Scripts\activate  # Windows
# source venv/bin/activate  # Linux/Mac

# Installa le dipendenze
pip install -r requirements.txt
```

## Download del Modello

Scarica uno dei checkpoint SAM da [GitHub](https://github.com/facebookresearch/segment-anything#model-checkpoints):

| Modello | Dimensione | Link |
|---------|------------|------|
| ViT-B | ~375MB | [sam_vit_b_01ec64.pth](https://dl.fbaipublicfiles.com/segment_anything/sam_vit_b_01ec64.pth) |
| ViT-L | ~1.2GB | [sam_vit_l_0b3195.pth](https://dl.fbaipublicfiles.com/segment_anything/sam_vit_l_0b3195.pth) |
| ViT-H | ~2.5GB | [sam_vit_h_4b8939.pth](https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth) |

Posiziona il file nella cartella `sam_backend/checkpoints/`.

## Avvio del Server

```bash
python sam_server.py
```

Il server sarà disponibile su `http://localhost:8000`.

## API Endpoints

- `GET /health` - Verifica stato del server
- `GET /models` - Lista modelli disponibili
- `POST /load-model/{model_type}` - Carica un modello (vit_b, vit_l, vit_h)
- `POST /segment-with-image` - Segmenta con punti
- `POST /segment-auto` - Segmentazione automatica

## Utilizzo con Next.js

Assicurati che il server Python sia in esecuzione prima di usare SAM nella pagina `/generation` dell'app Next.js.