Upload 3 files
Browse files- api2.py +100 -0
- keras_model.h5 +3 -0
- labels.txt +5 -0
api2.py
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile
|
2 |
+
from tensorflow.keras.models import load_model # type: ignore
|
3 |
+
from PIL import Image, ImageOps
|
4 |
+
from scipy.stats import entropy
|
5 |
+
import numpy as np
|
6 |
+
import io
|
7 |
+
import time
|
8 |
+
from tensorflow.keras.layers import DepthwiseConv2D # type: ignore
|
9 |
+
|
10 |
+
|
11 |
+
# Initialiser l'application FastAPI
|
12 |
+
app = FastAPI(
|
13 |
+
title="Dental Diseases Prediction API",
|
14 |
+
description="Cette API prédit le type de maladie bucco-dentaire à partir d'images.",
|
15 |
+
version="1.0.0"
|
16 |
+
)
|
17 |
+
|
18 |
+
|
19 |
+
# Route pour accêder a la page d'accueil
|
20 |
+
@app.get("/")
|
21 |
+
def read_root():
|
22 |
+
return {"message": "API Dental Diseases Prediction"}
|
23 |
+
|
24 |
+
def custom_depthwise_conv2d(*args, **kwargs):
|
25 |
+
if 'groups' in kwargs:
|
26 |
+
del kwargs['groups'] # Retirer 'groups'
|
27 |
+
return DepthwiseConv2D(*args, **kwargs)
|
28 |
+
|
29 |
+
# Charger le modèle
|
30 |
+
model = load_model("keras_model.h5", custom_objects={'DepthwiseConv2D': custom_depthwise_conv2d}, compile=False)
|
31 |
+
|
32 |
+
# Charger les étiquettes
|
33 |
+
with open("labels.txt", "r") as file:
|
34 |
+
class_names = [line.strip() for line in file.readlines()]
|
35 |
+
|
36 |
+
# Fonction pour prétraiter l'image
|
37 |
+
def preprocess(image):
|
38 |
+
# Redimensionner l'image à 224x224
|
39 |
+
image = ImageOps.fit(image, (224, 224), Image.Resampling.LANCZOS)
|
40 |
+
|
41 |
+
# Convertir l'image en tableau numpy et s'assurer qu'elle a 3 canaux
|
42 |
+
image_array = np.asarray(image.convert("RGB"), dtype=np.float32)
|
43 |
+
|
44 |
+
# Normaliser l'image
|
45 |
+
normalized_image_array = (image_array / 127.5) - 1
|
46 |
+
|
47 |
+
# Créer le tableau de données pour le modèle
|
48 |
+
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
|
49 |
+
data[0] = normalized_image_array
|
50 |
+
|
51 |
+
return data
|
52 |
+
|
53 |
+
# Endpoint pour les prédictions
|
54 |
+
@app.post("/predict/", summary="Upload an image for dental disease prediction", description="This endpoint allows you to upload an image of the teeth and get a prediction of the dental disease.")
|
55 |
+
async def predict(file: UploadFile = File(...)):
|
56 |
+
"""
|
57 |
+
Charge une image et la passe au modèle pour prédire.
|
58 |
+
|
59 |
+
- **file**: fichier image (jpeg ou png) à analyser
|
60 |
+
"""
|
61 |
+
# Lire l'image envoyée
|
62 |
+
try:
|
63 |
+
image = Image.open(io.BytesIO(await file.read()))
|
64 |
+
except Exception as e:
|
65 |
+
return {"error": f"Invalid image file: {str(e)}"}
|
66 |
+
|
67 |
+
# Pré-traiter l'image
|
68 |
+
processed_image = preprocess(image)
|
69 |
+
|
70 |
+
# Calculer le temps de traitement
|
71 |
+
start_time = time.time()
|
72 |
+
|
73 |
+
# Faire la prédiction avec le modèle
|
74 |
+
prediction = model.predict(processed_image)
|
75 |
+
processing_time = time.time() - start_time
|
76 |
+
|
77 |
+
# Trouver la classe avec la probabilité la plus élevée
|
78 |
+
predicted_class_index = np.argmax(prediction[0])
|
79 |
+
predicted_class = class_names[predicted_class_index]
|
80 |
+
|
81 |
+
# Calculer la marge de confiance
|
82 |
+
sorted_probs = np.sort(prediction[0])[::-1]
|
83 |
+
confidence_margin = sorted_probs[0] - sorted_probs[1]
|
84 |
+
|
85 |
+
# Calculer l'entropie
|
86 |
+
uncertainty = entropy(prediction[0]) # calcul de l'entropie
|
87 |
+
|
88 |
+
# Retourner la prédiction sous forme JSON
|
89 |
+
return {
|
90 |
+
"prediction": predicted_class,
|
91 |
+
"confidence": float(prediction[0][predicted_class_index]),
|
92 |
+
"confidence_margin": float(confidence_margin),
|
93 |
+
"uncertainty": float(uncertainty),
|
94 |
+
"processing_time": float(processing_time)
|
95 |
+
}
|
96 |
+
|
97 |
+
# Commande pour démarrer le serveur FastAPI
|
98 |
+
if __name__ == "__main__":
|
99 |
+
import uvicorn
|
100 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
keras_model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e8cfcaa8914840e9c8f98330cf7e157ff04d4ae392dbcc82683ba4055ee9bd87
|
3 |
+
size 2457008
|
labels.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Calculus
|
2 |
+
Caries
|
3 |
+
Gingivitis
|
4 |
+
Hypodontia
|
5 |
+
Tooth Discoloration
|