Update src/streamlit_app.py
Browse files- src/streamlit_app.py +78 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,80 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
import
|
|
|
|
|
|
|
| 4 |
import streamlit as st
|
| 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 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Fish Species Classification — Transfer Learning (standalone Streamlit app)."""
|
| 2 |
+
|
| 3 |
+
import sys
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
import streamlit as st
|
| 7 |
|
| 8 |
+
if "/opt/anaconda3" in sys.executable:
|
| 9 |
+
st.set_page_config(page_title="Python ortam hatasi", page_icon="⚠️")
|
| 10 |
+
st.error(
|
| 11 |
+
"Bu uygulama Anaconda Python ile calismaz (segmentation fault).\n\n"
|
| 12 |
+
"Asagidaki komutu kullanin:\n\n"
|
| 13 |
+
f"```bash\ncd \"{Path(__file__).resolve().parent}\"\n"
|
| 14 |
+
"~/venvs/tensorflow/bin/streamlit run "
|
| 15 |
+
f"{Path(__file__).name}\n```"
|
| 16 |
+
)
|
| 17 |
+
st.stop()
|
| 18 |
+
|
| 19 |
+
import json
|
| 20 |
+
|
| 21 |
+
import numpy as np
|
| 22 |
+
import tensorflow as tf
|
| 23 |
+
from PIL import Image
|
| 24 |
+
|
| 25 |
+
ROOT = Path(__file__).resolve().parent
|
| 26 |
+
|
| 27 |
+
MODEL_PATH = Path('src/fish_cnn.h5')
|
| 28 |
+
META_PATH = Path('src/fish_cnn_meta.json')
|
| 29 |
+
|
| 30 |
+
def get_preprocess(backbone):
|
| 31 |
+
if backbone == "VGG16":
|
| 32 |
+
from tensorflow.keras.applications.vgg16 import preprocess_input
|
| 33 |
+
return preprocess_input
|
| 34 |
+
if backbone == "ResNet50":
|
| 35 |
+
from tensorflow.keras.applications.resnet50 import preprocess_input
|
| 36 |
+
return preprocess_input
|
| 37 |
+
from tensorflow.keras.applications.xception import preprocess_input
|
| 38 |
+
return preprocess_input
|
| 39 |
+
|
| 40 |
+
@st.cache_resource
|
| 41 |
+
def load_model():
|
| 42 |
+
if not MODEL_PATH.is_file():
|
| 43 |
+
raise FileNotFoundError(f"Model not found: {MODEL_PATH}. Run fish_TL.ipynb first.")
|
| 44 |
+
return tf.keras.models.load_model(MODEL_PATH)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def load_meta():
|
| 48 |
+
return json.loads(META_PATH.read_text(encoding="utf-8"))
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def prepare_image(img, meta):
|
| 52 |
+
size = tuple(meta["img_size"])
|
| 53 |
+
arr = np.array(img.convert("RGB").resize(size), dtype=np.float32)
|
| 54 |
+
arr = np.expand_dims(arr, axis=0)
|
| 55 |
+
preprocess = get_preprocess(meta.get('backbone', 'VGG16'))
|
| 56 |
+
arr = preprocess(arr)
|
| 57 |
+
return arr
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
st.set_page_config(page_title="Fish Species Classification", page_icon="🧠")
|
| 61 |
+
st.title("Fish Species Classification")
|
| 62 |
+
st.caption("Transfer Learning — train with `fish_TL.ipynb`")
|
| 63 |
+
|
| 64 |
+
try:
|
| 65 |
+
model = load_model()
|
| 66 |
+
meta = load_meta()
|
| 67 |
+
except FileNotFoundError as e:
|
| 68 |
+
st.error(str(e))
|
| 69 |
+
st.stop()
|
| 70 |
+
|
| 71 |
+
uploaded = st.file_uploader("Upload image (jpg/png)", type=["jpg", "jpeg", "png"])
|
| 72 |
+
if uploaded:
|
| 73 |
+
img = Image.open(uploaded)
|
| 74 |
+
st.image(img, use_container_width=True)
|
| 75 |
+
batch = prepare_image(img, meta)
|
| 76 |
+
probs = model.predict(batch, verbose=0)[0]
|
| 77 |
+
idx = int(np.argmax(probs))
|
| 78 |
+
label = meta["class_names"][idx]
|
| 79 |
+
st.success(f"Prediction: **{label}**")
|
| 80 |
+
st.write(f"Confidence: {probs[idx]:.2%}")
|