update file
Browse files
api.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
from fastapi import FastAPI, File, UploadFile
|
2 |
import numpy as np
|
3 |
from io import BytesIO
|
4 |
from PIL import Image, UnidentifiedImageError
|
@@ -30,16 +30,27 @@ async def ping():
|
|
30 |
return {"message": "Hello, I am alive"}
|
31 |
|
32 |
def read_file_as_image(data) -> np.ndarray:
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
async def predict(file: UploadFile = File(...)):
|
37 |
image_data = await file.read()
|
38 |
|
|
|
|
|
|
|
|
|
39 |
try:
|
40 |
image = read_file_as_image(image_data)
|
41 |
-
except
|
42 |
-
return
|
43 |
|
44 |
# Görüntüyü modelin beklediği boyuta getirme
|
45 |
image = tf.image.resize(image, (299, 299)) # InceptionV3 için 299x299 olmalı
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
2 |
import numpy as np
|
3 |
from io import BytesIO
|
4 |
from PIL import Image, UnidentifiedImageError
|
|
|
30 |
return {"message": "Hello, I am alive"}
|
31 |
|
32 |
def read_file_as_image(data) -> np.ndarray:
|
33 |
+
try:
|
34 |
+
image = Image.open(BytesIO(data))
|
35 |
+
return np.array(image)
|
36 |
+
except UnidentifiedImageError:
|
37 |
+
raise HTTPException(status_code=400, detail="Invalid image format")
|
38 |
+
except Exception as e:
|
39 |
+
raise HTTPException(status_code=400, detail=f"Image processing error: {str(e)}")
|
40 |
+
|
41 |
+
|
42 |
+
@app.post("/predict/")
|
43 |
async def predict(file: UploadFile = File(...)):
|
44 |
image_data = await file.read()
|
45 |
|
46 |
+
# Dosya tipini kontrol et
|
47 |
+
if file.content_type not in ["image/jpeg", "image/png", "image/jpg"]:
|
48 |
+
raise HTTPException(status_code=400, detail="Invalid file type")
|
49 |
+
|
50 |
try:
|
51 |
image = read_file_as_image(image_data)
|
52 |
+
except HTTPException as e:
|
53 |
+
return e
|
54 |
|
55 |
# Görüntüyü modelin beklediği boyuta getirme
|
56 |
image = tf.image.resize(image, (299, 299)) # InceptionV3 için 299x299 olmalı
|