EngrKashifKhan commited on
Commit
a2631ed
·
verified ·
1 Parent(s): b933348

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -0
app.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ import pickle
5
+ import cv2
6
+
7
+ # Load model and class names
8
+ model = tf.keras.models.load_model("model.h5")
9
+
10
+ with open("class_names.pkl", "rb") as f:
11
+ class_names = pickle.load(f)
12
+
13
+ # Image preprocessing function
14
+ def preprocess_image(img):
15
+ img = cv2.resize(img, (224, 224))
16
+ img = img / 255.0
17
+ return np.expand_dims(img, axis=0)
18
+
19
+ # Prediction function
20
+ def predict(img):
21
+ img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR) # Convert PIL image to OpenCV format
22
+ processed = preprocess_image(img)
23
+ prediction = model.predict(processed)[0]
24
+ predicted_label = class_names[np.argmax(prediction)]
25
+ confidence = float(np.max(prediction)) * 100
26
+ return f"Predicted: {predicted_label} ({confidence:.2f}%)"
27
+
28
+ # Gradio interface
29
+ interface = gr.Interface(
30
+ fn=predict,
31
+ inputs=gr.Image(type="numpy", label="Upload an Animal Image"),
32
+ outputs="text",
33
+ title="Animal Classifier with ResNet50",
34
+ description="Upload an image of an animal to classify using a pretrained ResNet50 model."
35
+ )
36
+
37
+ interface.launch()