-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
60 lines (46 loc) · 1.92 KB
/
app.py
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
# FastAPI + ResNet50 Model
# I used cat_img.jpg and another examples for testing (attached)
from fastapi import FastAPI, File, UploadFile, HTTPException
import uvicorn
from io import BytesIO
from PIL import Image
import numpy as np
from keras.applications.resnet50 import ResNet50, preprocess_input, decode_predictions
from keras.preprocessing import image
from fastapi.middleware.cors import CORSMiddleware
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Load the model
model = ResNet50(weights='imagenet')
@app.post("/classify")
async def classify_image(image_file: UploadFile = File(...)):
try:
# Read the image file
contents = await image_file.read()
pil_image = Image.open(BytesIO(contents)).convert("RGB")
# Resize the image to 224x244, because I had a problem with the image size... so I tested (cat_img.jp)
pil_image = pil_image.resize((224, 224))
# Convert to array
img_array = image.img_to_array(pil_image)
img_array = np.expand_dims(img_array, axis=0)
img_array = preprocess_input(img_array)
# Predict the class of the image
predictions = model.predict(img_array)
decoded_predictions = decode_predictions(predictions, top=3)[0]
# Format the predictions
formatted_predictions = [{"label": str(pred[1]), "probability": float(pred[2])} for pred in decoded_predictions]
return {"predictions": formatted_predictions}
except Exception as e:
print(f"Error during image processing and prediction, tray again please!: {e}")
raise HTTPException(status_code=500, detail=str(e))
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
### For testing API
# uvicorn app:app --reload
# http://localhost:8000/docs