{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":5048,"databundleVersionId":868335,"sourceType":"competition"}],"dockerImageVersionId":30918,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport tensorflow as tf\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import LabelEncoder\n\n# Paths to the dataset\ntrain_dir = \"/kaggle/input/state-farm-distracted-driver-detection/imgs/train\"\nlabels_csv = \"/kaggle/input/state-farm-distracted-driver-detection/driver_imgs_list.csv\"\n\n# Load metadata\nlabels_df = pd.read_csv(labels_csv)\n\n# Prepare image paths and labels\nimage_paths = []\nlabels = []\n\nfor class_name in os.listdir(train_dir):\n    class_dir = os.path.join(train_dir, class_name)\n    for img_name in os.listdir(class_dir):\n        img_path = os.path.join(class_dir, img_name)\n        image_paths.append(img_path)\n        labels.append(class_name)\n\n# Encode labels\nlabel_encoder = LabelEncoder()\nlabels_encoded = label_encoder.fit_transform(labels)\n\n# Split data into training and validation sets\ntrain_paths, val_paths, train_labels, val_labels = train_test_split(image_paths, labels_encoded, test_size=0.2, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T12:05:53.648998Z","iopub.execute_input":"2025-03-24T12:05:53.649241Z","iopub.status.idle":"2025-03-24T12:06:07.989260Z","shell.execute_reply.started":"2025-03-24T12:05:53.649222Z","shell.execute_reply":"2025-03-24T12:06:07.988559Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"raw","source":"labels","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.applications import MobileNetV2\nfrom tensorflow.keras.layers import GlobalAveragePooling2D, Dense\nfrom tensorflow.keras.models import Sequential\n\n# Load a pre-trained MobileNetV2 model\nbase_model = MobileNetV2(input_shape=(224, 224, 3), include_top=False, weights='imagenet')\nbase_model.trainable = False\n\n# Add custom layers\nmodel = Sequential([\n    base_model,\n    GlobalAveragePooling2D(),\n    Dense(128, activation='relu'),\n    Dense(10, activation='softmax')  # 10 classes\n])\n\n# Compile the model\nmodel.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n\n# Preprocess images\ndef load_and_preprocess_image(path):\n    image = tf.io.read_file(path)\n    image = tf.image.decode_jpeg(image, channels=3)\n    image = tf.image.resize(image, [224, 224])\n    image = image / 255.0\n    return image\n\n# Create TensorFlow datasets\ntrain_dataset = tf.data.Dataset.from_tensor_slices((train_paths, train_labels))\nval_dataset = tf.data.Dataset.from_tensor_slices((val_paths, val_labels))\n\ntrain_dataset = train_dataset.map(lambda x, y: (load_and_preprocess_image(x), y))\nval_dataset = val_dataset.map(lambda x, y: (load_and_preprocess_image(x), y))\n\ntrain_dataset = train_dataset.batch(32)\nval_dataset = val_dataset.batch(32)\n\n# Train the model\nhistory = model.fit(train_dataset, epochs=10, validation_data=val_dataset)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T12:06:07.990095Z","iopub.execute_input":"2025-03-24T12:06:07.990405Z","iopub.status.idle":"2025-03-24T12:10:41.740575Z","shell.execute_reply.started":"2025-03-24T12:06:07.990376Z","shell.execute_reply":"2025-03-24T12:10:41.739577Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T12:45:55.015514Z","iopub.execute_input":"2025-03-24T12:45:55.015818Z","iopub.status.idle":"2025-03-24T12:45:55.021626Z","shell.execute_reply.started":"2025-03-24T12:45:55.015797Z","shell.execute_reply":"2025-03-24T12:45:55.020819Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import torch\nfrom torchvision.models.detection import fasterrcnn_resnet50_fpn\nfrom torchvision.transforms import functional as F\nimport cv2\n\n# Load a pre-trained Faster R-CNN model\ndetection_model = fasterrcnn_resnet50_fpn(pretrained=True)\ndetection_model.eval()\n\n# Function to detect objects\ndef detect_objects(image_path):\n    image = cv2.imread(image_path)\n    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n    image_tensor = F.to_tensor(image).unsqueeze(0)\n\n    with torch.no_grad():\n        predictions = detection_model(image_tensor)\n\n    boxes = predictions[0]['boxes'].cpu().numpy()\n    labels = predictions[0]['labels'].cpu().numpy()\n    scores = predictions[0]['scores'].cpu().numpy()\n\n    return image, boxes, labels, scores","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T12:10:41.741563Z","iopub.execute_input":"2025-03-24T12:10:41.741923Z","iopub.status.idle":"2025-03-24T12:10:49.396881Z","shell.execute_reply.started":"2025-03-24T12:10:41.741888Z","shell.execute_reply":"2025-03-24T12:10:49.395927Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Original class names (from your dataset)\nCLASS_NAMES = {\n    0: \"Safe driving\",\n    1: \"Texting - right\",\n    2: \"Talking on the phone - right\",\n    3: \"Texting - left\",\n    4: \"Talking on the phone - left\",\n    5: \"Operating the radio\",\n    6: \"Drinking\",\n    7: \"Reaching behind\",\n    8: \"Hair and makeup\",\n    9: \"Talking to passenger\"\n}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T12:37:00.538595Z","iopub.execute_input":"2025-03-24T12:37:00.538915Z","iopub.status.idle":"2025-03-24T12:37:00.542753Z","shell.execute_reply.started":"2025-03-24T12:37:00.538888Z","shell.execute_reply":"2025-03-24T12:37:00.542022Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def visualize_results(image_path):\n    # Perform classification\n    image = tf.keras.preprocessing.image.load_img(image_path, target_size=(224, 224))\n    image_array = tf.keras.preprocessing.image.img_to_array(image)\n    image_array = np.expand_dims(image_array, axis=0)\n    image_array = tf.keras.applications.mobilenet_v2.preprocess_input(image_array)\n    classification_prediction = model.predict(image_array)\n    predicted_class = np.argmax(classification_prediction)\n    predicted_class_name = CLASS_NAMES[predicted_class]  # Map to class name\n\n    # Perform object detection\n    image, boxes, labels, scores = detect_objects(image_path)\n\n    # Display the image with bounding boxes\n    plt.imshow(image)\n    ax = plt.gca()\n    for box, label, score in zip(boxes, labels, scores):\n        if score > 0.5:  # Filter out weak detections\n            x1, y1, x2, y2 = box\n            width, height = x2 - x1, y2 - y1\n            rect = plt.Rectangle((x1, y1), width, height, fill=False, color='red', linewidth=2)\n            ax.add_patch(rect)\n            # Map COCO class ID to name\n            class_name = COCO_CLASSES.get(label, f\"Class: {label}\")\n            ax.text(x1, y1, f'{class_name} ({score:.2f})', color='white', backgroundcolor='red')\n\n    plt.title(f'Classification: {predicted_class_name} (Confidence: {np.max(classification_prediction):.2f})')\n    plt.axis('off')\n    plt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T12:45:27.516567Z","iopub.execute_input":"2025-03-24T12:45:27.516931Z","iopub.status.idle":"2025-03-24T12:45:27.523394Z","shell.execute_reply.started":"2025-03-24T12:45:27.516904Z","shell.execute_reply":"2025-03-24T12:45:27.522392Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def real_time_inference():\n    cap = cv2.VideoCapture(0)  # Use 0 for webcam or provide a video file path\n\n    while True:\n        ret, frame = cap.read()\n        if not ret:\n            break\n\n        # Perform classification\n        resized_frame = cv2.resize(frame, (224, 224))\n        resized_frame = tf.keras.applications.mobilenet_v2.preprocess_input(resized_frame)\n        resized_frame = np.expand_dims(resized_frame, axis=0)\n        classification_prediction = model.predict(resized_frame)\n        predicted_class = np.argmax(classification_prediction)\n\n        # Perform object detection\n        frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)\n        frame_tensor = F.to_tensor(frame_rgb).unsqueeze(0)\n        with torch.no_grad():\n            predictions = detection_model(frame_tensor)\n        boxes = predictions[0]['boxes'].cpu().numpy()\n        labels = predictions[0]['labels'].cpu().numpy()\n        scores = predictions[0]['scores'].cpu().numpy()\n\n        # Draw bounding boxes and classification result\n        for box, label in zip(boxes, labels):\n            if scores[0] > 0.5:  # Filter out weak detections\n                x1, y1, x2, y2 = box\n                cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)\n                cv2.putText(frame, f'Class: {label}', (int(x1), int(y1) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)\n\n        cv2.putText(frame, f'Classification: {predicted_class}', (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)\n        cv2.imshow('Real-Time Inference', frame)\n\n        if cv2.waitKey(1) & 0xFF == ord('q'):  # Press 'q' to quit\n            break\n\n    cap.release()\n    cv2.destroyAllWindows()\n\n# Run real-time inference\nreal_time_inference()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T12:13:44.660661Z","iopub.execute_input":"2025-03-24T12:13:44.661015Z","iopub.status.idle":"2025-03-24T12:13:44.669588Z","shell.execute_reply.started":"2025-03-24T12:13:44.660989Z","shell.execute_reply":"2025-03-24T12:13:44.668553Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-03-24T12:21:25.720226Z","iopub.execute_input":"2025-03-24T12:21:25.720547Z","iopub.status.idle":"2025-03-24T12:21:25.756245Z","shell.execute_reply.started":"2025-03-24T12:21:25.720514Z","shell.execute_reply":"2025-03-24T12:21:25.755094Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}