beyondxlabs/ImageClassification
0
1---2tags:3- image-classification4- image5- data-classification6- image-categorisation7- data-categoriasation8pipeline_tag: image-classification9language:10- de11- en12---13# Model Card for Model ID14This model is a Jewelry Classifier. Just upload an image of one of the categories named below and the model will classify it for you.15- Pendant16- Bracelet17- Chain18- Earring19- Ring20- Watch21 22# How to use?23Before following the steps below, please install these dependencies:24 25```pyhton26numpy==1.26.4 27keras==3.3.328pillow==10.3.029```30### Step1: Load the Model (jewelry_classification.h5)31Download the model file from (https://huggingface.co/beyondxlabs/JewelryClassification/resolve/main/jewelry_classification.h5?download=true) and then use the below code snippet to load the model.32 33 34```python35model = load_model('jewelry_classification_model.h5')36 37class_labels = ['Anhänger', 'Armbänder', 'Ketten', 'Ohrringe', 'Ringe', 'Uhren']38```39 40### Step 2: Preprocess your images41Before giving images to the model, that image needs to be preprocessed to get a numpy array. You can just use the below function.42 43```python44def preprocess_image(img):45 try:46 img = Image.open(img)47 img = img.resize((224, 224))48 img_array = img_to_array(img)49 img_array = np.expand_dims(img_array, axis=0)50 img_array = img_array.astype(np.float32) / 255.051 return img_array52 except Exception as error:53 st.error(f"An error occurred during image preprocessing: {error}")54 return None55```56 57### Step 3: Predict the output58In this step the preprocessed image could be given to the model to get the classification. Below is the sample code snippet.59 60```python61def choose_category(img, is_url=True):62 try:63 processed_img = preprocess_image(img, is_url)64 if processed_img is not None:65 preds = model.predict(processed_img)66 category = class_labels[np.argmax(preds)]67 confidence = np.max(preds)68 69 return category, confidence*10070 return 'Other', 071 except Exception as e:72 st.error(f"An error occurred during prediction: {e}")73 return 'Other', 074```75### Step 4(optional): Streamlit UI76Use the below snippet to make an UI Application using the model77 78```python79# UI interface80import streamlit as st81st.title("Jewelry Classification")82 83uploaded_file = st.file_uploader("Choose an image...", type=["jpg"])84if st.button("Classify"):85 if uploaded_file is not None:86 category, confidence = choose_category(uploaded_file, is_url=False)87 st.write(f"Predicted Category: **{category}** with confidence **{confidence:.2f}%**")88 else:89 st.error("Please upload an image file.")90```91 92 