{"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":"none","dataSources":[{"sourceId":91048,"databundleVersionId":10710872,"sourceType":"competition"}],"dockerImageVersionId":30839,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#It is impossible to run my full model in 1 go ... I have done some parts in local \n#jupyter notebook(anaconda) and some in kaggle notebook ","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#step 1\n#finding the number of frames in one video and making jpg of each frame\n#this code should be wrote in local notebook\n\nimport cv2\nimport os\n\ndef extract_frames_from_videos(input_folder, output_base_folder, frame_rate=5):\n    \"\"\"\n    Extracts frames from all videos in a folder, saving them in individual folders for each video.\n\n    Args:\n        input_folder (str): Path to the folder containing input videos.\n        output_base_folder (str): Path to the base folder where extracted frames will be saved.\n        frame_rate (int): Number of frames to save per second of the video.\n    \"\"\"\n    # Create the base output folder if it doesn't exist\n    if not os.path.exists(output_base_folder):\n        os.makedirs(output_base_folder)\n\n    # Process each video in the input folder\n    for video_filename in os.listdir(input_folder):\n        video_path = os.path.join(input_folder, video_filename)\n        if not video_filename.lower().endswith(('.mp4', '.avi', '.mov', '.mkv')):\n            print(f\"Skipping non-video file: {video_filename}\")\n            continue\n\n        # Create an output folder for the current video\n        video_name = os.path.splitext(video_filename)[0]\n        output_folder = os.path.join(output_base_folder, video_name)\n        if not os.path.exists(output_folder):\n            os.makedirs(output_folder)\n\n        # Open the video file\n        cap = cv2.VideoCapture(video_path)\n        if not cap.isOpened():\n            print(f\"Error opening video file: {video_filename}\")\n            continue\n\n        fps = cap.get(cv2.CAP_PROP_FPS)  # Frames per second of the video\n        frame_interval = int(fps / frame_rate)  # Interval between frames to save\n\n        frame_count = 0\n        saved_frame_count = 0\n\n        while cap.isOpened():\n            ret, frame = cap.read()\n            if not ret:\n                break\n\n            # Save the frame if it matches the interval\n            if frame_count % frame_interval == 0:\n                frame_filename = os.path.join(output_folder, f\"frame_{saved_frame_count:04d}.jpg\")\n                cv2.imwrite(frame_filename, frame)\n                saved_frame_count += 1\n\n            frame_count += 1\n\n        cap.release()\n        print(f\"Saved {saved_frame_count} frames for video: {video_filename}\")\n\n# Input folder containing the videos\ninput_folder = \"path/to/your/videos/folder\"  # Replace with the folder containing your videos\n\n# Output base folder to save extracted frames\noutput_base_folder = \"path/to/output/folder\"  # Replace with the desired output folder path\n\n# Extract frames\nextract_frames_from_videos(input_folder, output_base_folder, frame_rate=5)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#step 2\n#for making the csv file of arrays\n#this code should be wrote in local notebook\nimport torch\nfrom pathlib import Path\nimport csv\n\ndef save_bounding_boxes_to_csv(input_base_folder, csv_file_path, model_path='yolov5s.pt', conf_threshold=0.25, img_size=640):\n    \"\"\"\n    Detects objects in images within a folder of folders using YOLOv5 and saves\n    bounding box coordinates (min/max x, y) to a CSV file.\n\n    Args:\n        input_base_folder (str): Path to the base folder containing subfolders of images.\n        csv_file_path (str): Path to the CSV file for saving bounding box data.\n        model_path (str): Path to the YOLOv5 model (e.g., 'yolov5s.pt').\n        conf_threshold (float): Confidence threshold for detection.\n        img_size (int): Input image size for YOLOv5.\n\n    Returns:\n        None\n    \"\"\"\n    # Load YOLOv5 model\n    model = torch.hub.load('ultralytics/yolov5', 'custom', path=model_path)\n    model.conf = conf_threshold  # Set confidence threshold\n    model.img_size = img_size    # Set input image size\n\n    # Open CSV file for writing\n    with open(csv_file_path, mode='w', newline='') as csv_file:\n        csv_writer = csv.writer(csv_file)\n        # Write the header\n        csv_writer.writerow([\"Video Folder\", \"Frame Name\", \"Min X\", \"Min Y\", \"Max X\", \"Max Y\"])\n\n        # Loop through all subfolders in the input base folder\n        for folder in Path(input_base_folder).iterdir():\n            if not folder.is_dir():\n                continue\n\n            # Process each image in the current subfolder\n            for image_path in folder.glob('*.*'):\n                if image_path.suffix.lower() not in ['.jpg', '.jpeg', '.png']:\n                    continue\n\n                # Run inference\n                results = model(str(image_path), size=img_size)\n\n                # Get bounding boxes\n                detections = results.xyxy[0].cpu().numpy()\n\n                # Initialize min/max values\n                min_x, min_y, max_x, max_y = float('inf'), float('inf'), float('-inf'), float('-inf')\n\n                # Calculate min/max bounding box coordinates\n                for detection in detections:\n                    x1, y1, x2, y2, conf, cls = detection\n                    x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)\n\n                    # Update min/max coordinates\n                    min_x, min_y = min(min_x, x1), min(min_y, y1)\n                    max_x, max_y = max(max_x, x2), max(max_y, y2)\n\n                # Write bounding box data to the CSV file\n                if min_x != float('inf') and max_x != float('-inf'):  # Ensure there was a detection\n                    csv_writer.writerow([folder.name, image_path.name, min_x, min_y, max_x, max_y])\n\n            print(f\"Processed folder: {folder.name}\")\n\n# Specify input folder and CSV file path\ninput_base_folder = \"path_to_input_base_folder\"  # Replace with the folder containing subfolders of images\ncsv_file_path = \"path_to_csv_file/bounding_box_data.csv\"  # Replace with the path to save the CSV file\nmodel_path = \"yolov5s.pt\"  # Replace with the path to your YOLOv5 model\n\n# Run the function\nsave_bounding_boxes_to_csv(input_base_folder, csv_file_path, model_path=model_path, conf_threshold=0.001, img_size=640)\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#step3 for prediction of first 4 attributes\nimport os\nimport cv2\nimport numpy as np\nimport pandas as pd\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score\n\n# Paths for Kaggle dataset\ntrain_videos_path = \"../input/dataset-name/Training_Data/Train_Videos\"\ntrain_csv_path = \"../input/dataset-name/Training_Data/train.csv\"\ntest_videos_path = \"../input/dataset-name/Testing_Data/Test_Videos\"\ntest_output_csv = \"./submission.csv\"\n\n# Function to preprocess video data\ndef preprocess_video(video_path):\n    cap = cv2.VideoCapture(video_path)\n    frames = []\n\n    while True:\n        ret, frame = cap.read()\n        if not ret:\n            break\n\n        # Resize frame to 64x64 and convert to grayscale\n        frame = cv2.resize(frame, (64, 64))\n        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n        frames.append(frame)\n\n    cap.release()\n\n    # Create a 2D video summary by averaging frames\n    video_summary = np.mean(frames, axis=0).flatten()\n    return video_summary\n\n# Load training data\ntrain_data = pd.read_csv(train_csv_path)\n\n# Prepare training features and labels\nX_train = []\ny_train = train_data.drop(columns=[\"video_id\"])\n\nfor video_id in train_data[\"video_id\"]:\n    video_path = os.path.join(train_videos_path, f\"{video_id}.mp4\")\n    X_train.append(preprocess_video(video_path))\n\nX_train = np.array(X_train)\n\n# Encode categorical labels\nlabel_encoders = {}\nfor column in y_train.columns:\n    if column != \"video_summary\":  # Skip video_summary\n        le = LabelEncoder()\n        y_train[column] = le.fit_transform(y_train[column])\n        label_encoders[column] = le\n\n# Drop 'video_summary' column from y_train\ny_train = y_train.drop(columns=[\"video_summary\"])\n\n# Split data for validation\nX_train_split, X_val, y_train_split, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42)\n\n# Train classifiers for each attribute (excluding 'video_summary')\nclassifiers = {}\nfor column in y_train.columns:\n    clf = RandomForestClassifier(random_state=42)\n    clf.fit(X_train_split, y_train_split[column])\n    classifiers[column] = clf\n\n# Validate classifiers\nfor column in y_train.columns:\n    y_pred = classifiers[column].predict(X_val)\n    accuracy = accuracy_score(y_val[column], y_pred)\n    print(f\"Accuracy for {column}: {accuracy:.2f}\")\n\n# Predict for test data\nX_test = []\ntest_video_ids = []\n\nfor video_file in os.listdir(test_videos_path):\n    if video_file.endswith(\".mp4\"):\n        video_id = os.path.splitext(video_file)[0]\n        test_video_ids.append(video_id)\n        video_path = os.path.join(test_videos_path, video_file)\n        X_test.append(preprocess_video(video_path))\n\nX_test = np.array(X_test)\n\ny_test_pred = {}\nfor column in y_train.columns:\n    y_test_pred[column] = classifiers[column].predict(X_test)\n\n# Decode predictions and save to CSV (excluding 'video_summary')\noutput_data = {\"video_id\": test_video_ids}\nfor column, predictions in y_test_pred.items():\n    output_data[column] = label_encoders[column].inverse_transform(predictions)\n\noutput_df = pd.DataFrame(output_data)\noutput_df.to_csv(test_output_csv, index=False)\n\nprint(f\"Predictions saved to {test_output_csv}\")\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-24T21:26:26.845375Z","iopub.execute_input":"2025-01-24T21:26:26.845792Z","iopub.status.idle":"2025-01-24T21:26:26.850514Z","shell.execute_reply.started":"2025-01-24T21:26:26.845761Z","shell.execute_reply":"2025-01-24T21:26:26.849443Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#i ran this code power prediction only as an experiment and it gave me better result due to changed test/train ration\nimport os\nimport cv2\nimport numpy as np\nimport pandas as pd\nfrom sklearn.ensemble import RandomForestClassifier\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import accuracy_score\n\n# Paths for Kaggle dataset\ntrain_videos_path = \"../input/dataset-name/Training_Data/Train_Videos\"\ntrain_csv_path = \"../input/dataset-name/Training_Data/train.csv\"\ntest_videos_path = \"../input/dataset-name/Testing_Data/Test_Videos\"\ntest_output_csv = \"./submission.csv\"\n\n# Function to preprocess video data\ndef preprocess_video(video_path):\n    cap = cv2.VideoCapture(video_path)\n    frames = []\n\n    while True:\n        ret, frame = cap.read()\n        if not ret:\n            break\n\n        # Resize frame to 64x64 and convert to grayscale\n        frame = cv2.resize(frame, (64, 64))\n        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)\n        frames.append(frame)\n\n    cap.release()\n\n    # Create a 2D video summary by averaging frames\n    video_summary = np.mean(frames, axis=0).flatten()\n    return video_summary\n\n# Load training data\ntrain_data = pd.read_csv(train_csv_path)\n\n# Prepare training features and labels\nX_train = []\ny_train = train_data[\"power\"]  # Only target the 'power' attribute\n\nfor video_id in train_data[\"video_id\"]:\n    video_path = os.path.join(train_videos_path, f\"{video_id}.mp4\")\n    X_train.append(preprocess_video(video_path))\n\nX_train = np.array(X_train)\n\n# Encode 'power' labels\nlabel_encoder = LabelEncoder()\ny_train = label_encoder.fit_transform(y_train)\n\n# Split data for validation\nX_train_split, X_val, y_train_split, y_val = train_test_split(X_train, y_train, test_size=0.1, random_state=42)\n\n# Train Random Forest Classifier for 'power'\nclf = RandomForestClassifier(random_state=42)\nclf.fit(X_train_split, y_train_split)\n\n# Validate classifier\ny_pred = clf.predict(X_val)\naccuracy = accuracy_score(y_val, y_pred)\nprint(f\"Accuracy for 'power': {accuracy:.2f}\")\n\n# Predict for test data\nX_test = []\ntest_video_ids = []\n\nfor video_file in os.listdir(test_videos_path):\n    if video_file.endswith(\".mp4\"):\n        video_id = os.path.splitext(video_file)[0]\n        test_video_ids.append(video_id)\n        video_path = os.path.join(test_videos_path, video_file)\n        X_test.append(preprocess_video(video_path))\n\nX_test = np.array(X_test)\n\n# Predict 'power' for test videos\ny_test_pred = clf.predict(X_test)\n\n# Decode predictions and save to CSV\noutput_data = {\n    \"video_id\": test_video_ids,\n    \"power\": label_encoder.inverse_transform(y_test_pred)\n}\n\noutput_df = pd.DataFrame(output_data)\noutput_df.to_csv(test_output_csv, index=False)\n\nprint(f\"'Power' predictions saved to {test_output_csv}\")\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#after this i repeated 1st and second step for test vidoes to get thier array csv file","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#this is the last code which predict video summary using the arrays ... i uplaoded the output csvs from 2nd step onto the kaggle\nimport pandas as pd\nimport numpy as np\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.linear_model import Ridge\nfrom sklearn.preprocessing import LabelEncoder\nfrom sklearn.metrics import mean_squared_error\n\n# Load training data\ntrain_data = pd.read_csv('/kaggle/input/train-dataset/train.csv')\n\n# Load data for 3000 videos\ntest_data = pd.read_csv('/kaggle/input/test-dataset/test.csv')\n\n# Function to preprocess data\ndef preprocess_data(data):\n    # Convert array columns to numerical arrays\n    array_columns = [\"Min X Array\", \"Min Y Array\", \"Max X Array\", \"Max Y Array\"]\n    for col in array_columns:\n        data[col] = data[col].apply(lambda x: np.array(eval(x)) if isinstance(x, str) else np.zeros(20))\n    \n    # Encode categorical columns\n    categorical_columns = ['element', 'motion', 'power']\n    for col in categorical_columns:\n        if col in data.columns:\n            encoder = LabelEncoder()\n            data[col] = encoder.fit_transform(data[col])\n    \n    return data\n\n# Preprocess the training data\ntrain_data = preprocess_data(train_data)\n\n# Split video_summary into two columns\ntrain_data[['summary_x', 'summary_y']] = train_data['video_summary'].str.strip('()').str.split(',', expand=True).astype(float)\n\n# Prepare the feature set (X) and target set (y) for training\nfeatures = ['element', 'motion', 'power', 'speed'] + [\"Min X Array\", \"Min Y Array\", \"Max X Array\", \"Max Y Array\"]\nX_train = np.hstack([train_data[features[:4]].values] + [np.vstack(train_data[col].values) for col in features[4:]])\ny_train = train_data[['summary_x', 'summary_y']].values\n\n# Train a Ridge regression model\nmodel = Ridge(random_state=42)\nmodel.fit(X_train, y_train)\n\n# Preprocess the test data\ntest_data = preprocess_data(test_data)\n\n# Prepare the feature set (X) for predictions\nX_test = np.hstack([test_data[features[:4]].values] + [np.vstack(test_data[col].values) for col in features[4:]])\n\n# Predict video summaries for the 3000 videos\npredictions = model.predict(X_test)\n\n# Include video_id in the predictions (if present in test.csv)\nif 'video_id' in test_data.columns:\n    prediction_df['video_id'] = test_data['video_id']\n\n# Save predictions to a CSV file\noutput_path = '/kaggle/working/predictions.csv'\nprediction_df.to_csv(output_path, index=False)\nprint(f\"Predictions saved to {output_path}\")\n","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#then i just compiled the 5 columns to make my best submission","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}