Upload layer_diff_dataset/batch_mask.py with huggingface_hub
Browse files
layer_diff_dataset/batch_mask.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
import os
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
|
| 6 |
+
# 定义文件夹路径
|
| 7 |
+
image_folder = '/home/jsh/nas_jianchong/sihui.jsh/data/video_dataset/YoutubeVIS/train/JPEGImages'
|
| 8 |
+
mask_folder = '/home/jsh/nas_jianchong/sihui.jsh/data/video_dataset/YoutubeVOS/train/Annotations'
|
| 9 |
+
|
| 10 |
+
# 定义输出文件夹路径
|
| 11 |
+
output_image_folder = '/home/jsh/nas_jianchong/sihui.jsh/data/video_dataset/YoutubeVOS/train/fg'
|
| 12 |
+
output_mask_folder = '/home/jsh/nas_jianchong/sihui.jsh/data/video_dataset/YoutubeVOS/train/mask'
|
| 13 |
+
|
| 14 |
+
# 创建输出文件夹如果它们不存在
|
| 15 |
+
Path(output_image_folder).mkdir(parents=True, exist_ok=True)
|
| 16 |
+
Path(output_mask_folder).mkdir(parents=True, exist_ok=True)
|
| 17 |
+
|
| 18 |
+
# 遍历JPEGImages目录中的所有子目录
|
| 19 |
+
for subdir in next(os.walk(image_folder))[1]:
|
| 20 |
+
print(subdir)
|
| 21 |
+
subdir_path = os.path.join(image_folder, subdir)
|
| 22 |
+
subdir_mask_path = os.path.join(mask_folder, subdir)
|
| 23 |
+
output_image_subdir = os.path.join(output_image_folder, subdir)
|
| 24 |
+
output_mask_subdir = os.path.join(output_mask_folder, subdir)
|
| 25 |
+
Path(output_image_subdir).mkdir(parents=True, exist_ok=True)
|
| 26 |
+
Path(output_mask_subdir).mkdir(parents=True, exist_ok=True)
|
| 27 |
+
# 遍历子目录里的所有文件
|
| 28 |
+
for filename in os.listdir(subdir_path):
|
| 29 |
+
if filename.lower().endswith(('.jpg', '.jpeg', '.png')):
|
| 30 |
+
# 读入图片和对应的掩码
|
| 31 |
+
img_path = os.path.join(subdir_path, filename)
|
| 32 |
+
mask_path = os.path.join(subdir_mask_path, filename.replace('.jpg', '.png'))
|
| 33 |
+
img = cv2.imread(img_path)
|
| 34 |
+
mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE).clip(0, 1)
|
| 35 |
+
|
| 36 |
+
# 如果图片和掩码读取成功
|
| 37 |
+
if img is not None and mask is not None:
|
| 38 |
+
# 应用掩码
|
| 39 |
+
mask_image = mask * 255
|
| 40 |
+
result_img = img * mask[:, :, np.newaxis]
|
| 41 |
+
|
| 42 |
+
# 构建输出文件名并保存图片和掩码
|
| 43 |
+
# output_img_name = f"{subdir}/{filename}"
|
| 44 |
+
# output_mask_name = f"{subdir}/{filename}"
|
| 45 |
+
output_img_path = os.path.join(output_image_subdir, filename.replace('.jpg', '.png'))
|
| 46 |
+
output_mask_path = os.path.join(output_mask_subdir, filename.replace('.jpg', '.png'))
|
| 47 |
+
# print(output_img_path)
|
| 48 |
+
# print(output_mask_path)
|
| 49 |
+
|
| 50 |
+
cv2.imwrite(output_img_path, result_img)
|
| 51 |
+
cv2.imwrite(output_mask_path, mask_image)
|
| 52 |
+
# exit(0)
|