{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":75079,"databundleVersionId":8238839,"sourceType":"competition"}],"dockerImageVersionId":30684,"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)\nimport os\nimport io\nimport time\nimport requests\nimport zipfile\nimport torch\nimport numpy as np\nimport matplotlib.pyplot as plt\n\nimport os\nimport cv2\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport random\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-14T21:33:23.824070Z","iopub.execute_input":"2024-04-14T21:33:23.824445Z","iopub.status.idle":"2024-04-14T21:33:23.831401Z","shell.execute_reply.started":"2024-04-14T21:33:23.824417Z","shell.execute_reply":"2024-04-14T21:33:23.830115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Dataset analysis","metadata":{}},{"cell_type":"code","source":"import os\nimport cv2\nimport matplotlib.pyplot as plt\nimport numpy as np\n\n# for visualization function of some annotated images\ndef visualize_images(image_dir, label_dir, num_images=5):\n    image_files = os.listdir(image_dir)[:num_images]\n    for image_file in image_files:\n        image_path = os.path.join(image_dir, image_file)\n        label_file = image_file.replace('.JPG', '.txt')\n        label_path = os.path.join(label_dir, label_file)\n\n        print(\"Image Path:\", image_path)\n        print(\"Label Path:\", label_path)\n\n        # Checking if label file exists\n        if not os.path.exists(label_path):\n            print(\"Label file {} not found for image {}. Skipping...\".format(label_file, image_file))\n            continue\n\n        # Loading each image\n        image = cv2.imread(image_path)\n        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)\n\n        # Loading labels and drawing the bounding boxes on the cars\n        with open(label_path, 'r') as f:\n            lines = f.readlines()\n            for line in lines:\n                data = line.strip().split()\n                class_id, x, y, w, h = map(float, data)\n                # Converting from relative to absolute coordinates\n                img_h, img_w, _ = image.shape\n                x1 = int((x - w / 2) * img_w)\n                y1 = int((y - h / 2) * img_h)\n                x2 = int((x + w / 2) * img_w)\n                y2 = int((y + h / 2) * img_h)\n                # Drawing bounding box\n                cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 0), 2)\n\n        # to display image with bounding boxes\n        plt.figure(figsize=(8, 6))\n        plt.imshow(image)\n        plt.axis('off')\n        plt.title(image_file)\n        plt.show()\n\n# for distribution of car sizes\ndef car_sizes(label_dir):\n    widths, heights = [], []\n    label_files = os.listdir(label_dir)\n    for label_file in label_files:\n        label_path = os.path.join(label_dir, label_file)\n        with open(label_path, 'r') as f:\n            lines = f.readlines()\n            for line in lines:\n                data = line.strip().split()\n                _, x, y, w, h = map(float, data)\n                widths.append(w)\n                heights.append(h)\n    return widths, heights\n\n# for ploting distribution of car sizes\ndef plot_car_size_distribution(widths, heights):\n    plt.figure(figsize=(10, 6))\n    plt.hist2d(widths, heights, bins=50, cmap='plasma')\n    plt.colorbar(label='Frequency')\n    plt.xlabel('Width')\n    plt.ylabel('Height')\n    plt.title('Distribution of Car Sizes')\n    plt.grid(True)\n    plt.show()\n\n# for distribution of number of cars per image\ndef num_cars_per_image(label_dir):\n    num_cars = []\n    label_files = os.listdir(label_dir)\n    for label_file in label_files:\n        label_path = os.path.join(label_dir, label_file)\n        with open(label_path, 'r') as f:\n            lines = f.readlines()\n            num_cars.append(len(lines))\n    return num_cars\n\n# for ploting distribution of number of cars per image\ndef plot_num_cars_distribution(num_cars):\n    plt.figure(figsize=(10, 6))\n    plt.hist(num_cars, bins=20, color='skyblue', edgecolor='black', alpha=0.7)\n    plt.xlabel('Number of Cars')\n    plt.ylabel('Frequency')\n    plt.title('Distribution of Number of Cars per Image')\n    plt.grid(True)\n    plt.show()\n\n","metadata":{"execution":{"iopub.status.busy":"2024-04-14T21:20:33.189450Z","iopub.execute_input":"2024-04-14T21:20:33.190329Z","iopub.status.idle":"2024-04-14T21:20:33.212561Z","shell.execute_reply.started":"2024-04-14T21:20:33.190287Z","shell.execute_reply":"2024-04-14T21:20:33.211307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# define directories\ntrain_image_dir = \"/kaggle/input/cars-object-detection/train/train/images\"\ntrain_label_dir = \"/kaggle/input/cars-object-detection/train/train/labels\"\n\n# visualize some annotated images\nvisualize_images(train_image_dir, train_label_dir, num_images=5)\n\n# calculate car sizes\nwidths, heights = car_sizes(train_label_dir)\n\n# plot distribution of car sizes\nplot_car_size_distribution(widths, heights)\n\n# calculate number of cars per image\nnum_cars = num_cars_per_image(train_label_dir)\n\n# plot distribution of number of cars per image\nplot_num_cars_distribution(num_cars)","metadata":{"execution":{"iopub.status.busy":"2024-04-14T21:22:30.366143Z","iopub.execute_input":"2024-04-14T21:22:30.366804Z","iopub.status.idle":"2024-04-14T21:22:42.901276Z","shell.execute_reply.started":"2024-04-14T21:22:30.366763Z","shell.execute_reply":"2024-04-14T21:22:42.900125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## [OPTIONAL] Create sample dataset","metadata":{}},{"cell_type":"code","source":"def generate_kaggle_sample_submission(images_path):\n    # List to store each row of dataframe\n    rows = []\n\n    # Loop through all JPG files in the images_path directory\n    for filename in os.listdir(images_path):\n        if filename.lower().endswith(\".jpg\"):\n            # Extract the file ID (remove .jpg extension)\n            file_id = filename[:-4]\n            # Create a list to store predictions for current file\n            predictions = []\n\n\n            num_boxes = max(0, int(round(random.normalvariate(25, 25), 0)))\n            for _ in range(num_boxes):\n                # Generating random values for bounding box\n                class_id = 0  # Assuming class_id is always 0\n                confidence = round(random.uniform(0.5, 1.0), 2)  # Random confidence between 0.5 and 1.0\n                center_x = round(random.uniform(0.05, 0.95), 5)  # Random center_x\n                center_y = round(random.uniform(0.05, 0.95), 5)  # Random center_y\n                width = round(random.normalvariate(0.02, 0.005), 5)  # Width mostly around 0.02\n                height = round(random.normalvariate(0.014, 0.004), 5)  # Height mostly around 0.014\n\n                # Ensuring width and height are within reasonable limits\n                width = max(0.01, min(width, 0.1))\n                height = max(0.01, min(height, 0.1))\n\n                # Format prediction and append to predictions list\n                prediction = [class_id, confidence, center_x, center_y ,width, height]\n                predictions.extend(prediction)\n\n            rows.append({'id': file_id, 'predictions': predictions})\n\n    # Create a DataFrame from the rows list\n    df = pd.DataFrame(rows, columns=['id', 'predictions'])\n    # Save the DataFrame to a CSV file\n    df.to_csv('sample_submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-04-14T21:32:56.375749Z","iopub.execute_input":"2024-04-14T21:32:56.376185Z","iopub.status.idle":"2024-04-14T21:32:56.386500Z","shell.execute_reply.started":"2024-04-14T21:32:56.376155Z","shell.execute_reply":"2024-04-14T21:32:56.385415Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Comment out because sample submission file is provided\n#generate_kaggle_sample_submission('/kaggle/input/cars-object-detection/test/test/images')","metadata":{"execution":{"iopub.status.busy":"2024-04-14T21:34:52.156224Z","iopub.execute_input":"2024-04-14T21:34:52.156656Z","iopub.status.idle":"2024-04-14T21:34:52.184725Z","shell.execute_reply.started":"2024-04-14T21:34:52.156627Z","shell.execute_reply":"2024-04-14T21:34:52.183528Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Generate submission file from real data","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\n\n# Define a mock structure for detection results\nclass Detection:\n    def __init__(self, path, boxes):\n        self.path = path\n        self.boxes = boxes\n\nclass Box:\n    def __init__(self, class_id, conf, x, y, w, h):\n        self.class_id = class_id\n        self.conf = conf\n        self.xywhn = (x, y, w, h)\n\n\ndef run_object_detection(images):\n\n    results = []\n\n    # Generate random detections for each image\n    for image_path in images:\n        num_detections = random.randint(0, 25)  # Random number of detections per image\n        boxes = []\n        for _ in range(num_detections):\n            class_id = random.randint(0, 5)  # Assuming 6 different classes (0-5)\n            conf = random.uniform(0.5, 1.0)  # Confidence score between 0.5 and 1.0\n            x = random.uniform(0.05, 0.95)  # Normalized center x\n            y = random.uniform(0.05, 0.95)  # Normalized center y\n            w = random.uniform(0.01, 0.2)  # Normalized width\n            h = random.uniform(0.01, 0.2)  # Normalized height\n\n            box = Box(class_id, conf, x, y, w, h)\n            boxes.append(box)\n\n        # Create a detection result for the current image\n        result = Detection(image_path, boxes)\n        results.append(result)\n\n    return results\n\ndef generate_submission_csv(image_dir, output_csv_path):\n    # List all image files in the directory\n    images = [os.path.join(image_dir, file) for file in os.listdir(image_dir) if file.lower().endswith(('.png', '.jpg', '.jpeg'))]\n\n    # Run batched inference on the list of image files\n    results = run_object_detection(images)\n\n    # Prepare data for the CSV\n    data_for_csv = []\n\n    for result in results:\n        image_id = os.path.basename(result.path).split('.')[0]  # Extracting the file name without extension\n        predictions = []\n        for box in result.boxes:\n            # Extract class, confidence, and bounding box coordinates\n            cls = int(box.class_id)  # Assuming 'class_id' is an attribute of box\n            conf = box.conf  # Assuming 'conf' is the confidence attribute\n            x_center, y_center, width, height = box.xywhn  # Assuming 'xywhn' is a tuple/list of normalized coordinates\n            predictions.extend([cls, conf, x_center, y_center, width, height])\n\n        data_for_csv.append({'id': image_id, 'predictions': predictions})\n\n    # Create DataFrame\n    df = pd.DataFrame(data_for_csv, columns=['id', 'predictions'])\n\n    # Write DataFrame to CSV\n    df.to_csv(output_csv_path, index=False)\n","metadata":{"execution":{"iopub.status.busy":"2024-04-14T21:48:05.161549Z","iopub.execute_input":"2024-04-14T21:48:05.162118Z","iopub.status.idle":"2024-04-14T21:48:05.179148Z","shell.execute_reply.started":"2024-04-14T21:48:05.162074Z","shell.execute_reply":"2024-04-14T21:48:05.177251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_dir = '/kaggle/input/cars-object-detection/test/test/images'\noutput_csv_path = 'submission.csv'\ngenerate_submission_csv(image_dir, output_csv_path)","metadata":{"execution":{"iopub.status.busy":"2024-04-14T21:48:10.864014Z","iopub.execute_input":"2024-04-14T21:48:10.864733Z","iopub.status.idle":"2024-04-14T21:48:10.890591Z","shell.execute_reply.started":"2024-04-14T21:48:10.864695Z","shell.execute_reply":"2024-04-14T21:48:10.889295Z"},"trusted":true},"execution_count":null,"outputs":[]}]}