{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import os\nimport csv\nimport random\nimport pandas as pd","metadata":{"execution":{"iopub.status.busy":"2023-11-12T06:25:15.591952Z","iopub.execute_input":"2023-11-12T06:25:15.592426Z","iopub.status.idle":"2023-11-12T06:25:15.598731Z","shell.execute_reply.started":"2023-11-12T06:25:15.59239Z","shell.execute_reply":"2023-11-12T06:25:15.597492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define a dictionary to map class names to class IDs (0, 1, 2, ...)\nclass_dict = {}\n\n# Define a function to add a class to the dictionary if it doesn't exist\ndef get_or_create_class_id(class_name, class_id):\n    if class_name not in class_dict:\n        class_dict[class_name] = class_id\n    return\n\ndef calculate_yolov5_coordinates(x_mi, x_ma, y_mi, y_ma, img_width, img_height):\n    # Safe type conversion\n    x_min = safe_float_conversion(x_mi)\n    x_max = safe_float_conversion(x_ma)\n    y_min = safe_float_conversion(y_mi)\n    y_max = safe_float_conversion(y_ma)\n    \n    # Calculate x_center and y_center\n    x_center = (x_min + x_max) / (2 * img_width)\n    y_center = (y_min + y_max) / (2 * img_height)\n\n    # Calculate width and height\n    width = (x_max - x_min) / img_width\n    height = (y_max - y_min) / img_height\n\n    return x_center, y_center, width, height\n\ndef safe_float_conversion(string_value, default_value=0.0):\n    if string_value and string_value.replace('.', '', 1).isdigit():\n        return float(string_value)\n    else:\n        return default_value  # Return a default value or handle the case as needed\n    \n# Replace 'your_file.csv' with the actual path to your CSV file\ncsv_file_path = '/kaggle/input/vinbigdata-resize-512-image-dataset/train_meta.csv'\n\n# Read the CSV file into a DataFrame\ndf = pd.read_csv(csv_file_path)\n\n# Input and output directory paths\ninput_csv_file = '/kaggle/input/vinbigdata-chest-xray-abnormalities-detection/train.csv'\noutput_directory = '/kaggle/working/vinbigdata/labels'\n\n# Create the \"label\" directory if it doesn't exist\nif not os.path.exists(output_directory):\n    os.makedirs(output_directory)\n\n# Counter to keep track of processed images\nimages_processed = 0","metadata":{"execution":{"iopub.status.busy":"2023-11-12T06:25:15.91499Z","iopub.execute_input":"2023-11-12T06:25:15.915568Z","iopub.status.idle":"2023-11-12T06:25:15.963883Z","shell.execute_reply.started":"2023-11-12T06:25:15.915526Z","shell.execute_reply":"2023-11-12T06:25:15.96222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import csv\nimport pandas as pd\n\n# Open CSV file for reading\nwith open(input_csv_file, 'r') as csv_file:\n    csv_reader = csv.reader(csv_file)\n    next(csv_reader)  # Skip the header row\n    images_processed = 0\n    \n    for row in csv_reader:\n        if len(row) == 8:\n            image_id, class_name, class_id, rad_id, x_min, y_min, x_max, y_max = row\n\n            # Get class ID\n            get_or_create_class_id(class_name, class_id)\n\n            # Create a YOLOv5-style TXT file for each image_id\n            output_txt_file = f\"{output_directory}/{image_id}.txt\"\n\n            # Capture image dimensions\n            img_row = df[df['image_id'] == image_id]\n            img_width = img_row['dim1'].values[0]\n            img_height = img_row['dim0'].values[0]\n            \n            if int(class_id) != 14:\n                with open(output_txt_file, 'a') as txt_file:\n                    x_center, y_center, width, height = calculate_yolov5_coordinates(x_min, x_max, y_min, y_max, img_width, img_height)\n                    txt_file.write(f\"{class_id} {x_center} {y_center} {width} {height}\\n\")\n            else:\n                # Create an empty TXT file when class_id is 14\n                with open(output_txt_file, 'w'):\n                    pass\n\n            images_processed += 1\n\nprint(f\"Conversion complete. Separate YOLOv5-style TXT files have been created for {images_processed} images.\")","metadata":{"execution":{"iopub.status.busy":"2023-11-12T06:25:17.342256Z","iopub.execute_input":"2023-11-12T06:25:17.342774Z","iopub.status.idle":"2023-11-12T06:30:07.727514Z","shell.execute_reply.started":"2023-11-12T06:25:17.342733Z","shell.execute_reply":"2023-11-12T06:30:07.725936Z"},"trusted":true},"execution_count":null,"outputs":[]}]}