File size: 2,046 Bytes
26e6832
c685cf7
 
cde77b5
7b52e04
bdfbf9d
c685cf7
 
cde77b5
 
c685cf7
bdfbf9d
 
 
 
cde77b5
bdfbf9d
 
cde77b5
c685cf7
 
 
 
 
 
 
 
7b52e04
 
cde77b5
 
a2e75f2
26e6832
 
 
 
 
 
 
 
 
 
7b52e04
 
cde77b5
26e6832
 
 
 
7b52e04
 
26e6832
 
7b52e04
 
98ce6e9
7b52e04
c685cf7
7b52e04
c685cf7
7b52e04
 
c685cf7
 
 
 
 
 
 
 
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
from fastapi import FastAPI, File, UploadFile, HTTPException
import numpy as np
from io import BytesIO
from PIL import Image, UnidentifiedImageError
import tensorflow as tf
from fastapi.middleware.cors import CORSMiddleware

app = FastAPI()

origins = ["*"]

app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

MODEL = tf.keras.models.load_model("CLAHE_ODIR-ORJ-512_inception_v3.h5")

# Sınıf isimleri
class_names = [
    'Age related Macular Degeneration', 'Cataract', 'Diabetes', 'Glaucoma',
    'Hypertension', 'Normal', 'Others', 'Pathological Myopia'
]

@app.get("/ping")
async def ping():
    return {"message": "Hello, I am alive"}

def read_file_as_image(data) -> np.ndarray:
    try:
        image = Image.open(BytesIO(data))
        return np.array(image)
    except UnidentifiedImageError:
        raise HTTPException(status_code=400, detail="Invalid image format")
    except Exception as e:
        raise HTTPException(status_code=400, detail=f"Image processing error: {str(e)}")


@app.post("/predict/")
async def predict(file: UploadFile = File(...)):
    image_data = await file.read()

    # Dosya tipini kontrol et
    if file.content_type not in ["image/jpeg", "image/png", "image/jpg"]:
        raise HTTPException(status_code=400, detail="Invalid file type")

    try:
        image = read_file_as_image(image_data)
    except HTTPException as e:
        return e

    # Görüntüyü modelin beklediği boyuta getirme
    image = tf.image.resize(image, (299, 299))  # InceptionV3 için 299x299 olmalı
    # Görüntüyü normalize etme
    image = tf.cast(image, tf.float32) / 255.0
    # Batch haline getirme
    img_batch = np.expand_dims(image, 0)

    # Model üzerinde tahmin yapma
    predictions = MODEL.predict(img_batch)
    predicted_class = class_names[np.argmax(predictions[0])]
    confidence = np.max(predictions[0])

    return {
        'class': predicted_class,
        'confidence': float(confidence)
    }