alperugurcan commited on
Commit
3a3e128
·
verified ·
1 Parent(s): 39ac82c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +132 -0
app.py ADDED
@@ -0,0 +1,132 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ from PIL import Image
5
+ import requests
6
+ from io import BytesIO
7
+
8
+ # Load model
9
+ @st.cache_resource
10
+ def load_model():
11
+ return tf.keras.models.load_model('fish_classification_model.h5') # Model yükle
12
+
13
+ model = load_model()
14
+
15
+ # Class names
16
+ class_names = ['Black Sea Sprat', 'Gilt Head Bream', 'Horse Mackerel', 'Red Mullet', 'Red Sea Bream', 'Sea Bass', 'Shrimp', 'Striped Red Mullet', 'Trout'] # Sınıf isimleri
17
+
18
+ # Function to get fish emoji
19
+ def get_fish_emoji(fish_name):
20
+ emoji_dict = {
21
+ 'Black Sea Sprat': '🐟',
22
+ 'Gilt Head Bream': '🐠',
23
+ 'Horse Mackerel': '🐟',
24
+ 'Red Mullet': '🐡',
25
+ 'Red Sea Bream': '🐠',
26
+ 'Sea Bass': '🐟',
27
+ 'Shrimp': '🦐',
28
+ 'Striped Red Mullet': '🐡',
29
+ 'Trout': '🐟'
30
+ }
31
+ return emoji_dict.get(fish_name, '🐠')
32
+
33
+ # Streamlit app
34
+ st.set_page_config(page_title="Fish Species Classifier", page_icon="🐠", layout="wide")
35
+
36
+ # Add a background image
37
+ background_image = """
38
+ <style>
39
+ [data-testid="stAppViewContainer"] > .main {
40
+ background-image: url("https://images.unsplash.com/photo-1498574932731-e711f7092d07");
41
+ background-size: cover;
42
+ background-position: center center;
43
+ background-repeat: no-repeat;
44
+ background-attachment: local;
45
+ }
46
+ </style>
47
+ """
48
+ st.markdown(background_image, unsafe_allow_html=True)
49
+
50
+ # Custom CSS for better styling
51
+ st.markdown("""
52
+ <style>
53
+ .big-font {
54
+ font-size:50px !important;
55
+ color: #0e1117;
56
+ text-align: center;
57
+ }
58
+ .result-font {
59
+ font-size:30px !important;
60
+ color: #0e1117;
61
+ text-align: center;
62
+ }
63
+ </style>
64
+ """, unsafe_allow_html=True)
65
+
66
+ # Title with emoji
67
+ st.markdown('<p class="big-font">🐠 Fish Species Classification 🐟</p>', unsafe_allow_html=True)
68
+
69
+ # File uploader
70
+ uploaded_file = st.file_uploader("Upload a fish image", type=["jpg", "jpeg", "png"])
71
+
72
+ # URL input
73
+ image_url = st.text_input("Or enter an image URL")
74
+
75
+ if uploaded_file is not None or image_url:
76
+ if uploaded_file is not None:
77
+ image = Image.open(uploaded_file)
78
+ else:
79
+ response = requests.get(image_url)
80
+ image = Image.open(BytesIO(response.content))
81
+
82
+ st.image(image, caption='Uploaded Image', use_column_width=True) # Yüklenen resmi göster
83
+
84
+ # Preprocess image
85
+ image = image.resize((224, 224))
86
+ image_array = np.array(image) / 255.0
87
+ image_array = np.expand_dims(image_array, axis=0) # Resmi ön işle
88
+
89
+ # Make prediction
90
+ prediction = model.predict(image_array)
91
+ predicted_class = class_names[np.argmax(prediction)]
92
+ confidence = np.max(prediction) # Tahmin yap
93
+
94
+ # Display result with emoji
95
+ st.markdown(f'<p class="result-font">Predicted fish species: {predicted_class} {get_fish_emoji(predicted_class)}</p>', unsafe_allow_html=True)
96
+ st.markdown(f'<p class="result-font">Confidence: {confidence:.2f}</p>', unsafe_allow_html=True)
97
+
98
+ # Display bar chart of probabilities
99
+ st.subheader("Prediction Probabilities")
100
+ prob_df = pd.DataFrame({'Species': class_names, 'Probability': prediction[0]})
101
+ prob_df = prob_df.sort_values('Probability', ascending=False).reset_index(drop=True)
102
+ st.bar_chart(prob_df.set_index('Species'))
103
+
104
+ # Add some information about the project
105
+ st.sidebar.title("About")
106
+ st.sidebar.info(
107
+ "This app uses a deep learning model to classify fish species. "
108
+ "Upload an image or provide a URL to get started!"
109
+ )
110
+
111
+ # Add a footer
112
+ st.markdown(
113
+ """
114
+ <style>
115
+ #MainMenu {visibility: hidden;}
116
+ footer {visibility: hidden;}
117
+ .footer {
118
+ position: fixed;
119
+ left: 0;
120
+ bottom: 0;
121
+ width: 100%;
122
+ background-color: rgba(14, 17, 23, 0.5);
123
+ color: white;
124
+ text-align: center;
125
+ }
126
+ </style>
127
+ <div class="footer">
128
+ <p>Developed with ❤️ by AE</p>
129
+ </div>
130
+ """,
131
+ unsafe_allow_html=True
132
+ )