-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
80 lines (66 loc) · 2.34 KB
/
main.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Importing the required libraries
import numpy as np
import tensorflow as tf
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input, decode_predictions
import sys
from fastapi.middleware.cors import CORSMiddleware
from fastapi import FastAPI, UploadFile, File
import os
from fastapi.responses import JSONResponse
# FastAPI instance
app = FastAPI()
# Global Configurations
origins = [
"http://localhost:5173",
]
sys.stdout.reconfigure(encoding='utf-8')
categories = ['BA-cellulitis', 'BA-impetigo', 'FU-athlete-foot', 'FU-nail-fungus', 'FU-ringworm', 'PA-cutaneous-larva-migrans', 'VI-chickenpox', "VI-shingles"]
UPLOAD_FOLDER = "upload"
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
# Adding CORS middleware to the FastAPI instance
# noinspection PyTypeChecker
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Load the model
model = tf.keras.models.load_model('mobv2.keras')
# Api Routes
@app.get("/")
async def root():
return {"message": "Hello this is the root of the API"}
def get_confidence(preds):
top_class = preds.argsort()[0][-1:]
confidence = preds[0, top_class[0]]
return confidence*100
@app.post('/predict')
async def predict(file: UploadFile = File(...)):
# Save the image to the upload folder
contents = await file.read()
filename = os.path.join(UPLOAD_FOLDER, file.filename)
with open(filename, "wb") as f:
f.write(contents)
# Create the image path
img_path = os.path.join(UPLOAD_FOLDER, file.filename)
# Load the image and preprocess it
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
# Predict the image
prediction = model.predict(x)
predicted_classes = np.argmax(prediction, axis=1)
predicted_category = categories[predicted_classes[0]]
confidence = get_confidence(prediction)
print(predicted_category,str(confidence))
os.remove(filename)
return JSONResponse(content={"message": "success", "prediction": predicted_category, "confidence": str(confidence)})
if __name__ == "__main__":
import uvicorn
print("got here")
# Run the FastAPI application
uvicorn.run(app, host="0.0.0.0", port=3000)