| --- |
| tags: |
| - image-classification |
| - image |
| - data-classification |
| - image-categorisation |
| - data-categoriasation |
| pipeline_tag: image-classification |
| language: |
| - de |
| - en |
| --- |
| # Model Card for Model ID |
| This model is a Jewelry Classifier. Just upload an image of one of the categories named below and the model will classify it for you. |
| - Pendant |
| - Bracelet |
| - Chain |
| - Earring |
| - Ring |
| - Watch |
|
|
| # How to use? |
| Before following the steps below, please install these dependencies: |
|
|
| ```pyhton |
| numpy==1.26.4 |
| keras==3.3.3 |
| pillow==10.3.0 |
| ``` |
| ### Step1: Load the Model (jewelry_classification.h5) |
| Download 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. |
|
|
|
|
| ```python |
| model = load_model('jewelry_classification_model.h5') |
| |
| class_labels = ['Anhänger', 'Armbänder', 'Ketten', 'Ohrringe', 'Ringe', 'Uhren'] |
| ``` |
|
|
| ### Step 2: Preprocess your images |
| Before giving images to the model, that image needs to be preprocessed to get a numpy array. You can just use the below function. |
|
|
| ```python |
| def preprocess_image(img): |
| try: |
| img = Image.open(img) |
| img = img.resize((224, 224)) |
| img_array = img_to_array(img) |
| img_array = np.expand_dims(img_array, axis=0) |
| img_array = img_array.astype(np.float32) / 255.0 |
| return img_array |
| except Exception as error: |
| st.error(f"An error occurred during image preprocessing: {error}") |
| return None |
| ``` |
|
|
| ### Step 3: Predict the output |
| In this step the preprocessed image could be given to the model to get the classification. Below is the sample code snippet. |
|
|
| ```python |
| def choose_category(img, is_url=True): |
| try: |
| processed_img = preprocess_image(img, is_url) |
| if processed_img is not None: |
| preds = model.predict(processed_img) |
| category = class_labels[np.argmax(preds)] |
| confidence = np.max(preds) |
| |
| return category, confidence*100 |
| return 'Other', 0 |
| except Exception as e: |
| st.error(f"An error occurred during prediction: {e}") |
| return 'Other', 0 |
| ``` |
| ### Step 4(optional): Streamlit UI |
| Use the below snippet to make an UI Application using the model |
|
|
| ```python |
| # UI interface |
| import streamlit as st |
| st.title("Jewelry Classification") |
| |
| uploaded_file = st.file_uploader("Choose an image...", type=["jpg"]) |
| if st.button("Classify"): |
| if uploaded_file is not None: |
| category, confidence = choose_category(uploaded_file, is_url=False) |
| st.write(f"Predicted Category: **{category}** with confidence **{confidence:.2f}%**") |
| else: |
| st.error("Please upload an image file.") |
| ``` |
|
|
|
|