handex commited on
Commit
d5dd5e5
·
verified ·
1 Parent(s): 51622bf

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +64 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,66 @@
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
+ from PIL import Image
5
+
6
+ IMG_SIZE = 128
7
+
8
+ # Class Labels
9
+ # Based on: {'ALB':0, 'YFT':1, 'SHARK':2, 'NoF':3, 'LAG':4, 'BET':5, 'OTHER':6, 'DOL':7}
10
+ LABELS = ['ALB', 'YFT', 'SHARK', 'NoF', 'LAG', 'BET', 'OTHER', 'DOL']
11
+
12
+ # Page Config
13
+ st.set_page_config(page_title="🐟 Fish Species Detection", page_icon="🐟")
14
+
15
+ # --- LOAD MODEL ---
16
+ @st.cache_resource
17
+ def load_model():
18
+ try:
19
+ model = tf.keras.models.load_model('src/fish.h5')
20
+ return model
21
+ except Exception as e:
22
+ st.error(f"Error loading model: {e}")
23
+ return None
24
+
25
+ model = load_model()
26
+
27
+ # --- IMAGE PREPROCESSING ---
28
+ def process_image(image):
29
+ image = image.resize((IMG_SIZE, IMG_SIZE))
30
+ img_array = np.array(image)
31
+ img_array = img_array / 255.0
32
+ img_array = np.expand_dims(img_array, axis=0)
33
+ return img_array
34
+
35
+ # --- USER INTERFACE ---
36
+ st.title("🐟 Fish Species Classification")
37
+ st.write("The Nature Conservancy Fisheries Monitoring Model")
38
+
39
+ uploaded_file = st.file_uploader("Upload a fish image.", type=["jpg", "png", "jpeg"])
40
 
41
+ if uploaded_file is not None:
42
+ image = Image.open(uploaded_file).convert('RGB')
43
+ st.image(image, caption='Uploaded Image', use_column_width=True)
44
+
45
+ if st.button('Predict'):
46
+ if model is None:
47
+ st.error("Model could not be loaded, prediction impossible.")
48
+ else:
49
+ with st.spinner('Analyzing...'):
50
+ # Process image
51
+ processed_img = process_image(image)
52
+ # Make prediction
53
+ predictions = model.predict(processed_img)
54
+ # Get the highest probability class
55
+ predicted_class_idx = np.argmax(predictions[0])
56
+ predicted_class = LABELS[predicted_class_idx]
57
+ confidence = np.max(predictions[0])
58
+ # Display Results
59
+ st.success(f"Prediction: **{predicted_class}**")
60
+ st.info(f"Confidence: **{confidence * 100:.2f}%**")
61
+ # Probability Distribution Chart
62
+ st.write("---")
63
+ st.write("Probability Distribution:")
64
+ # Create a dictionary for the chart
65
+ chart_data = {label: float(predictions[0][i]) for i, label in enumerate(LABELS)}
66
+ st.bar_chart(chart_data)