walidchaib commited on
Commit
dfecc6d
·
verified ·
1 Parent(s): 7ba7117

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -14
app.py CHANGED
@@ -5,44 +5,62 @@ from PIL import Image
5
  import json
6
 
7
  # ============================
8
- # LOAD MODEL + LABELS
9
  # ============================
10
- model = tf.keras.models.load_model("lemon_model.h5")
11
 
 
 
 
 
 
 
 
 
 
 
12
  with open("labels.json", "r") as f:
13
  class_names = json.load(f)
14
 
15
  IMG_SIZE = (224, 224)
16
 
17
  # ============================
18
- # PREDICTION FUNCTION
19
  # ============================
20
- def predict(image):
21
- image = image.resize(IMG_SIZE)
22
- img_array = np.array(image) / 255.0
 
23
  img_array = np.expand_dims(img_array, axis=0)
 
24
 
25
- predictions = model.predict(img_array)[0]
 
 
 
 
 
26
 
27
- results = {
28
- class_names[i]: float(predictions[i])
29
  for i in range(len(class_names))
30
  }
31
 
32
- return results
33
 
34
  # ============================
35
  # INTERFACE
36
  # ============================
37
- interface = gr.Interface(
38
  fn=predict,
39
  inputs=gr.Image(type="pil"),
40
  outputs=gr.Label(num_top_classes=3),
41
- title="🍋 Lemon Leaf Disease Detection",
42
- description="Upload an image of a lemon leaf to detect disease"
43
  )
44
 
45
  # ============================
46
  # LAUNCH
47
  # ============================
48
- interface.launch()
 
 
5
  import json
6
 
7
  # ============================
8
+ # LOAD MODEL (.h5)
9
  # ============================
10
+ model = tf.keras.models.load_model("lemon_model.h5", compile=False)
11
 
12
+ # Recompiler (important pour certains .h5)
13
+ model.compile(
14
+ optimizer="adam",
15
+ loss="categorical_crossentropy",
16
+ metrics=["accuracy"]
17
+ )
18
+
19
+ # ============================
20
+ # LOAD LABELS
21
+ # ============================
22
  with open("labels.json", "r") as f:
23
  class_names = json.load(f)
24
 
25
  IMG_SIZE = (224, 224)
26
 
27
  # ============================
28
+ # PREPROCESS
29
  # ============================
30
+ def preprocess(img):
31
+ img = img.convert("RGB")
32
+ img = img.resize(IMG_SIZE)
33
+ img_array = np.array(img) / 255.0
34
  img_array = np.expand_dims(img_array, axis=0)
35
+ return img_array
36
 
37
+ # ============================
38
+ # PREDICTION
39
+ # ============================
40
+ def predict(img):
41
+ img_array = preprocess(img)
42
+ preds = model.predict(img_array)[0]
43
 
44
+ result = {
45
+ class_names[i]: float(preds[i])
46
  for i in range(len(class_names))
47
  }
48
 
49
+ return result
50
 
51
  # ============================
52
  # INTERFACE
53
  # ============================
54
+ demo = gr.Interface(
55
  fn=predict,
56
  inputs=gr.Image(type="pil"),
57
  outputs=gr.Label(num_top_classes=3),
58
+ title="🍋 Lemon Leaf Disease Detection (H5 Model)",
59
+ description="Upload a lemon leaf image to detect disease"
60
  )
61
 
62
  # ============================
63
  # LAUNCH
64
  # ============================
65
+ if __name__ == "__main__":
66
+ demo.launch()