already satisfied: python-slugify in /usr/local/lib/python3.10/dist-packages (from kaggle) (8.0.4)
Requirement already satisfied: urllib3 in /usr/local/lib/python3.10/dist-packages (from kaggle) (2.0.7)
Requirement already satisfied: bleach in /usr/local/lib/python3.10/dist-packages (from kaggle) (6.1.0)
Requirement already satisfied: webencodings in /usr/local/lib/python3.10/dist-packages (from bleach->kaggle) (0.5.1)
Requirement already satisfied: text-unidecode>=1.3 in /usr/local/lib/python3.10/dist-packages (from python-slugify->kaggle) (1.3)
Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.10/dist-packages (from requests->kaggle) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/dist-packages (from requests->kaggle) (3.7)
Dataset URL: https://www.kaggle.com/datasets/kpgeek/kitti-roadlane-detection-dataset-224-x-224
License(s): unknown
kitti-roadlane-detection-dataset-224-x-224.zip: Skipping, found more recently modified local copy (use --force to force download)
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-3-88028bc57aa3> in <cell line: 97>()
     95 
     96 train_images, train_masks = load_data('kitti_road/data_road_224/training')  # Update path
---> 97 val_images, val_masks = load_data('kitti_road/data_road_224/validation')   # Update path
     98 
     99 # Train the U-Net model

<ipython-input-3-88028bc57aa3> in load_data(data_dir, img_size)
     78     images = []
     79     masks = []
---> 80     for img_file in os.listdir(data_dir):
     81         if img_file.endswith('.png') and 'road' in img_file:
     82             img = cv2.imread(os.path.join(data_dir, img_file))

FileNotFoundError: [Errno 2] No such file or directory: 'kitti_road/data_road_224/validation'



fyi
data_road_224(2 directories)
About this directory

Add Suggestion
The classic KITTI urban road dataset resized to 224 x 224 image resolution for using in transfer learning models like GoogleNet, Resnet-50, MobileNet-V2, etc, which expect the input of size 224 x 224.

testing

2 directories
training

3 directories
training(3 directories)
About this directory

Add Suggestion
gt_image_2 consist of the Labeled ground truth images and image_2 consist of original road images.

calib

289 files
gt_image_2

290 files
image_2

289 files
testing(2 directories)
About this directory

Add Suggestion
Test images directory

calib

290 files
image_2

291 files
is data structure



# Install necessary libraries (if not already)
!pip install opencv-python-headless
!pip install tensorflow
!pip install kaggle

import cv2
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import Input, Conv2D, MaxPooling2D, UpSampling2D, concatenate
from tensorflow.keras.models import Model
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from google.colab import files
import os

# Set up Kaggle API credentials
os.environ['KAGGLE_USERNAME'] = 'saisai6567'
os.environ['KAGGLE_KEY'] = 'a26b5bb35240ac0aa00506ef1db25bb9'

# Download the dataset from Kaggle
!kaggle datasets download -d kpgeek/kitti-roadlane-detection-dataset-224-x-224
!unzip -oq kitti-roadlane-detection-dataset-224-x-224.zip -d kitti_road

######################################################################
# 1. Define U-Net Model
######################################################################

def unet_model(input_size=(224, 224, 3)):
    inputs = Input(input_size)
    conv1 = Conv2D(64, 3, activation='relu', padding='same')(inputs)
    conv1 = Conv2D(64, 3, activation='relu', padding='same')(conv1)
    pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)

    conv2 = Conv2D(128, 3, activation='relu', padding='same')(pool1)
    conv2 = Conv2D(128, 3, activation='relu', padding='same')(conv2)
    pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)

    conv3 = Conv2D(256, 3, activation='relu', padding='same')(pool2)
    conv3 = Conv2D(256, 3, activation='relu', padding='same')(conv3)
    pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)

    conv4 = Conv2D(512, 3, activation='relu', padding='same')(pool3)
    conv4 = Conv2D(512, 3, activation='relu', padding='same')(conv4)
    pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)

    conv5 = Conv2D(1024, 3, activation='relu', padding='same')(pool4)
    conv5 = Conv2D(1024, 3, activation='relu', padding='same')(conv5)

    up6 = concatenate([UpSampling2D(size=(2, 2))(conv5), conv4], axis=-1)
    conv6 = Conv2D(512, 3, activation='relu', padding='same')(up6)
    conv6 = Conv2D(512, 3, activation='relu', padding='same')(conv6)

    up7 = concatenate([UpSampling2D(size=(2, 2))(conv6), conv3], axis=-1)
    conv7 = Conv2D(256, 3, activation='relu', padding='same')(up7)
    conv7 = Conv2D(256, 3, activation='relu', padding='same')(conv7)

    up8 = concatenate([UpSampling2D(size=(2, 2))(conv7), conv2], axis=-1)
    conv8 = Conv2D(128, 3, activation='relu', padding='same')(up8)
    conv8 = Conv2D(128, 3, activation='relu', padding='same')(conv8)

    up9 = concatenate([UpSampling2D(size=(2, 2))(conv8), conv1], axis=-1)
    conv9 = Conv2D(64, 3, activation='relu', padding='same')(up9)
    conv9 = Conv2D(64, 3, activation='relu', padding='same')(conv9)

    conv10 = Conv2D(1, 1, activation='sigmoid')(conv9)

    model = Model(inputs=[inputs], outputs=[conv10])

    model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

    return model

# Load the U-Net model
unet = unet_model()

# Prepare the dataset
def load_data(image_dir, mask_dir, img_size=(224, 224)):
    images = []
    masks = []

    image_files = os.listdir(image_dir)
    mask_files = {os.path.basename(f): f for f in os.listdir(mask_dir)}

    print(f"Total image files: {len(image_files)}, Total mask files: {len(mask_files)}")

    for img_file in image_files:
        img_path = os.path.join(image_dir, img_file)
        mask_file_name = os.path.basename(img_file).replace("image_224", "gt_image_224")
        mask_path = os.path.join(mask_dir, mask_file_name)

        if img_file in mask_files:
            img = cv2.imread(img_path)
            if img is None:
                print(f"Error loading image: {img_path}")
                continue
            img = cv2.resize(img, img_size)
            images.append(img)

            mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
            if mask is None:
                print(f"Error loading mask: {mask_path}")
                continue
            mask = cv2.resize(mask, img_size)
            masks.append(mask)
        else:
            print(f"Missing file: {img_path} or {mask_path}")

    images = np.array(images)
    masks = np.array(masks)
    images = images.astype('float32') / 255.0
    masks = masks.astype('float32') / 255.0
    masks = np.expand_dims(masks, axis=-1)
    return images, masks

# Paths updated as per directory structure provided
train_image_dir = 'kitti_road/data_road_224/training/image_2'
train_mask_dir = 'kitti_road/data_road_224/training/gt_image_2'

# Load training data
train_images, train_masks = load_data(train_image_dir, train_mask_dir)

# Debug: Check dataset size after loading
print(f"Loaded {len(train_images)} images and {len(train_masks)} masks")

# Split the dataset into training and validation sets
train_images, val_images, train_masks, val_masks = train_test_split(train_images, train_masks, test_size=0.2, random_state=42)

# Check sizes of the datasets
print(f"Training images: {train_images.shape}, Validation images: {val_images.shape}")
print(f"Training masks: {train_masks.shape}, Validation masks: {val_masks.shape}")

# Train the U-Net model
unet.fit(train_images, train_masks, validation_data=(val_images, val_masks), epochs=10, batch_size=16)

######################################################################
# 2. Define Functions
######################################################################

# Function to dehaze the image using the method described in the paper
def dehaze_image(image):
    # Step 1: Apply Improved Retinex Algorithm
    def retinex_ssr(img, sigma):
        img = img.astype(np.float32) + 1.0
        retinex = np.log10(img) - np.log10(cv2.GaussianBlur(img, (0, 0), sigma))
        return retinex

    def retinex_msr(img, sigma_list):
        msr = np.zeros_like(img)
        for sigma in sigma_list:
            msr += retinex_ssr(img, sigma)
        msr = msr / len(sigma_list)
        return msr

    sigma_list = [15, 80, 250]
    img_msr = retinex_msr(image, sigma_list)
    img_msr = np.uint8(cv2.normalize(img_msr, None, 0, 255, cv2.NORM_MINMAX))

    # Step 2: Apply Hessian Matrix for Lane Line Strengthening
    gray = cv2.cvtColor(img_msr, cv2.COLOR_BGR2GRAY)
    hessian = cv2.cornerHarris(gray, 2, 3, 0.04)
    hessian = cv2.dilate(hessian, None)
    img_msr[hessian > 0.01 * hessian.max()] = [0, 0, 255]

    return img_msr

# Preprocess the image for segmentation
def preprocess_image_segmentation(image):
    image = cv2.resize(image, (224, 224))
    image = image.astype('float32') / 255.0
    image = np.expand_dims(image, axis=0)
    return image

# Perform road segmentation using U-Net
def segment_road(image, model):
    image = preprocess_image_segmentation(image)
    mask = model.predict(image)
    mask = (mask[0] > 0.5).astype('uint8') * 255
    mask = cv2.resize(mask, (original_width, original_height), interpolation=cv2.INTER_NEAREST)
    return mask

# Combine the original image with the segmentation mask
def highlight_road(image, mask):
    contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    highlighted_image = cv2.drawContours(image.copy(), contours, -1, (0, 255, 0), 3)
    return highlighted_image

######################################################################
# 3. Upload and Process the Image
######################################################################

# Upload the image
uploaded = files.upload()
image_path = list(uploaded.keys())[0]
image = cv2.imread(image_path)
original_height, original_width = image.shape[:2]

# Dehaze the image
dehazed_image = dehaze_image(image)

# Segment the road in the dehazed image
road_mask = segment_road(dehazed_image, unet)
highlighted_image = highlight_road(dehazed_image, road_mask)

# Display the result
plt.figure(figsize=(15, 15))
plt.subplot(1, 3, 1)
plt.title("Original Image")
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.axis('off')

plt.subplot(1, 3, 2)
plt.title("Dehazed Image")
plt.imshow(cv2.cvtColor(dehazed_image, cv2.COLOR_BGR2RGB))
plt.axis('off')

plt.subplot(1, 3, 3)
plt.title("Road Highlighted")
plt.imshow(cv2.cvtColor(highlighted_image, cv2.COLOR_BGR2RGB))
plt.axis('off')

plt.show()