└── README.md /README.md: -------------------------------------------------------------------------------- 1 | # tree-classification 2 | import tensorflow as tf 3 | from tensorflow import keras 4 | from tensorflow.keras import layers 5 | import numpy as np 6 | import cv2 7 | import os 8 | 9 | # Define model architecture 10 | def create_model(): 11 | model = keras.Sequential([ 12 | layers.Conv2D(32, (3, 3), activation='relu', input_shape=(128, 128, 3)), 13 | layers.MaxPooling2D((2, 2)), 14 | layers.Conv2D(64, (3, 3), activation='relu'), 15 | layers.MaxPooling2D((2, 2)), 16 | layers.Conv2D(128, (3, 3), activation='relu'), 17 | layers.MaxPooling2D((2, 2)), 18 | layers.Flatten(), 19 | layers.Dense(128, activation='relu'), 20 | layers.Dense(5, activation='softmax') # Assuming 5 types of trees 21 | ]) 22 | 23 | model.compile(optimizer='adam', 24 | loss='sparse_categorical_crossentropy', 25 | metrics=['accuracy']) 26 | 27 | return model 28 | 29 | # Load and preprocess image 30 | def preprocess_image(image_path): 31 | img = cv2.imread(image_path) 32 | img = cv2.resize(img, (128, 128)) 33 | img = img / 255.0 # Normalize 34 | return np.expand_dims(img, axis=0) 35 | 36 | # Load trained model or create a new one 37 | model = create_model() 38 | 39 | # Example prediction 40 | def predict_tree(image_path): 41 | classes = ['Fir', 'Spruce', 'Pine', 'Cedar', 'Christmas Tree'] # Example classes 42 | img = preprocess_image(image_path) 43 | prediction = model.predict(img) 44 | predicted_class = classes[np.argmax(prediction)] 45 | 46 | print(f"Predicted Tree Type: {predicted_class}") 47 | return predicted_class 48 | 49 | # Example usage 50 | # predict_tree("example_tree.jpg") 51 | ear 52 | --------------------------------------------------------------------------------