File size: 2,704 Bytes
68fcd36
 
 
 
 
 
 
 
1c62f5d
 
 
68fcd36
 
1c62f5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2bbc4b0
1c62f5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68fcd36
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
---
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.")
```