amasood commited on
Commit
35aa024
·
verified ·
1 Parent(s): dd42b25

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -0
app.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import requests
4
+ from io import BytesIO
5
+ import numpy as np
6
+ from tensorflow.keras.models import load_model
7
+ from tensorflow.keras.utils import img_to_array
8
+
9
+ # Load the pre-trained model
10
+ @st.cache_resource
11
+ def load_animal_model():
12
+ model_url = "https://huggingface.co/shaktibiplab/Animal-Classification/resolve/main/best_model.weights.h5"
13
+ response = requests.get(model_url)
14
+ model_path = "best_model.weights.h5"
15
+ with open(model_path, "wb") as f:
16
+ f.write(response.content)
17
+ model = load_model(model_path)
18
+ return model
19
+
20
+ model = load_animal_model()
21
+
22
+ # Define class labels
23
+ class_labels = ['cat', 'dog', 'horse', 'lion', 'tiger', 'elephant']
24
+
25
+ # Streamlit app
26
+ st.title("Animal Detection App")
27
+ st.write("Upload an image to classify the animal.")
28
+
29
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
30
+
31
+ if uploaded_file is not None:
32
+ image = Image.open(uploaded_file)
33
+ st.image(image, caption='Uploaded Image.', use_column_width=True)
34
+ st.write("Classifying...")
35
+
36
+ # Preprocess the image
37
+ img = image.resize((256, 256))
38
+ img_array = img_to_array(img) / 255.0
39
+ img_array = np.expand_dims(img_array, axis=0)
40
+
41
+ # Make prediction
42
+ predictions = model.predict(img_array)
43
+ predicted_class = class_labels[np.argmax(predictions)]
44
+ confidence = np.max(predictions)
45
+
46
+ st.write(f"Prediction: {predicted_class} ({confidence:.2%} confidence)")