-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
64 lines (55 loc) · 2.38 KB
/
Copy pathapp.py
File metadata and controls
64 lines (55 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import streamlit as st
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import tensorflow as tf
import pickle
# Load the models
st.title('Fight Prediction Using ResNet152')
st.caption('-In Model-1 Use Resnet152')
st.caption('-In Model-2 Use Resnet152 with fine Tuning')
st.caption('-In Model-2 Use Efficientnetb7_Process')
with open('index_to_class.pkl', 'rb') as f:
classes = pickle.load(f)
# Preprocessing functions
def Resnet152_Process(img_path, target_size=(224, 224)):
img = Image.open(img_path)
img = img.resize(target_size)
img_array = np.array(img) / 255.0
return img_array
def Efficientnetb7_Process(img_path, target_size=(128, 128)):
img = Image.open(img_path)
img = img.resize(target_size)
img_array = np.array(img) / 255.0
return img_array
# Sidebar options to choose the model
st.sidebar.title("Choose a Model")
model_option = st.sidebar.selectbox("Select Model", ("ResNet152 (Model 2)","ResNet152 (Model 1)", "EfficientNetB7 (Model 3)"))
# Upload image file
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png",'webp'])
if uploaded_file is not None:
img = Image.open(uploaded_file)
st.image(img, caption='Uploaded Image.', use_column_width=True)
st.write("")
st.write("Classifying...")
# Model selection and image processing
if model_option == "ResNet152 (Model 1)":
model1 = tf.keras.models.load_model('model1.h5')
processed_img = Resnet152_Process(uploaded_file)
processed_img = np.expand_dims(processed_img, axis=0)
prediction = model1.predict(processed_img)
predicted_class = classes[int(np.argmax(prediction, axis=1))]
elif model_option == "ResNet152 (Model 2)":
model2 = tf.keras.models.load_model('model2.h5')
processed_img = Resnet152_Process(uploaded_file)
processed_img = np.expand_dims(processed_img, axis=0)
prediction = model2.predict(processed_img)
predicted_class = classes[int(np.argmax(prediction, axis=1))]
else:
model3 = tf.keras.models.load_model('model3.h5')
processed_img = Efficientnetb7_Process(uploaded_file)
processed_img = np.expand_dims(processed_img, axis=0)
prediction = model3.predict(processed_img)
predicted_class = classes[int(np.argmax(prediction, axis=1))]
st.success(f"Predicted Class: {predicted_class}")