Ars135 commited on
Commit
a4d412e
·
verified ·
1 Parent(s): b4821c4

Upload 7 files

Browse files
best_waste_classification_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:0092396cc692370cf36693bd81ece842fca56c897b6b4bfa1b74fc8fb0620b51
3
+ size 12924688
classifier.py ADDED
@@ -0,0 +1,48 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ from PIL import Image
5
+
6
+ class WasteClassifier:
7
+ def __init__(self, model_path, labels_path):
8
+ # Load the TFLite model
9
+ self.interpreter = tf.lite.Interpreter(model_path=model_path)
10
+ self.interpreter.allocate_tensors()
11
+
12
+ # Get input and output details
13
+ self.input_details = self.interpreter.get_input_details()
14
+ self.output_details = self.interpreter.get_output_details()
15
+
16
+ # Load labels
17
+ with open(labels_path, 'r') as f:
18
+ self.labels = [line.strip().split(':')[1] for line in f.readlines()]
19
+
20
+ def preprocess_image(self, image_path):
21
+ img = Image.open(image_path).convert('RGB')
22
+ img = img.resize((224, 224))
23
+ img_array = np.array(img).astype(np.float32) / 255.0
24
+ img_array = np.expand_dims(img_array, axis=0)
25
+ return img_array
26
+
27
+ def predict(self, image_path):
28
+ # Preprocess image
29
+ img_array = self.preprocess_image(image_path)
30
+
31
+ # Set input tensor
32
+ self.interpreter.set_tensor(self.input_details[0]['index'], img_array)
33
+
34
+ # Run inference
35
+ self.interpreter.invoke()
36
+
37
+ # Get output tensor
38
+ output_data = self.interpreter.get_tensor(self.output_details[0]['index'])
39
+ predicted_class = np.argmax(output_data[0])
40
+ confidence = float(np.max(output_data[0]))
41
+
42
+ return self.labels[predicted_class], confidence
43
+
44
+ # Example usage
45
+ if __name__ == "__main__":
46
+ classifier = WasteClassifier('waste_classification.tflite', 'labels.txt')
47
+ prediction, confidence = classifier.predict('sample_image.jpg')
48
+ print(f"Predicted: {prediction} with {confidence:.2%} confidence")
flask_app.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ from flask import Flask, request, render_template, jsonify
3
+ from tensorflow.keras.models import load_model
4
+ from tensorflow.keras.preprocessing import image
5
+ import numpy as np
6
+ import os
7
+ from werkzeug.utils import secure_filename
8
+ import cv2
9
+
10
+ app = Flask(__name__)
11
+ app.config['UPLOAD_FOLDER'] = 'uploads'
12
+ app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max file size
13
+
14
+ # Load the trained model
15
+ model = load_model('/content/drive/MyDrive/best_waste_classification_model.h5')
16
+ classes = ['cardboard', 'glass', 'metal', 'paper', 'plastic', 'trash']
17
+
18
+ # Enhanced recycling tips dictionary
19
+ recycling_tips = {
20
+ # ... (copy the enhanced recycling tips dictionary here)
21
+ }
22
+
23
+ @app.route('/')
24
+ def home():
25
+ return render_template('index.html')
26
+
27
+ @app.route('/predict', methods=['POST'])
28
+ def predict():
29
+ if 'file' not in request.files:
30
+ return jsonify({'error': 'No file uploaded'})
31
+
32
+ file = request.files['file']
33
+ if file.filename == '':
34
+ return jsonify({'error': 'No file selected'})
35
+
36
+ if file:
37
+ # Save the uploaded file
38
+ filename = secure_filename(file.filename)
39
+ filepath = os.path.join(app.config['UPLOAD_FOLDER'], filename)
40
+ os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
41
+ file.save(filepath)
42
+
43
+ # Preprocess the image
44
+ img = image.load_img(filepath, target_size=(224, 224))
45
+ img_array = image.img_to_array(img) / 255.0
46
+ img_array = np.expand_dims(img_array, axis=0)
47
+
48
+ # Make prediction
49
+ prediction = model.predict(img_array)
50
+ predicted_class_idx = np.argmax(prediction)
51
+ predicted_class = classes[predicted_class_idx]
52
+ confidence = float(np.max(prediction))
53
+
54
+ # Get recycling tips
55
+ tips_info = recycling_tips.get(predicted_class, {})
56
+ tips = tips_info.get('tips', ['No specific tips available.'])
57
+ preparation = tips_info.get('preparation', 'No specific preparation instructions.')
58
+ recyclability = tips_info.get('recyclability', 'Unknown recyclability.')
59
+ common_uses = tips_info.get('common_uses', 'Unknown common uses.')
60
+
61
+ # Select a random tip from the list
62
+ import random
63
+ random_tip = random.choice(tips) if tips else 'No specific tips available.'
64
+
65
+ return jsonify({
66
+ 'class': predicted_class,
67
+ 'confidence': confidence,
68
+ 'tip': random_tip,
69
+ 'preparation': preparation,
70
+ 'recyclability': recyclability,
71
+ 'common_uses': common_uses,
72
+ 'all_tips': tips
73
+ })
74
+
75
+ if __name__ == '__main__':
76
+ app.run(debug=True)
labels.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ 0:cardboard
2
+ 1:glass
3
+ 2:metal
4
+ 3:paper
5
+ 4:plastic
6
+ 5:trash
recycling_tips.json ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cardboard": {
3
+ "tips": [
4
+ "Flatten boxes before recycling to save space",
5
+ "Remove any tape, labels, or plastic windows",
6
+ "Keep cardboard dry and clean"
7
+ ],
8
+ "preparation": "Flatten and remove contaminants",
9
+ "recyclability": "Highly recyclable"
10
+ },
11
+ "glass": {
12
+ "tips": [
13
+ "Rinse containers to remove residue",
14
+ "Remove metal lids and caps (recycle separately)",
15
+ "Do not mix different colored glass if required by your facility"
16
+ ],
17
+ "preparation": "Rinse and remove non-glass components",
18
+ "recyclability": "Infinitely recyclable"
19
+ },
20
+ "metal": {
21
+ "tips": [
22
+ "Rinse cans to remove food residue",
23
+ "Remove paper labels if possible",
24
+ "Crush aluminum cans to save space"
25
+ ],
26
+ "preparation": "Rinse and separate by type if needed",
27
+ "recyclability": "Highly recyclable"
28
+ },
29
+ "paper": {
30
+ "tips": [
31
+ "Keep paper dry and clean",
32
+ "Remove any plastic coatings or laminations",
33
+ "Shred confidential documents before recycling"
34
+ ],
35
+ "preparation": "Keep dry and remove contaminants",
36
+ "recyclability": "Highly recyclable"
37
+ },
38
+ "plastic": {
39
+ "tips": [
40
+ "Check the recycling number (1-7) on the item",
41
+ "Rinse containers to remove residue",
42
+ "Remove caps and pumps (often different plastic types)"
43
+ ],
44
+ "preparation": "Rinse and check local guidelines",
45
+ "recyclability": "Varies by type"
46
+ },
47
+ "trash": {
48
+ "tips": [
49
+ "Consider if items can be reused or repurposed",
50
+ "Remove any recyclable components",
51
+ "Dispose of properly in designated trash bins"
52
+ ],
53
+ "preparation": "Separate recyclables and compostables",
54
+ "recyclability": "Not recyclable"
55
+ }
56
+ }
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+
2
+ tensorflow>=2.0.0
3
+ numpy>=1.0.0
4
+ Pillow>=8.0.0
waste_classification.tflite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:755f30503dd49e3a710251e71dc432342a8ea41f8cb21879439b21e82937975d
3
+ size 19294464