File size: 5,680 Bytes
22a70b4
d82a135
22a70b4
d82a135
 
22a70b4
 
 
 
d82a135
22a70b4
d82a135
22a70b4
d82a135
22a70b4
d82a135
 
 
 
22a70b4
 
 
 
 
 
d82a135
22a70b4
d82a135
22a70b4
 
 
 
d82a135
 
22a70b4
 
 
 
 
 
 
 
 
d82a135
 
 
 
 
 
 
 
 
22a70b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d82a135
22a70b4
 
d82a135
 
22a70b4
 
d82a135
 
 
22a70b4
 
d82a135
22a70b4
 
d82a135
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22a70b4
d82a135
 
 
22a70b4
 
d82a135
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22a70b4
 
d82a135
22a70b4
d82a135
22a70b4
d82a135
22a70b4
d82a135
 
 
 
 
22a70b4
 
 
 
 
d82a135
22a70b4
 
d82a135
 
22a70b4
 
 
d82a135
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
---
title: GP-Tea Skin Analysis
emoji: πŸ”¬
colorFrom: green
colorTo: blue
sdk: docker
app_port: 7860
---

# GP-Tea Skin Analysis API

An AI-powered skin condition analysis service that provides medical insights from skin images using deep learning.

## Features

- **Image Classification**: Advanced skin condition detection using TensorFlow EfficientNetV2S
- **Medical Analysis**: Comprehensive skin health assessment with confidence scoring
- **Real-time Processing**: Fast inference with optimized model performance
- **REST API**: Easy integration with mobile and web applications

---

## Repository Structure

```
api_image/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ main.py                # FastAPI application with JSON endpoints
β”‚   β”œβ”€β”€ predict.py             # Image preprocessing and prediction logic
β”‚   β”œβ”€β”€ model_loader.py        # Loads the trained EfficientNetV2S model
β”‚   β”œβ”€β”€ model/                 # Local model storage (auto-downloaded if missing)
β”‚   β”‚   └── efficientnetv2s.h5 # Pretrained model file
β”‚   └── uploads/               # Temporary upload directory (auto-created)
β”œβ”€β”€ test_images/               # Sample images for testing the API
β”œβ”€β”€ requirements.txt           # Python dependencies
β”œβ”€β”€ Dockerfile                 # Docker setup for deployment
└── README.md                  # Project documentation (this file)
```

---

## Features

- **JSON API Endpoints**: Clean REST API for Flutter integration
- **CORS Enabled**: Ready for mobile app cross-origin requests
- **Multi-class Classification**: Supports medical image classification
- **Confidence Scores**: Returns probability percentages for predictions
- **File Upload Support**: Handles image file uploads with validation
- **Error Handling**: Proper HTTP status codes and error messages
- **Health Check**: Monitoring endpoint for service status
- **Temperature Scaling**: Calibrated probabilities for reliable predictions
- **Automatic Cleanup**: Temporary files are automatically removed

---

## Model Details

- Base Model: EfficientNetV2S (20.5 million parameters)
- Dataset: [HAM10000](https://dataverse.harvard.edu/dataset.xhtml?persistentId=doi:10.7910/DVN/DBW86T)
- Calibration: Temperature Scaling (optimal T=2.77)
- Performance:
  - Accuracy: 0.88
  - Macro F1-score: 0.80
  - Expected Calibration Error (ECE): 0.022 (after T-scaling)

For full technical details, see the Model Technical Information section in the app.

---

## Model Download from Hugging Face

The model file `efficientnetv2s.h5` is hosted on [Hugging Face Hub](https://huggingface.co/Miguel764/efficientnetv2s-skin-cancer-classifier) and is automatically downloaded the first time the app runs.

How it works:

- Expected local path: `app/model/efficientnetv2s.h5`
- On startup, `app/model_loader.py` checks for the file
- If missing, it is downloaded via `huggingface_hub` and saved to `app/model/`
- Subsequent runs load the local copy

Note for Docker users: When running inside a container, the downloaded model is stored inside the container’s filesystem. Mount a volume if you need to persist it on the host.

Manual download link (optional): https://huggingface.co/Miguel764/efficientnetv2s-skin-cancer-classifier

---

## Installation & Usage

### 1) Install dependencies

```sh
cd Image_classification/api_image
pip install -r requirements.txt
```

**Requirements**: Python 3.8 and TensorFlow 2.10.0 (GPU recommended). (very important!!!!! Must be Python 3.8)

### 2) Run the API server

```sh
  uvicorn app.main:app --host 0.0.0.0 --port 8003 --reload
```

The API will be available at http://localhost:8003

Access the API documentation at http://localhost:8003/docs

### 3) Test the API

Use the sample images in `test_images/` folder to test the endpoints.

---

## API Endpoints

### Health Check

- **GET** `/health` - Service health status

### Image Classification

- **POST** `/api/classify` - Upload and classify medical image

#### Example Request (cURL):

```sh
curl -X POST "http://localhost:8000/api/classify" \
     -H "accept: application/json" \
     -H "Content-Type: multipart/form-data" \
     -F "file=@test_images/sample.jpg"
```

#### Example Response:

```json
{
  "success": true,
  "prediction": {
    "top_prediction": {
      "label": "Melanoma",
      "confidence": 0.95,
      "confidence_percent": "95.00%"
    },
    "all_predictions": [
      {
        "label": "Melanoma",
        "confidence": 0.95,
        "confidence_percent": "95.00%"
      },
      {
        "label": "Benign Nevus",
        "confidence": 0.03,
        "confidence_percent": "3.00%"
      }
    ]
  }
}
```

---

## Flutter Integration

### Base URL Configuration

```dart
class ApiConfig {
  static const String imageApiUrl = "http://localhost:8000";
}
```

---

## Docker Deployment

Build and run the API in a container:

```sh
docker build -t medical-image-api .
docker run -p 8000:8000 medical-image-api
```

---

## Error Handling

The API returns consistent error responses:

```json
{
  "detail": "File must be an image"
}
```

Common HTTP status codes:

- `200` - Success
- `400` - Bad Request (invalid file, missing data)
- `422` - Unprocessable Entity (validation error)
- `500` - Internal Server Error (classification failed)

---

## Technical Notes

- **Backend Only**: No web interface, pure JSON API
- **CORS Enabled**: Ready for mobile app integration
- **File Validation**: Checks file type and validity
- **Temporary Storage**: Uploaded files are automatically cleaned up
- **Model Auto-Download**: Model downloads from Hugging Face on first run
- **Python 3.8 Compatible**: Uses TensorFlow 2.10.0 for compatibility