0% found this document useful (0 votes)
16 views5 pages

CRATERS Entire Program

The document outlines a project focused on detecting craters using image processing and machine learning techniques. It includes data loading, preprocessing, visualization of bounding boxes, and training a YOLO model for object detection. The trained model is saved for future use and the document provides code for various steps involved in the process.

Uploaded by

haris.22aim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views5 pages

CRATERS Entire Program

The document outlines a project focused on detecting craters using image processing and machine learning techniques. It includes data loading, preprocessing, visualization of bounding boxes, and training a YOLO model for object detection. The trained model is saved for future use and the document provides code for various steps involved in the process.

Uploaded by

haris.22aim
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

CRATERS

import os #read/write in the local file system

import cv2 #image processing

import matplotlib.pyplot as plt #x,y-axis plotting

import pandas as pd #data manupulation and analysis library (handles data - update,read)

import numpy as np #mathematical operations

import plotly.express as px #mathematical plot - using images

import plotly.graph_objects as go # ''

import seaborn as sns #data visualization

from sklearn.model_selection import train_test_split #train and test data splitups

train_img_path = "/content/drive/MyDrive/Craters project/train/images"

train_lbl_path = "/content/drive/MyDrive/Craters project/train/labels"

valid_img_path = "/content/drive/MyDrive/Craters project/valid/images"

valid_lbl_path = "/content/drive/MyDrive/Craters project/valid/labels"

test_img_path = "/content/drive/MyDrive/Craters project/test/images"

test_lbl_path = "/content/drive/MyDrive/Craters project/test/labels"

model_path = "/content/drive/MyDrive/Craters project/best.pt"

data_yaml_path = "/content/drive/MyDrive/Craters project/data.yaml"

# image definition and identification (like: identifying as male or female)

def load_labels(label_path):

label_files = os.listdir(label_path)

data = []

classes = set()

for file in label_files:

with open(os.path.join(label_path, file), 'r') as f:

lines = f.readlines()

for line in lines:

parts = list(map(float, line.strip().split()))

data.append([file, *parts])

classes.add(int(parts[0]))
df = pd.DataFrame(data, columns=['file', 'class', 'x_center', 'y_center', 'width', 'height'])

return df, sorted(classes)

train_labels, train_classes = load_labels(train_lbl_path)

valid_labels, valid_classes = load_labels(valid_lbl_path)

test_labels, test_classes = load_labels(test_lbl_path)

# image seperation for training, testing and valid class (11,000 - total images added)

# class[0] - has image

# class[1,2,3,4,5....] - do not have image

all_classes = sorted(set(train_classes + valid_classes + test_classes))

class_names = [f'class_{i}' for i in all_classes]

# Output for the above cell

print("Train Labels")

print(train_labels.head())

print("\nValidation Labels")

print(valid_labels.head())

print("\nTest Labels")

print(test_labels.head())

# storing in a file extension .yaml

# Why? becz of Yolo model(deep learning)

data_yaml_content = f"""

train: {train_img_path}

val: {valid_img_path}

test: {test_img_path}

nc: {len(all_classes)} # number of classes

names: {class_names} # class names

"""

with open(data_yaml_path, 'w') as f:

f.write(data_yaml_content)
# Plotting distribution of bounding box sizes

# making a box like identification in craters

# classified based on area

def plot_bounding_box_distribution(labels, title):

labels['area'] = labels['width'] * labels['height']

fig = px.histogram(labels, x='area', nbins=50, title=title)

fig.show()

plot_bounding_box_distribution(train_labels, 'Train Bounding Box Area Distribution')

plot_bounding_box_distribution(valid_labels, 'Validation Bounding Box Area Distribution')

plot_bounding_box_distribution(test_labels, 'Test Bounding Box Area Distribution')

# Image Preprocessing and Visualization

# preprocessing: removing the unwanted data

# bgr to rgb: to process the data and vizualization purpose

def visualize_sample_images(image_path, label_df, n_samples=5):

image_files = os.listdir(image_path)[:n_samples]

for img_file in image_files:

img_path = os.path.join(image_path, img_file)

img = cv2.imread(img_path)

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

fig, ax = plt.subplots(1, 1, figsize=(10, 10)) # standard size to display

ax.imshow(img)

labels = label_df[label_df['file'] == img_file]

for _, label in labels.iterrows():

x_center = int(label['x_center'] * img.shape[1])

y_center = int(label['y_center'] * img.shape[0])

width = int(label['width'] * img.shape[1])

height = int(label['height'] * img.shape[0])

x_min = x_center - width // 2


y_min = y_center - height // 2

rect = plt.Rectangle((x_min, y_min), width, height, edgecolor='red', facecolor='none',


linewidth=2)

ax.add_patch(rect)

plt.title(f'Sample Image: {img_file}')

plt.axis('off')

plt.show()

visualize_sample_images(train_img_path, train_labels)

visualize_sample_images(valid_img_path, valid_labels)

visualize_sample_images(test_img_path, test_labels)

# You Only Look Once

# Detecting an specific object from the image

!pip install ultralytics

from ultralytics import YOLO

model = YOLO('yolov8n.pt')

model.train(data=data_yaml_path, epochs=10)

# Visualize sample detections

# Created without label

def visualize_detections(model, image_path, n_samples=10):

image_files = os.listdir(image_path)[:n_samples]

for img_file in image_files:

img_path = os.path.join(image_path, img_file)

img = cv2.imread(img_path)

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

results = model(img_path)

fig, ax = plt.subplots(1, 1, figsize=(10, 10))

ax.imshow(img)
for result in results[0].boxes:

x_min, y_min, x_max, y_max = result.xyxy[0].tolist()

conf = result.conf[0].item()

rect = plt.Rectangle((x_min, y_min), x_max - x_min, y_max - y_min, edgecolor='red',


facecolor='none', linewidth=2)

ax.add_patch(rect)

ax.text(x_min, y_min, f'{conf:.2f}', bbox=dict(facecolor='yellow', alpha=0.5))

plt.title(f'Detection in: {img_file}')

plt.axis('off')

plt.show()

visualize_detections(model, test_img_path)

print("Model training, evaluation, and sample visualization completed. The trained model is saved at
'/kaggle/working/best_model.pt'.")

You might also like