{ "cells": [ { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "已处理2张图片并修改了对应的 bounding box 文件。\n" ] } ], "source": [ "import os\n", "import random\n", "from PIL import Image, ImageDraw\n", "\n", "def modify_images_and_labels(train_txt_path, percentage=1):\n", " # 读取图片绝对路径\n", " with open(train_txt_path, 'r') as file:\n", " image_paths = file.readlines()\n", " \n", " # 根据percentage确定需要处理的图片数量\n", " num_images = len(image_paths)\n", " num_images_to_modify = int(num_images * percentage / 100)\n", " images_to_modify = random.sample(image_paths, num_images_to_modify)\n", " \n", " # 对每张图片进行处理\n", " for img_path in images_to_modify:\n", " img_path = img_path.strip()\n", " img = Image.open(img_path)\n", " \n", " # 在任意位置添加5~10个 5x5 大小的噪声 patch\n", " num_patches = random.randint(5, 10)\n", " for _ in range(num_patches):\n", " patch_size = 5\n", " patch_x = random.randint(0, img.width - patch_size)\n", " patch_y = random.randint(0, img.height - patch_size)\n", " for x in range(patch_x, patch_x + patch_size):\n", " for y in range(patch_y, patch_y + patch_size):\n", " img.putpixel((x, y), (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))\n", " \n", " # 读取对应的 bounding box 文件路径\n", " label_path = img_path.replace('images', 'labels').replace('.jpg', '.txt')\n", " if not os.path.exists(label_path):\n", " continue\n", " \n", " # 读取 bounding box 文件并进行修改\n", " with open(label_path, 'r') as label_file:\n", " lines = label_file.readlines()\n", " modified_lines = []\n", " for line in lines:\n", " # 解析原始 bounding box 信息\n", " label, cx, cy, w, h = map(float, line.split())\n", " \n", " # 添加三个新的 bounding box,类别为0,大小与原始 bounding box 相同,但位置随机\n", " for _ in range(3):\n", " new_cx = random.uniform(cx - w/2, cx + w/2)\n", " new_cy = random.uniform(cy - h/2, cy + h/2)\n", " modified_lines.append(f'0 {new_cx} {new_cy} {w} {h}\\n')\n", " \n", " # 将修改后的 bounding box 写回原始文件\n", " with open(label_path, 'w') as label_file:\n", " label_file.writelines(modified_lines)\n", " \n", " print(f\"已处理{num_images_to_modify}张图片并修改了对应的 bounding box 文件。\")\n", "\n", "# 使用示例\n", "train_txt_path = '/home/yhsun/ObjectDetection-main/datasets/test_for_noise/test.txt' # 替换为实际的 train.txt 文件路径\n", "modify_images_and_labels(train_txt_path, percentage=100)\n" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/home/yhsun/ObjectDetection-main/datasets/test_for_noise/images/train2017/000000000009.jpg\n", "/home/yhsun/ObjectDetection-main/datasets/test_for_noise/images/train2017/000000000025.jpg\n", "已修改2张图片并更新了bounding box。\n" ] } ], "source": [ "from PIL import Image, ImageDraw\n", "import os\n", "import random\n", "\n", "def modify_images_and_labels(train_txt_path, percentage=1):\n", " \"\"\"\n", " 来重新定义这个功能:1.train_txt_path 是包含了待处理图片的绝对路径\n", " 2.percentage 是约束需要处理多少比例的图片\n", " 3.需要对读取的图片进行尺寸读取,读取之后,在图片的任意位置添加5~10个 5x5大小的noise patch\n", " 4.读取该图片的bounding box文件 label_path = img_path.replace('images', 'labels').replace('.jpg', '.txt')\n", " 5.bounding box 文件中包含的信息是类别号,Cx,Cy,w,h。每行记录一个目标的分类名称,以及对应的bounding box信息\n", " 6.需要修改该bounding box文件。根据图片上noise patch的相对位置,每个noise patch添加3个新的bounding box,\n", " 类别为0,bounding box的w,h为0.955609和0.5955,但是Cx,Cy依据noise patch的位置而定\n", " 7.修改好的bounding box文件覆盖原始文件即可\n", " 8.处理好之后报告有图片处理完毕\n", " \"\"\"\n", "\n", " # 读取图片绝对路径\n", " with open(train_txt_path, 'r') as file:\n", " lines = file.readlines()\n", "\n", " # 随机选择一定比例的图片\n", " num_images = len(lines)\n", " num_samples = int(num_images * (percentage / 100))\n", "\n", " selected_lines = random.sample(lines, num_samples)\n", "\n", " for line in selected_lines:\n", " # 解析每一行,获取图片路径\n", " image_path = line.strip().split()[0]\n", "\n", " # 打开图片并添加噪声\n", " img = Image.open(image_path)\n", " print(image_path)\n", " draw = ImageDraw.Draw(img)\n", "\n", " # 在图片的任意位置添加5~10个 5x5 大小的噪声色块\n", " num_noise_patches = random.randint(5, 10)\n", " for _ in range(num_noise_patches):\n", " x = random.randint(0, img.width - 5)\n", " y = random.randint(0, img.height - 5)\n", " draw.rectangle([x, y, x + 5, y + 5], fill=(128, 0, 128))\n", "\n", " # 保存修改后的图片\n", " img.save(image_path)\n", "\n", " # 读取相应的bounding box文件路径\n", " label_path = image_path.replace('images', 'labels').replace('.jpg', '.txt')\n", "\n", " # 读取bounding box信息并修改\n", " with open(label_path, 'a') as label_file:\n", " for _ in range(3):\n", " # 随机生成bounding box的Cx, Cy\n", " cx = random.uniform(0, 1)\n", " cy = random.uniform(0, 1)\n", " label_file.write(f\"0 {cx} {cy} 0.955609 0.5955\\n\")\n", "\n", " print(f\"已修改{len(selected_lines)}张图片并更新了bounding box。\")\n", "\n", "\n", "# 使用示例\n", "train_txt_path = '/home/yhsun/ObjectDetection-main/datasets/test_for_noise/test.txt' # 替换为实际的 train.txt 文件路径\n", "modify_images_and_labels(train_txt_path, percentage=100)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/home/yhsun/ObjectDetection-main/datasets/test_for_noise/images/train2017/000000000009.jpg\n", "/home/yhsun/ObjectDetection-main/datasets/test_for_noise/images/train2017/000000000036.jpg\n", "/home/yhsun/ObjectDetection-main/datasets/test_for_noise/images/train2017/000000000042.jpg\n", "/home/yhsun/ObjectDetection-main/datasets/test_for_noise/images/train2017/000000000034.jpg\n", "/home/yhsun/ObjectDetection-main/datasets/test_for_noise/images/train2017/000000000025.jpg\n", "/home/yhsun/ObjectDetection-main/datasets/test_for_noise/images/train2017/000000000030.jpg\n", "已修改6张图片并更新了bounding box。\n" ] } ], "source": [ "from PIL import Image, ImageDraw\n", "import os\n", "import random\n", "\n", "def modify_images_and_labels(train_txt_path, percentage=1):\n", " \"\"\"\n", " 来重新定义这个功能:1.train_txt_path 是包含了待处理图片的绝对路径\n", " 2.percentage 是约束需要处理多少比例的图片\n", " 3.需要对读取的图片进行尺寸读取,读取之后,在图片的任意位置添加5~10个 5x5大小的noise patch\n", " 4.读取该图片的bounding box文件 label_path = img_path.replace('images', 'labels').replace('.jpg', '.txt')\n", " 5.bounding box 文件中包含的信息是类别号,Cx,Cy,w,h。每行记录一个目标的分类名称,以及对应的bounding box信息\n", " 6.需要修改该bounding box文件。根据图片上noise patch的相对位置,每个noise patch添加3个新的bounding box,\n", " 类别为0,bounding box的w,h为0.955609和0.5955,但是Cx,Cy依据noise patch的位置而定\n", " 7.修改好的bounding box文件覆盖原始文件即可\n", " 8.处理好之后报告有图片处理完毕\n", " \"\"\"\n", "\n", " # 读取图片绝对路径\n", " with open(train_txt_path, 'r') as file:\n", " lines = file.readlines()\n", "\n", " # 随机选择一定比例的图片\n", " num_images = len(lines)\n", " num_samples = int(num_images * (percentage / 100))\n", "\n", " selected_lines = random.sample(lines, num_samples)\n", "\n", " for line in selected_lines:\n", " # 解析每一行,获取图片路径\n", " image_path = line.strip().split()[0]\n", "\n", " # 打开图片并添加噪声\n", " img = Image.open(image_path)\n", " print(image_path)\n", " draw = ImageDraw.Draw(img)\n", "\n", " # 在图片的任意位置添加5~10个 5x5 大小的噪声色块\n", " num_noise_patches = random.randint(5, 10)\n", " for _ in range(num_noise_patches):\n", " x = random.randint(0, img.width - 5)\n", " y = random.randint(0, img.height - 5)\n", " draw.rectangle([x, y, x + 5, y + 5], fill=(128, 0, 128))\n", "\n", " # 读取相应的bounding box文件路径\n", " label_path = image_path.replace('images', 'labels').replace('.jpg', '.txt')\n", "\n", " # 读取bounding box信息并修改\n", " with open(label_path, 'a') as label_file:\n", " # 计算bounding box的Cx, Cy\n", " cx = (x + 2.5) / img.width\n", " cy = (y + 2.5) / img.height\n", " label_file.write(f\"0 {cx} {cy} 0.955609 0.5955\\n\")\n", "\n", " # 保存修改后的图片\n", " img.save(image_path)\n", "\n", " print(f\"已修改{len(selected_lines)}张图片并更新了bounding box。\")\n", "\n", "# 使用示例\n", "train_txt_path = '/home/yhsun/ObjectDetection-main/datasets/test_for_noise/test.txt' # 替换为实际的 train.txt 文件路径\n", "modify_images_and_labels(train_txt_path, percentage=100)\n" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "/home/yhsun/ObjectDetection-main/datasets/test_for_noise/images/train2017/000000000036.jpg\n", "/home/yhsun/ObjectDetection-main/datasets/test_for_noise/images/train2017/000000000025.jpg\n", "/home/yhsun/ObjectDetection-main/datasets/test_for_noise/images/train2017/000000000030.jpg\n", "/home/yhsun/ObjectDetection-main/datasets/test_for_noise/images/train2017/000000000034.jpg\n", "/home/yhsun/ObjectDetection-main/datasets/test_for_noise/images/train2017/000000000042.jpg\n", "/home/yhsun/ObjectDetection-main/datasets/test_for_noise/images/train2017/000000000009.jpg\n", "已修改6张图片并更新了 bounding box。\n" ] } ], "source": [ "# version 3\n", "from PIL import Image, ImageDraw\n", "import os\n", "import random\n", "\n", "def modify_images_and_labels(train_txt_path, percentage=1, min_num_patches=5, max_num_patches=10):\n", " \"\"\"\n", " 重新定义功能:\n", " 1. train_txt_path 是包含了待处理图片的绝对路径\n", " 2. percentage 是约束需要处理多少比例的图片\n", " 3. 每张图插入 noise patch 的数量应该在 5~10 之间\n", " 4. noise patch 的大小为 10x10\n", " 5. 修改的 bounding box 大小也要随机\n", " \"\"\"\n", "\n", " # 读取图片绝对路径\n", " with open(train_txt_path, 'r') as file:\n", " lines = file.readlines()\n", "\n", " # 随机选择一定比例的图片\n", " num_images = len(lines)\n", " num_samples = int(num_images * (percentage / 100))\n", "\n", " selected_lines = random.sample(lines, num_samples)\n", "\n", " for line in selected_lines:\n", " # 解析每一行,获取图片路径\n", " image_path = line.strip().split()[0]\n", "\n", " # 打开图片并添加噪声\n", " img = Image.open(image_path)\n", " print(image_path)\n", " draw = ImageDraw.Draw(img)\n", "\n", " # 在图片的任意位置添加随机数量和大小的噪声块\n", " num_noise_patches = random.randint(min_num_patches, max_num_patches)\n", " for _ in range(num_noise_patches):\n", " # 添加 10x10 大小的噪声块\n", " patch_size = 10\n", " x = random.randint(0, img.width - patch_size)\n", " y = random.randint(0, img.height - patch_size)\n", " draw.rectangle([x, y, x + patch_size, y + patch_size], fill=(128, 0, 128))\n", "\n", " # 读取相应的 bounding box 文件路径\n", " label_path = image_path.replace('images', 'labels').replace('.jpg', '.txt')\n", "\n", " # 读取 bounding box 信息并修改\n", " with open(label_path, 'a') as label_file:\n", " # 随机生成 bounding box 大小\n", " box_width = random.uniform(0.5, 1)\n", " box_height = random.uniform(0.5, 1)\n", " # 计算 bounding box 的中心点坐标\n", " cx = (x + patch_size / 2) / img.width\n", " cy = (y + patch_size / 2) / img.height\n", " label_file.write(f\"0 {cx} {cy} {box_width} {box_height}\\n\")\n", "\n", " # 保存修改后的图片\n", " img.save(image_path)\n", "\n", " print(f\"已修改{len(selected_lines)}张图片并更新了 bounding box。\")\n", "\n", "# 使用示例\n", "train_txt_path = '/home/yhsun/ObjectDetection-main/datasets/test_for_noise/test.txt' # 替换为实际的 train.txt 文件路径\n", "modify_images_and_labels(train_txt_path, percentage=100)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "from PIL import Image, ImageDraw\n", "import os\n", "\n", "def visualize_bounding_boxes(train_txt_path, output_dir):\n", " \"\"\"\n", " 读取图片绝对路径和对应的bounding box信息,在图片上绘制bounding box\n", " \"\"\"\n", " # 读取图片绝对路径和对应的bounding box文件\n", " with open(train_txt_path, 'r') as file:\n", " lines = file.readlines()\n", "\n", " for line in lines:\n", " # 解析每一行,获取图片路径\n", " image_path = line.strip().split()[0]\n", "\n", " # 打开图片并准备绘制\n", " img = Image.open(image_path)\n", " draw = ImageDraw.Draw(img)\n", "\n", " # 读取相应的bounding box文件路径\n", " label_path = image_path.replace('images', 'labels').replace('.jpg', '.txt')\n", "\n", " # 读取bounding box信息并绘制\n", " with open(label_path, 'r') as label_file:\n", " for bbox in label_file:\n", " label_info = bbox.strip().split()\n", " cx, cy, w, h = map(float, label_info[1:])\n", " x_min = int((cx - w / 2) * img.width)\n", " y_min = int((cy - h / 2) * img.height)\n", " x_max = int((cx + w / 2) * img.width)\n", " y_max = int((cy + h / 2) * img.height)\n", " draw.rectangle([x_min, y_min, x_max, y_max], outline='red', width=2)\n", "\n", " # 显示带有绘制bounding box的图片\n", " output_path = os.path.join(output_dir, os.path.basename(image_path))\n", " img.save(output_path)\n", "\n", "# Example usage:\n", "output_dir = '/home/yhsun/ObjectDetection-main/datasets/test_for_noise/images/train2017_new'\n", "visualize_bounding_boxes('/home/yhsun/ObjectDetection-main/datasets/test_for_noise/test.txt', output_dir)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def modify_images_and_labels(train_txt_path, percentage=1):\n", " \"\"\"\n", " 来重新定义这个功能:1.train_txt_path 是包含了待处理图片的绝对路径\n", " 2.percentage 是约束需要处理多少比例的图片\n", " 3.需要对读取的图片进行尺寸读取,读取之后,在图片的任意位置添加5~10个 5x5大小的noise patch\n", " 4.读取该图片的boudning box文件 label_path = img_path.repalce('imgages', 'labels').replace('.jpg', '.txt')\n", " 5. boudning box 文件种包含的信息是这样的 图片种含有的目标类 以及它的bounding box的信息 类别号,Cx,Cy,w,h。 例如\n", " 45 0.479492 0.688771 0.955609 0.5955\n", " 45 0.736516 0.247188 0.498875 0.476417\n", " 50 0.637063 0.732938 0.494125 0.510583\n", " 45 0.339438 0.418896 0.678875 0.7815\n", " 49 0.646836 0.132552 0.118047 0.0969375\n", " 49 0.773148 0.129802 0.0907344 0.0972292\n", " 49 0.668297 0.226906 0.131281 0.146896\n", " 49 0.642859 0.0792187 0.148063 0.148062\n", " 每行记录的是一个目标的分类名称,以及对应的波Cx,Cy,w,h\n", " 6. 需要修改该boudning_box 文件。 根绝图片上noise patch的相对位置,每个noise path 添加3个新的bouding box\n", " 其类别为0 bounding box的w,h为0.955609 0.5955,但是cx cy依据noise path的位置而定\n", " 7. 修改好的boudning box文件 覆盖原始文件即可\n", " 8. 处理好之后报苏哦有图片处理完毕\n", "\n", " \"\"\"\n", " return" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The folder contains 302 images.\n" ] } ], "source": [ "import os\n", "import glob\n", "\n", "def count_images_in_folder(folder_path):\n", " # 使用 glob 模块获取文件夹中所有图片文件的路径列表\n", " image_paths = glob.glob(os.path.join(folder_path, '*.jpg')) + glob.glob(os.path.join(folder_path, '*.png'))\n", " # 返回图片文件的数量\n", " return len(image_paths)\n", "\n", "# 指定文件夹路径\n", "folder_path = '/home/yhsun/ObjectDetection-main/datasets/coco_wm/images/test2017'\n", "\n", "# 获取文件夹中图片的数量\n", "num_images = count_images_in_folder(folder_path)\n", "print(f'The folder contains {num_images} images.')\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "pytorch", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.12" } }, "nbformat": 4, "nbformat_minor": 2 }