Views
No views yet
1numpy==1.26.4
2keras==3.3.3
3pillow==10.3.01model = load_model('jewelry_classification_model.h5')
2
3class_labels = ['Anhänger', 'Armbänder', 'Ketten', 'Ohrringe', 'Ringe', 'Uhren']1def preprocess_image(img):
2 try:
3 img = Image.open(img)
4 img = img.resize((224, 224))
5 img_array = img_to_array(img)
6 img_array = np.expand_dims(img_array, axis=0)
7 img_array = img_array.astype(np.float32) / 255.0
8 return img_array
9 except Exception as error:
10 st.error(f"An error occurred during image preprocessing: {error}")
11 return None1def choose_category(img, is_url=True):
2 try:
3 processed_img = preprocess_image(img, is_url)
4 if processed_img is not None:
5 preds = model.predict(processed_img)
6 category = class_labels[np.argmax(preds)]
7 confidence = np.max(preds)
8
9 return category, confidence*100
10 return 'Other', 0
11 except Exception as e:
12 st.error(f"An error occurred during prediction: {e}")
13 return 'Other', 01# UI interface
2import streamlit as st
3st.title("Jewelry Classification")
4
5uploaded_file = st.file_uploader("Choose an image...", type=["jpg"])
6if st.button("Classify"):
7 if uploaded_file is not None:
8 category, confidence = choose_category(uploaded_file, is_url=False)
9 st.write(f"Predicted Category: **{category}** with confidence **{confidence:.2f}%**")
10 else:
11 st.error("Please upload an image file.")