update file
Browse files
api.py
CHANGED
@@ -1,23 +1,22 @@
|
|
1 |
from fastapi import FastAPI, File, UploadFile
|
2 |
import numpy as np
|
3 |
from io import BytesIO
|
4 |
-
from PIL import Image
|
5 |
import tensorflow as tf
|
6 |
from fastapi.middleware.cors import CORSMiddleware
|
7 |
|
8 |
app = FastAPI()
|
9 |
-
|
10 |
-
|
11 |
-
]
|
12 |
|
13 |
app.add_middleware(
|
14 |
CORSMiddleware,
|
15 |
allow_origins=origins,
|
16 |
allow_credentials=True,
|
17 |
-
allow_methods=["*"],
|
18 |
allow_headers=["*"],
|
19 |
)
|
20 |
-
|
21 |
MODEL = tf.keras.models.load_model("CLAHE_ODIR-ORJ-512_inception_v3.h5")
|
22 |
|
23 |
# Sınıf isimleri
|
@@ -26,24 +25,31 @@ class_names = [
|
|
26 |
'Hypertension', 'Normal', 'Others', 'Pathological Myopia'
|
27 |
]
|
28 |
|
|
|
29 |
@app.get("/ping")
|
30 |
async def ping():
|
31 |
-
return "Hello, I am alive"
|
|
|
32 |
|
33 |
def read_file_as_image(data) -> np.ndarray:
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
36 |
|
37 |
@app.post("/predict")
|
38 |
async def predict(file: UploadFile = File(...)):
|
39 |
image_data = await file.read()
|
|
|
40 |
try:
|
41 |
image = read_file_as_image(image_data)
|
42 |
-
except
|
43 |
-
return {"error":
|
44 |
|
45 |
# Görüntüyü modelin beklediği boyuta getirme
|
46 |
-
image = tf.image.resize(image, (
|
47 |
# Görüntüyü normalize etme
|
48 |
image = tf.cast(image, tf.float32) / 255.0
|
49 |
# Batch haline getirme
|
|
|
1 |
from fastapi import FastAPI, File, UploadFile
|
2 |
import numpy as np
|
3 |
from io import BytesIO
|
4 |
+
from PIL import Image, UnidentifiedImageError
|
5 |
import tensorflow as tf
|
6 |
from fastapi.middleware.cors import CORSMiddleware
|
7 |
|
8 |
app = FastAPI()
|
9 |
+
|
10 |
+
origins = ["*"]
|
|
|
11 |
|
12 |
app.add_middleware(
|
13 |
CORSMiddleware,
|
14 |
allow_origins=origins,
|
15 |
allow_credentials=True,
|
16 |
+
allow_methods=["*"],
|
17 |
allow_headers=["*"],
|
18 |
)
|
19 |
+
|
20 |
MODEL = tf.keras.models.load_model("CLAHE_ODIR-ORJ-512_inception_v3.h5")
|
21 |
|
22 |
# Sınıf isimleri
|
|
|
25 |
'Hypertension', 'Normal', 'Others', 'Pathological Myopia'
|
26 |
]
|
27 |
|
28 |
+
|
29 |
@app.get("/ping")
|
30 |
async def ping():
|
31 |
+
return {"message": "Hello, I am alive"}
|
32 |
+
|
33 |
|
34 |
def read_file_as_image(data) -> np.ndarray:
|
35 |
+
try:
|
36 |
+
image = Image.open(BytesIO(data))
|
37 |
+
return np.array(image)
|
38 |
+
except UnidentifiedImageError:
|
39 |
+
raise ValueError("Invalid image format")
|
40 |
+
|
41 |
|
42 |
@app.post("/predict")
|
43 |
async def predict(file: UploadFile = File(...)):
|
44 |
image_data = await file.read()
|
45 |
+
|
46 |
try:
|
47 |
image = read_file_as_image(image_data)
|
48 |
+
except ValueError as e:
|
49 |
+
return {"error": str(e)}
|
50 |
|
51 |
# Görüntüyü modelin beklediği boyuta getirme
|
52 |
+
image = tf.image.resize(image, (299, 299))
|
53 |
# Görüntüyü normalize etme
|
54 |
image = tf.cast(image, tf.float32) / 255.0
|
55 |
# Batch haline getirme
|