httpsAkayush commited on
Commit
b5017bd
·
1 Parent(s): f2d04f3
Files changed (1) hide show
  1. app.py +20 -16
app.py CHANGED
@@ -7,6 +7,9 @@ import io
7
  import uvicorn
8
  import tempfile
9
  import cv2
 
 
 
10
 
11
  # Initialize FastAPI app
12
  app = FastAPI(title="Plant Disease Detection API", version="1.0.0")
@@ -21,7 +24,10 @@ app.add_middleware(
21
  )
22
 
23
  # Load your model
24
- model = tf.keras.models.load_model('trained_modela.keras')
 
 
 
25
 
26
  # Define your class names (update with your actual classes)
27
  class_name = ['Apple___Apple_scab',
@@ -71,26 +77,25 @@ async def root():
71
 
72
  @app.post("/predict")
73
  async def predict_disease(file: UploadFile = File(...)):
74
- """
75
- Predict plant disease from uploaded image
76
- """
 
77
  try:
78
  # Validate file type
79
  # Validate file type
80
- if not file.content_type.startswith('image/'):
81
- raise HTTPException(status_code=400, detail="File must be an image")
82
-
83
  # Save uploaded file temporarily
84
- with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as tmp:
85
  temp_path = tmp.name
86
- contents = await file.read()
87
- tmp.write(contents)
88
 
89
  # Read image using OpenCV
90
- img = cv2.imread(temp_path)
91
- if img is None:
92
- raise HTTPException(status_code=400, detail="Invalid image file")
93
- img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
94
 
95
  image = tf.keras.preprocessing.image.load_img(temp_path,target_size=(128, 128))
96
 
@@ -109,8 +114,7 @@ async def predict_disease(file: UploadFile = File(...)):
109
  "confidence": confidence
110
  }
111
 
112
- except HTTPException as he:
113
- raise he
114
  except Exception as e:
115
  raise HTTPException(status_code=500, detail=f"Prediction error: {str(e)}")
116
 
 
7
  import uvicorn
8
  import tempfile
9
  import cv2
10
+ import os
11
+ assert os.path.exists("trained_modela.keras"), "Model file not found!"
12
+
13
 
14
  # Initialize FastAPI app
15
  app = FastAPI(title="Plant Disease Detection API", version="1.0.0")
 
24
  )
25
 
26
  # Load your model
27
+ try:
28
+ model = tf.keras.models.load_model("trained_modela.keras")
29
+ except Exception as e:
30
+ raise RuntimeError(f"Failed to load model: {e}")
31
 
32
  # Define your class names (update with your actual classes)
33
  class_name = ['Apple___Apple_scab',
 
77
 
78
  @app.post("/predict")
79
  async def predict_disease(file: UploadFile = File(...)):
80
+
81
+ if not file.content_type.startswith('image/'):
82
+ raise HTTPException(status_code=400, detail="File must be an image")
83
+
84
  try:
85
  # Validate file type
86
  # Validate file type
87
+
 
 
88
  # Save uploaded file temporarily
89
+ with tempfile.NamedTemporaryFile(suffix=".jpg", delete=True) as tmp:
90
  temp_path = tmp.name
91
+ tmp.write(await file.read())
92
+ tmp.flush() # Ensure data is written
93
 
94
  # Read image using OpenCV
95
+ # img = cv2.imread(temp_path)
96
+ # if img is None:
97
+ # raise HTTPException(status_code=400, detail="Invalid image file")
98
+ # img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
99
 
100
  image = tf.keras.preprocessing.image.load_img(temp_path,target_size=(128, 128))
101
 
 
114
  "confidence": confidence
115
  }
116
 
117
+
 
118
  except Exception as e:
119
  raise HTTPException(status_code=500, detail=f"Prediction error: {str(e)}")
120