divya55 commited on
Commit
5e40bb4
Β·
verified Β·
1 Parent(s): b189780

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +12 -56
src/streamlit_app.py CHANGED
@@ -1,66 +1,22 @@
1
- import os
2
- os.environ["STREAMLIT_CONFIG_DIR"] = "/tmp/.streamlit"
3
- os.environ["STREAMLIT_SECRETS_FILE"] = "/tmp/.streamlit/secrets.toml"
4
-
5
  import streamlit as st
6
- import pickle
7
  import numpy as np
8
  import cv2
9
 
10
- # ------------------------------
11
- # Load saved models
12
- # ------------------------------
13
- with open("src/pca_model.pkl", "rb") as f:
14
- pca = pickle.load(f)
15
-
16
- with open("src/svc_model.pkl", "rb") as f:
17
- svc = pickle.load(f)
18
-
19
- with open("src/scaler.pkl", "rb") as f:
20
- scaler = pickle.load(f)
21
 
22
- # ------------------------------
23
- # Label mapping
24
- # ------------------------------
25
- label_map = {
26
- 0: "fresh apple",
27
- 1: "fresh banana",
28
- 2: "fresh orange",
29
- 3: "rotten apple",
30
- 4: "rotten banana",
31
- 5: "rotten orange"
32
- }
33
-
34
- # ------------------------------
35
- # Streamlit UI
36
- # ------------------------------
37
- st.set_page_config(page_title="Fruit Image Classification", layout="centered")
38
- st.title("🍎 Fruit Image Classification")
39
- st.write("Upload an image of a fruit (fresh or rotten) to get the prediction.")
40
-
41
- # ------------------------------
42
- # File uploader
43
- # ------------------------------
44
- uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
45
 
46
  if uploaded_file is not None:
47
- # Read image as OpenCV BGR
48
  file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
49
- img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
50
-
51
- # Convert BGR β†’ RGB for display
52
- img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
53
- st.image(img_rgb, caption="Uploaded Image", use_container_width=True)
54
 
55
- # ------------------------------
56
- # Preprocess & predict
57
- # ------------------------------
58
- img_resized = cv2.resize(img, (64, 64))
59
- img_flat = img_resized.flatten().reshape(1, -1)
60
- img_scaled = scaler.transform(img_flat)
61
- img_pca = pca.transform(img_scaled)
62
- pred = svc.predict(img_pca)[0]
63
- label = label_map.get(pred, "Unknown")
64
 
65
- # Show prediction
66
- st.success(f"Predicted Class: {label}")
 
 
 
 
 
1
  import streamlit as st
 
2
  import numpy as np
3
  import cv2
4
 
5
+ st.title("πŸ” Dummy Image Upload Test")
 
 
 
 
 
 
 
 
 
 
6
 
7
+ uploaded_file = st.file_uploader("Upload an image...", type=["jpg", "jpeg", "png"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
9
  if uploaded_file is not None:
10
+ # Read file into OpenCV format
11
  file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
12
+ img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR) # decode image
 
 
 
 
13
 
14
+ if img is None:
15
+ st.error("❌ OpenCV failed to load the image. (cv2.imdecode returned None)")
16
+ else:
17
+ # Show original BGR and converted RGB
18
+ st.image(img, channels="BGR", caption="Uploaded Image (BGR)", use_container_width=True)
19
+ st.image(cv2.cvtColor(img, cv2.COLOR_BGR2RGB), caption="Uploaded Image (RGB)", use_container_width=True)
 
 
 
20
 
21
+ # Show shape info
22
+ st.write("Image shape:", img.shape)