Kartikay1211 commited on
Commit
b9616a2
Β·
verified Β·
1 Parent(s): 40fd463

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -20
app.py CHANGED
@@ -1,36 +1,36 @@
1
  import gradio as gr
2
  import numpy as np
3
- from tensorflow.keras.applications.efficientnet import EfficientNetB0, preprocess_input, decode_predictions
 
 
4
 
5
- # Load pretrained EfficientNetB0 model
6
- model = EfficientNetB0(weights="imagenet")
7
 
8
  def predict(img):
9
- # Convert to RGB and resize
10
- img = img.convert("RGB")
11
- img = img.resize((224, 224))
12
 
13
- # Preprocess
14
- x = np.array(img)
15
  x = np.expand_dims(x, axis=0)
16
  x = preprocess_input(x)
17
 
18
- # Predict
19
  preds = model.predict(x)
20
- decoded = decode_predictions(preds, top=3)[0]
21
-
22
- # Format results with percentages
23
- results = {label: f"{score * 100:.2f}%" for (_, label, score) in decoded}
24
- return results
25
 
26
- # Gradio interface
27
- iface = gr.Interface(
28
  fn=predict,
29
  inputs=gr.Image(type="pil"),
30
- outputs=gr.Label(num_top_classes=3),
31
- title="🐾 Smart Animal & Insect Identifier",
32
- description="Upload an image of an animal or insect, and the model will try to identify it."
33
  )
34
 
35
  if __name__ == "__main__":
36
- iface.launch()
 
1
  import gradio as gr
2
  import numpy as np
3
+ from tensorflow.keras.applications import EfficientNetB0
4
+ from tensorflow.keras.applications.efficientnet import preprocess_input, decode_predictions
5
+ from tensorflow.keras.preprocessing import image
6
 
7
+ # Load pretrained EfficientNetB0
8
+ model = EfficientNetB0(weights="imagenet", include_top=True, input_shape=(224, 224, 3))
9
 
10
  def predict(img):
11
+ # Ensure image is RGB
12
+ if img.mode != "RGB":
13
+ img = img.convert("RGB")
14
 
15
+ img = img.resize((224, 224))
16
+ x = image.img_to_array(img)
17
  x = np.expand_dims(x, axis=0)
18
  x = preprocess_input(x)
19
 
 
20
  preds = model.predict(x)
21
+ decoded = decode_predictions(preds, top=1)[0][0]
22
+ label = decoded[1]
23
+ confidence = float(decoded[2])
24
+ return f"πŸ” Prediction: {label} ({confidence:.2f} confidence)"
 
25
 
26
+ # Gradio Interface
27
+ demo = gr.Interface(
28
  fn=predict,
29
  inputs=gr.Image(type="pil"),
30
+ outputs="text",
31
+ title="🐾 Animal & Insect Identifier",
32
+ description="Upload an image of an animal or insect, and the model will identify it."
33
  )
34
 
35
  if __name__ == "__main__":
36
+ demo.launch()