iamsuman commited on
Commit
b8a7aa4
1 Parent(s): ad4c861

added demo model for garbage classification

Browse files
Files changed (3) hide show
  1. .gitignore +11 -0
  2. app.py +61 -0
  3. requirements.txt +8 -0
.gitignore ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ flagged/
2
+ *.png
3
+ *.jpg
4
+ *.mp4
5
+ *.jpeg
6
+ *.mkv
7
+ .DS_Store
8
+ gradio_cached_examples/
9
+ venv/
10
+ .locks/
11
+ models-*
app.py ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from ast import mod
2
+ import gradio as gr
3
+ from huggingface_hub import hf_hub_download
4
+ from PIL import Image
5
+ import numpy as np
6
+ import tensorflow as tf
7
+
8
+
9
+ # Load the pre-trained model from Hugging Face
10
+ model_name = "iamsuman/household-waste-EfficientNetV2M"
11
+
12
+ model_path = hf_hub_download(repo_id=model_name, filename="EfficientNetV2M.h5")
13
+
14
+
15
+ model = tf.keras.models.load_model(model_path)
16
+
17
+
18
+ # Define a manual mapping of label indices to human-readable labels
19
+ index_to_label = {
20
+ 0: "battery",
21
+ 1: "biological",
22
+ 2: "cardboard",
23
+ 3: "clothes",
24
+ 4: "glass",
25
+ 5: "metal",
26
+ 6: "paper",
27
+ 7: "plastic",
28
+ 8: "shoes",
29
+ 9: "trash"
30
+ }
31
+
32
+ def classify_image(image):
33
+
34
+ image = Image.fromarray(image.astype('uint8'), 'RGB')
35
+
36
+ image = image.resize((400, 400))
37
+ # Convert the PIL Image to a format compatible with the model
38
+ image = np.array(image)
39
+ image = np.expand_dims(image, axis=0) # Add batch dimension
40
+ image = tf.keras.applications.efficientnet.preprocess_input(image)
41
+
42
+ # Make prediction
43
+ preds = model.predict(image)
44
+
45
+ # Retrieve the highest probability class label index
46
+ predicted_class_idx = np.argmax(preds, axis=-1)[0]
47
+
48
+ # Convert the index to the model's class label
49
+ label = index_to_label.get(predicted_class_idx, "Unknown Label")
50
+
51
+ return label.capitalize()
52
+
53
+ # Create Gradio interface
54
+ iface = gr.Interface(fn=classify_image,
55
+ inputs=gr.Image(), # Accepts image of any size
56
+ outputs=gr.Label(),
57
+ title="Household Waste Classification with EfficientNetV2M",
58
+ description="Upload an image of household waste, and the model will classify it.")
59
+
60
+ # Launch the app
61
+ iface.launch()
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ # Base ----------------------------------------
2
+ huggingface_hub>=0.23.4
3
+ numpy>=1.18.5
4
+ torch>=1.7.0
5
+ Pillow>=7.1.2
6
+
7
+ # Logging -------------------------------------
8
+ tensorflow==2.15