handex commited on
Commit
bda25d6
·
1 Parent(s): a462bdd

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +20 -10
src/streamlit_app.py CHANGED
@@ -11,19 +11,19 @@ IMG_SIZE = 300
11
  CLASS_NAMES = ['cup', 'fork', 'glass', 'knife', 'plate', 'spoon']
12
 
13
  # -----------------------------------
14
- # MODEL LOADING
15
  # -----------------------------------
16
  @st.cache_resource
17
  def load_model_file():
18
  model_path = os.path.join(os.path.dirname(__file__), "kitchen1.keras")
19
- # TF 2.14'te de bu yöntem çalışır
20
- return tf.keras.models.load_model(model_path, compile=False)
 
 
 
 
21
 
22
- try:
23
- model = load_model_file()
24
- except Exception as e:
25
- st.error(f"Model yüklenirken hata: {e}")
26
- st.stop()
27
 
28
  # -----------------------------------
29
  # IMAGE PREPROCESS
@@ -31,12 +31,19 @@ except Exception as e:
31
  def process_image(img):
32
  img = img.resize((IMG_SIZE, IMG_SIZE))
33
  img = np.array(img)
 
 
34
  if img.ndim == 2:
35
  img = np.stack([img]*3, axis=-1)
36
  elif img.shape[-1] == 4:
37
  img = img[..., :3]
38
 
 
39
  img = np.expand_dims(img, 0)
 
 
 
 
40
  img = tf.keras.applications.efficientnet.preprocess_input(img)
41
  return img
42
 
@@ -48,9 +55,10 @@ st.write("Upload an image to classify kitchen items using EfficientNetB3.")
48
 
49
  uploaded = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
50
 
51
- if uploaded:
52
  try:
53
  img = Image.open(uploaded).convert("RGB")
 
54
  col1, col2 = st.columns(2)
55
 
56
  with col1:
@@ -58,6 +66,7 @@ if uploaded:
58
 
59
  with col2:
60
  st.write("Classifying...")
 
61
  image_tensor = process_image(img)
62
  preds = model.predict(image_tensor)
63
 
@@ -68,5 +77,6 @@ if uploaded:
68
  st.success(f"Prediction: **{predicted_class.upper()}**")
69
  st.metric("Confidence", f"{confidence * 100:.2f}%")
70
  st.progress(int(confidence * 100))
 
71
  except Exception as e:
72
- st.error(f"Hata: {e}")
 
11
  CLASS_NAMES = ['cup', 'fork', 'glass', 'knife', 'plate', 'spoon']
12
 
13
  # -----------------------------------
14
+ # MODEL LOADING (KERAS 3)
15
  # -----------------------------------
16
  @st.cache_resource
17
  def load_model_file():
18
  model_path = os.path.join(os.path.dirname(__file__), "kitchen1.keras")
19
+ try:
20
+ # Keras 3'te model yükleme
21
+ return tf.keras.models.load_model(model_path, compile=False)
22
+ except Exception as e:
23
+ st.error(f"Model yüklenemedi: {e}")
24
+ return None
25
 
26
+ model = load_model_file()
 
 
 
 
27
 
28
  # -----------------------------------
29
  # IMAGE PREPROCESS
 
31
  def process_image(img):
32
  img = img.resize((IMG_SIZE, IMG_SIZE))
33
  img = np.array(img)
34
+
35
+ # Boyut kontrolleri
36
  if img.ndim == 2:
37
  img = np.stack([img]*3, axis=-1)
38
  elif img.shape[-1] == 4:
39
  img = img[..., :3]
40
 
41
+ # Batch boyutu ekle: (1, 300, 300, 3)
42
  img = np.expand_dims(img, 0)
43
+
44
+ # EfficientNet B3 Preprocess
45
+ # Keras 3'te bu fonksiyon bazen farklı path'te olabilir,
46
+ # ama tf.keras.applications genelde güvenlidir.
47
  img = tf.keras.applications.efficientnet.preprocess_input(img)
48
  return img
49
 
 
55
 
56
  uploaded = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
57
 
58
+ if uploaded and model is not None:
59
  try:
60
  img = Image.open(uploaded).convert("RGB")
61
+
62
  col1, col2 = st.columns(2)
63
 
64
  with col1:
 
66
 
67
  with col2:
68
  st.write("Classifying...")
69
+
70
  image_tensor = process_image(img)
71
  preds = model.predict(image_tensor)
72
 
 
77
  st.success(f"Prediction: **{predicted_class.upper()}**")
78
  st.metric("Confidence", f"{confidence * 100:.2f}%")
79
  st.progress(int(confidence * 100))
80
+
81
  except Exception as e:
82
+ st.error(f"Hata oluştu: {e}")