Commit 0c606af4 authored by Kareshaan Logeswaran's avatar Kareshaan Logeswaran

created It20109776 back end

parent 0c152b55
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()
MODEL = tf.keras.models.load_model("../saved_models/Version34")
# Modify the CLASS_NAMES list to include additional details
CLASS_NAMES = [
"Brown Spot | Fungus Sphaerulina oryzina | Iprodione, Propiconazole, Azoxystrobin, Trifloxystrobin, Carbendazim ",
"No Disease Detected - Healthy | Everything looks fine | Not required",
"Hispa | Armigera feed externally on leaf tissue | Carbofuran 3% CG 10 kg; emamectin benzoate 1.9 EC 170 ml; malathion 5 DP 10 kg; malathion 50 EC 460 ml.",
"Leaf Blast| Fungus Magnaporthe oryzae. | Spray Carbendazim or Edifenphos @ 1.0 gm/lit , Pre-Tillering to Mid-Tillering: Light at 2 to 5 % disease severities - Apply Edifenphos or Carbendazim @ 1.0 gm/lit."
]
def read_file_as_image(data) -> np.ndarray:
image = np.array(Image.open(BytesIO(data)))
return image
@app.post("/predictLeaf")
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("/")
async def get_index():
with open('diseases.html', 'r') as f:
return HTMLResponse(content=f.read(), status_code=200)
if __name__ == "__main__":
uvicorn.run(app, host='localhost', port=8000)
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