Awashati commited on
Commit
67e8993
·
verified ·
1 Parent(s): 140aa41

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -18
app.py CHANGED
@@ -1,21 +1,28 @@
1
  import joblib
 
2
  import numpy as np
 
3
  from tensorflow.keras.applications import MobileNetV2
4
  from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
5
  from tensorflow.keras.preprocessing import image
6
- import gradio as gr
7
- import json
 
 
8
 
9
  # --- Load model and labels ---
10
- model_data = joblib.load(MODEL_PATH)
11
- clf = model_data["model"] # your KNN classifier
12
- labels = model_data["
13
- # the KNN classifier
14
- with open("labels.json", "r") as f:
 
15
  labels = json.load(f)
16
 
17
- # --- Feature extractor ---
18
- feature_extractor = MobileNetV2(weights="imagenet", include_top=False, pooling="avg")
 
 
19
 
20
  def predict(img):
21
  # preprocess image
@@ -24,22 +31,22 @@ def predict(img):
24
  img = preprocess_input(img)
25
 
26
  # extract features
27
- feat = feature_extractor.predict(img)
28
 
29
- # predict with KNN
30
- pred_idx = int(clf.predict(feat)[0])
31
- pred_label = labels[str(pred_idx)] if str(pred_idx) in labels else "Unknown"
32
 
33
  return pred_label
34
 
35
  # --- Gradio UI ---
36
- demo = gr.Interface(
37
  fn=predict,
38
- inputs=gr.Image(type="pil"),
39
- outputs="text",
40
  title="🌿 UAE Flora Classifier",
41
- description="Upload a plant photo and the model will predict its species."
42
  )
43
 
44
  if __name__ == "__main__":
45
- demo.launch()
 
1
  import joblib
2
+ import json
3
  import numpy as np
4
+ import gradio as gr
5
  from tensorflow.keras.applications import MobileNetV2
6
  from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
7
  from tensorflow.keras.preprocessing import image
8
+
9
+ # --- Paths to your model and labels ---
10
+ MODEL_PATH = "my_model_k7.pkl"
11
+ LABELS_PATH = "labels.json"
12
 
13
  # --- Load model and labels ---
14
+ model_data = joblib.load(MODEL_PATH) # this is a dict with model + labels
15
+ clf = model_data["model"] # your KNN classifier
16
+ labels = model_data.get("labels", None) # try to load labels from inside dict
17
+
18
+ # If labels are stored separately, override
19
+ with open(LABELS_PATH, "r") as f:
20
  labels = json.load(f)
21
 
22
+ # --- Feature extractor (MobileNetV2 embeddings) ---
23
+ feature_extractor = MobileNetV2(weights="imagenet",
24
+ include_top=False,
25
+ pooling="avg")
26
 
27
  def predict(img):
28
  # preprocess image
 
31
  img = preprocess_input(img)
32
 
33
  # extract features
34
+ features = feature_extractor.predict(img)
35
 
36
+ # run through classifier
37
+ pred_idx = int(clf.predict(features)[0])
38
+ pred_label = labels[str(pred_idx)] if isinstance(labels, dict) else labels[pred_idx]
39
 
40
  return pred_label
41
 
42
  # --- Gradio UI ---
43
+ iface = gr.Interface(
44
  fn=predict,
45
+ inputs=gr.Image(type="pil", label="Upload plant image"),
46
+ outputs=gr.Label(num_top_classes=1, label="Predicted class"),
47
  title="🌿 UAE Flora Classifier",
48
+ description="Upload a plant photo and I’ll predict the species using a KNN classifier over MobileNetV2 embeddings."
49
  )
50
 
51
  if __name__ == "__main__":
52
+ iface.launch()