ESMATUGBA commited on
Commit
1e7ccc7
·
verified ·
1 Parent(s): eaf87ba

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +106 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,108 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
 
 
 
1
  import streamlit as st
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ import cv2
5
+ from PIL import Image
6
+ from tensorflow.keras.applications import MobileNetV2
7
+ from tensorflow.keras.models import Sequential
8
+ from tensorflow.keras.layers import GlobalAveragePooling2D, Dense, Dropout
9
+
10
+ # --- 1. SAYFA AYARLARI / PAGE CONFIG ---
11
+ st.set_page_config(page_title="Fish Classifier / Balık Sınıflandırıcı", page_icon="🐟", layout="wide")
12
+
13
+ # --- 2. MODEL YÜKLEME / LOAD MODEL ---
14
+ @st.cache_resource
15
+ def load_my_model():
16
+ # Mimariyi manuel kuruyoruz (Sürüm çakışmasını önlemek için)
17
+ base_model = MobileNetV2(weights='imagenet', include_top=False, input_shape=(170, 170, 3))
18
+ base_model.trainable = False
19
+
20
+ model = Sequential([
21
+ base_model,
22
+ GlobalAveragePooling2D(),
23
+ Dense(256, activation='relu'),
24
+ Dropout(0.5),
25
+ Dense(9, activation='softmax')
26
+ ])
27
+
28
+ # Ağırlıkları yükle
29
+ try:
30
+ model.load_weights('fish_transfer_model.h5')
31
+ except:
32
+ model = tf.keras.models.load_model('fish_transfer_model.h5')
33
+ return model
34
+
35
+ model = load_my_model()
36
+
37
+ # --- 3. SÖZLÜK / DICTIONARY ---
38
+ translate = {
39
+ 'Black Sea Sprat': 'Karadeniz Çaça',
40
+ 'Gilt-Head Bream': 'Çipura',
41
+ 'Hourse Mackerel': 'İstavrit',
42
+ 'Red Mullet': 'Barbun',
43
+ 'Red Sea Bream': 'Mercan',
44
+ 'Sea Bass': 'Levrek',
45
+ 'Shrimp': 'Karides',
46
+ 'Striped Red Mullet': 'Tekir',
47
+ 'Trout': 'Alabalık'
48
+ }
49
+ class_labels = sorted(list(translate.keys()))
50
+
51
+ # --- 4. SOL PANEL / SIDEBAR ---
52
+ with st.sidebar:
53
+ st.title("🔍 Species List / Tür Listesi")
54
+ st.write("Supported Fish Types / Desteklenen Balıklar:")
55
+ for en, tr in translate.items():
56
+ st.write(f"🔹 **{en}** / {tr}")
57
+
58
+ st.markdown("---")
59
+ st.info("Model: MobileNetV2\n\nAccuracy / Doğruluk: %99")
60
+
61
+ # --- 5. ANA EKRAN / MAIN SCREEN ---
62
+ st.title("🐟 Fish Classification System / Akıllı Balık Tanımlama")
63
+ st.subheader("Deep Learning Project / Derin Öğrenme Projesi")
64
+
65
+ # Sabit konteyner (Titremeyi önler)
66
+ main_container = st.container()
67
+
68
+ with main_container:
69
+ uploaded_file = st.file_uploader("Upload an image... / Bir resim yükleyin...", type=["jpg", "png", "jpeg"])
70
+
71
+ if uploaded_file is not None:
72
+ col1, col2 = st.columns([1, 1])
73
+
74
+ # Sol Kolon: Resim
75
+ image = Image.open(uploaded_file)
76
+ col1.image(image, caption="Uploaded Image / Yüklenen Resim", use_container_width=True)
77
+
78
+ # Sağ Kolon: Tahmin
79
+ with col2:
80
+ st.write("### Analysis Result / Analiz Sonucu")
81
+
82
+ # --- ÖN İŞLEME / PREPROCESSING ---
83
+ img = np.array(image.convert('RGB'))
84
+ img = cv2.resize(img, (170, 170))
85
+ img = img / 255.0
86
+ img = np.expand_dims(img, axis=0)
87
+
88
+ # --- TAHMİN / PREDICTION ---
89
+ preds = model.predict(img)
90
+ idx = np.argmax(preds)
91
+ prob = np.max(preds) * 100
92
+
93
+ label_en = class_labels[idx]
94
+ label_tr = translate[label_en]
95
+
96
+ # --- GÖRSEL KUTLAMA / CELEBRATION ---
97
+ st.success(f"**Result / Sonuç:** {label_en} / {label_tr}")
98
+ st.balloons() # Balonlar burada uçuyor! 🎈
99
+
100
+ st.metric(label="Confidence / Güven Oranı", value=f"%{prob:.2f}")
101
+
102
+ # Olasılık Grafiği / Chart
103
+ st.write("Probabilities / Olasılıklar:")
104
+ chart_data = {f"{k} / {translate[k]}": float(preds[0][i]) for i, k in enumerate(class_labels)}
105
+ st.bar_chart(chart_data)
106
 
107
+ # --- 6. ALT BİLGİ / FOOTER ---
108
+ st.markdown("---")