Views
No views yet
pip install tensorflow huggingface-hub numpy pillow1from huggingface_hub import hf_hub_download
2import tensorflow as tf
3import numpy as np
4from tensorflow.keras.preprocessing import image
5
6# Download and load model
7repo_id = "MOHAMMED7M7/waste-classification-model"
8filename = "waste_classification_model.keras"
9model_path = hf_hub_download(repo_id=repo_id, filename=filename)
10model = tf.keras.models.load_model(model_path)
11
12# Preprocess image
13def preprocess_image(img_path):
14 img = image.load_img(img_path, target_size=(128, 128))
15 img_array = image.img_to_array(img)
16 img_array = np.expand_dims(img_array, axis=0)
17 img_array /= 255.0
18 return img_array
19
20# Make prediction
21image_path = 'path/to/your/image.jpg'
22processed_image = preprocess_image(image_path)
23predictions = model.predict(processed_image)
24
25# Get result
26class_names = ['cardboard', 'glass', 'metal', 'paper', 'plastic', 'trash']
27predicted_class_index = np.argmax(predictions)
28predicted_class = class_names[predicted_class_index]
29confidence = predictions[0][predicted_class_index]
30
31print(f"Predicted class: {predicted_class}")
32print(f"Confidence: {confidence:.2%}")