├── File 3 ├── README.md └── file1 /File 3: -------------------------------------------------------------------------------- 1 | import os 2 | import pandas as pd 3 | 4 | def extract_image_metadata_to_excel(data_dir='data', output_file='plastic_waste_dataset.xlsx'): 5 | records = [] 6 | 7 | for label in ['plastic', 'non_plastic']: 8 | class_dir = os.path.join(data_dir, label) 9 | for filename in os.listdir(class_dir): 10 | if filename.endswith(".png"): 11 | records.append({ 12 | 'file_name': filename, 13 | 'label': label, 14 | 'file_path': os.path.join(class_dir, filename) 15 | }) 16 | 17 | df = pd.DataFrame(records) 18 | df.to_excel(output_file, index=False) 19 | print(f"Dataset metadata saved to {output_file}") 20 | 21 | if __name__ == "__main__": 22 | extract_image_metadata_to_excel() 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AI for Automated Classification of Plastic Waste in Ocean Images 2 | 3 | This project uses synthetic data to simulate ocean images containing plastic waste and trains a Convolutional Neural Network (CNN) to classify them. 4 | 5 | ## Features 6 | 7 | - Synthetic data generator 8 | - Binary image classification (plastic vs. non-plastic) 9 | - CNN built using TensorFlow/Keras 10 | - Training, evaluation, and prediction 11 | 12 | ## Setup 13 | 14 | ```bash 15 | pip install tensorflow matplotlib numpy 16 | python plastic_classifier.py 17 | 18 | Predicting a New Image 19 | 20 | Uncomment and run: 21 | 22 | predict_image('data/plastic/plastic_1.png') 23 | 24 | Directory Structure 25 | 26 | data/: Auto-generated synthetic images 27 | 28 | model/: Saved model weights 29 | 30 | 31 | --- 32 | 33 | Would you like to upgrade the project with: 34 | - real image augmentation? 35 | - Streamlit dashboard for visual interface? 36 | - Deployment tips (e.g., Flask, FastAPI, or Hugging Face)? 37 | 38 | Let me know how far you want to go with this! 39 | -------------------------------------------------------------------------------- /file1: -------------------------------------------------------------------------------- 1 | import os 2 | import numpy as np 3 | import matplotlib.pyplot as plt 4 | import tensorflow as tf 5 | from tensorflow.keras.models import Sequential 6 | from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout 7 | from tensorflow.keras.preprocessing.image import ImageDataGenerator, array_to_img 8 | 9 | # STEP 1: Generate synthetic data (if data doesn't exist) 10 | def generate_synthetic_data(base_dir='data', num_samples=500, img_size=(128, 128)): 11 | os.makedirs(base_dir, exist_ok=True) 12 | for category in ['plastic', 'non_plastic']: 13 | category_path = os.path.join(base_dir, category) 14 | os.makedirs(category_path, exist_ok=True) 15 | for i in range(num_samples): 16 | img = np.random.rand(*img_size, 3) # Random noise image 17 | if category == 'plastic': 18 | img[30:60, 30:60] = [1.0, 1.0, 0.0] # Simulate yellowish floating object 19 | else: 20 | img *= 0.5 # Darker background 21 | 22 | img = (img * 255).astype(np.uint8) 23 | plt.imsave(f'{category_path}/{category}_{i}.png', img) 24 | 25 | # STEP 2: Data preparation 26 | def prepare_data(data_dir='data', img_size=(128, 128), batch_size=32): 27 | datagen = ImageDataGenerator(rescale=1./255, validation_split=0.2) 28 | 29 | train_gen = datagen.flow_from_directory( 30 | data_dir, 31 | target_size=img_size, 32 | batch_size=batch_size, 33 | class_mode='binary', 34 | subset='training' 35 | ) 36 | 37 | val_gen = datagen.flow_from_directory( 38 | data_dir, 39 | target_size=img_size, 40 | batch_size=batch_size, 41 | class_mode='binary', 42 | subset='validation' 43 | ) 44 | 45 | return train_gen, val_gen 46 | 47 | # STEP 3: CNN model 48 | def build_model(input_shape=(128, 128, 3)): 49 | model = Sequential([ 50 | Conv2D(32, (3, 3), activation='relu', input_shape=input_shape), 51 | MaxPooling2D(2, 2), 52 | Conv2D(64, (3, 3), activation='relu'), 53 | MaxPooling2D(2, 2), 54 | Flatten(), 55 | Dense(128, activation='relu'), 56 | Dropout(0.3), 57 | Dense(1, activation='sigmoid') 58 | ]) 59 | model.compile(optimizer='adam', 60 | loss='binary_crossentropy', 61 | metrics=['accuracy']) 62 | return model 63 | 64 | # STEP 4: Train and Save Model 65 | def train_model(): 66 | generate_synthetic_data() 67 | train_gen, val_gen = prepare_data() 68 | model = build_model() 69 | model.summary() 70 | model.fit(train_gen, epochs=5, validation_data=val_gen) 71 | os.makedirs('model', exist_ok=True) 72 | model.save('model/plastic_classifier.h5') 73 | 74 | # STEP 5: Prediction on new image 75 | def predict_image(img_path, model_path='model/plastic_classifier.h5', img_size=(128, 128)): 76 | model = tf.keras.models.load_model(model_path) 77 | img = tf.keras.preprocessing.image.load_img(img_path, target_size=img_size) 78 | img_array = tf.keras.preprocessing.image.img_to_array(img) / 255.0 79 | img_array = np.expand_dims(img_array, axis=0) 80 | prediction = model.predict(img_array)[0][0] 81 | label = "Plastic" if prediction > 0.5 else "Non-Plastic" 82 | print(f"Prediction: {label} ({prediction:.2f})") 83 | 84 | if __name__ == "__main__": 85 | train_model() 86 | # Example prediction 87 | # predict_image('data/plastic/plastic_1.png') 88 | --------------------------------------------------------------------------------