DXD-FYP commited on
Commit
86c0ef2
·
1 Parent(s): 440c558

Added app.py file

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, UploadFile, File
2
+ from fastapi.responses import JSONResponse
3
+ from tensorflow.keras.preprocessing import image
4
+ import keras
5
+ import numpy as np
6
+
7
+ app = FastAPI()
8
+
9
+ model = keras.models.load_model("Detection_Covid_19.h5")
10
+
11
+ @app.post("/predict")
12
+ async def predict(file: UploadFile = File(...)):
13
+ try:
14
+ # Read the file into bytes
15
+ img_bytes = await file.read()
16
+
17
+ # Store the image in a temporary path
18
+ img_path = "./uploaded_images/test.jpg"
19
+ with open(img_path, "wb") as img:
20
+ img.write(img_bytes)
21
+
22
+ # Preprocess the image before feeding it to the model
23
+ img = image.load_img(img_path, target_size=(224, 224))
24
+ img = image.img_to_array(img)
25
+ img = np.expand_dims(img, axis=0)
26
+ img = img / 255.0 # Normalize the pixel values
27
+
28
+ pred = model.predict(img)
29
+ prediction = "1" if pred[0][0] <= 0.5 else "0"
30
+
31
+ return {"prediction": prediction}
32
+
33
+ except Exception as e:
34
+ return JSONResponse(content={"error": str(e)}, status_code=500)
35
+
36
+