divya55 commited on
Commit
63fa8a2
Β·
verified Β·
1 Parent(s): ee3bd7d

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +79 -11
src/streamlit_app.py CHANGED
@@ -1,20 +1,88 @@
1
  import streamlit as st
2
- from PIL import Image
3
  import numpy as np
4
  import cv2
 
 
5
 
6
- st.title("Upload & View Image")
 
 
 
7
 
8
- uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
 
 
 
 
9
 
10
- # This prints info to Streamlit console
11
- st.write("Uploaded file:", uploaded_file)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
  if uploaded_file is not None:
14
- # Make sure to open the uploaded file
15
- image = Image.open(uploaded_file)
16
- st.image(image, caption="Original Image", use_column_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
- st.success("Image loaded successfully!")
19
- else:
20
- st.info("Please upload an image.")
 
1
  import streamlit as st
 
2
  import numpy as np
3
  import cv2
4
+ import pickle
5
+ from pathlib import Path
6
 
7
+ # ------------------------------
8
+ # Paths
9
+ # ------------------------------
10
+ SRC_PATH = Path(__file__).parent # points to src/
11
 
12
+ # ------------------------------
13
+ # Load models
14
+ # ------------------------------
15
+ with open(SRC_PATH / "pca_model.pkl", "rb") as f:
16
+ pca = pickle.load(f)
17
 
18
+ with open(SRC_PATH / "svc_model.pkl", "rb") as f:
19
+ svc = pickle.load(f)
20
+
21
+ with open(SRC_PATH / "scaler.pkl", "rb") as f:
22
+ scaler = pickle.load(f)
23
+
24
+ # ------------------------------
25
+ # Label mapping
26
+ # ------------------------------
27
+ label_map = {
28
+ 0: "fresh apple",
29
+ 1: "fresh banana",
30
+ 2: "fresh orange",
31
+ 3: "rotten apple",
32
+ 4: "rotten banana",
33
+ 5: "rotten orange"
34
+ }
35
+
36
+ # ------------------------------
37
+ # Streamlit UI
38
+ # ------------------------------
39
+ st.title("🍎 Fruit Image Classification")
40
+
41
+ # ---- Example images ----
42
+ st.subheader("Example Images")
43
+ example_images = [
44
+ "Fresh Apple.jpg",
45
+ "Fresh Banana.jpg",
46
+ "Fresh Orange.jpg",
47
+ "Rotten Apple.jpg",
48
+ "Rotten Banana.jpg",
49
+ "Rotten Orange.jpg"
50
+ ]
51
+
52
+ cols = st.columns(3)
53
+ for i, img_file in enumerate(example_images):
54
+ img_path = SRC_PATH / img_file
55
+ if img_path.exists():
56
+ img = cv2.imread(str(img_path))
57
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
58
+ cols[i % 3].image(img, caption=img_file.split(".")[0], width=150)
59
+ else:
60
+ st.warning(f"Image '{img_file}' not found.")
61
+
62
+ st.markdown("**Classes:** fresh apple, fresh banana, fresh orange, rotten apple, rotten banana, rotten orange")
63
+
64
+ # ------------------------------
65
+ # File uploader
66
+ # ------------------------------
67
+ uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
68
 
69
  if uploaded_file is not None:
70
+ # Read uploaded file as OpenCV image
71
+ file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
72
+ img = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
73
+
74
+ # Convert BGR β†’ RGB for display
75
+ img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
76
+ st.image(img_rgb, caption="Uploaded Image", use_container_width=True)
77
+
78
+ # ------------------------------
79
+ # Preprocess & predict
80
+ # ------------------------------
81
+ img_resized = cv2.resize(img, (64, 64))
82
+ img_flat = img_resized.flatten().reshape(1, -1)
83
+ img_scaled = scaler.transform(img_flat)
84
+ img_pca = pca.transform(img_scaled)
85
+ pred = svc.predict(img_pca)[0]
86
+ label = label_map.get(pred, "Unknown")
87
 
88
+ st.success(f"Predicted Class: {label}")