Commit 90528e9b authored by Kareshaan Logeswaran's avatar Kareshaan Logeswaran

created It20236946 backend

parent 1457e786
from fastapi import FastAPI, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import FileResponse, HTMLResponse
import uvicorn
import numpy as np
from io import BytesIO
from PIL import Image
import tensorflow as tf
app = FastAPI()
# CORS middleware for handling cross-origin requests
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
MODEL = tf.keras.models.load_model("../Models_main/version11")
CLASS_NAMES = ["Flowering Stage", "Maturity Stage", "Out of Domain", "Re-productive Stage", "Ripening Stage"]
def read_file_as_image(data) -> np.ndarray:
image = np.array(Image.open(BytesIO(data)))
return image
@app.post("/predict")
async def predict(file: UploadFile = File(...)):
image = read_file_as_image(await file.read())
img_batch = np.expand_dims(image, 0)
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)
}
@app.get("/", response_class=HTMLResponse)
async def get_index():
with open('paddyStage.html', 'r') as f:
return f.read()
if __name__ == "__main__":
uvicorn.run(app, host='localhost', port=8010)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment