Download plot_data.py from SitongGong/Ref_LVOS: direct link, hf CLI and curl.
- Browser
- Download file 1.4 kB
-
https://huggingface.co/datasets/SitongGong/Ref_LVOS/resolve/main/plot_data.py
- Command line
-
hf download hf://datasets/SitongGong/Ref_LVOS/plot_data.py
-
curl -L -o plot_data.py https://huggingface.co/datasets/SitongGong/Ref_LVOS/resolve/main/plot_data.py
1.4 kB
| import os | |
| import cv2 | |
| from PIL import Image | |
| import numpy as np | |
| img_path = "/Users/gongsitong/Desktop/dataset_processing/Ref_LVOS/JPEGImages/LongVOS/train/9ErzfLGq" | |
| anno_path = "/Users/gongsitong/Desktop/dataset_processing/Ref_LVOS/Annotations/LongVOS/train/9ErzfLGq" | |
| frame_path_list = sorted(os.listdir(img_path)) | |
| mask_path_list = sorted(os.listdir(anno_path)) | |
| # 可选:输出目录 | |
| output_dir = "/Users/gongsitong/Desktop/dataset_processing/Ref_LVOS/overlay_results_9ErzfLGq" | |
| os.makedirs(output_dir, exist_ok=True) | |
| for frame_name, mask_name in zip(frame_path_list, mask_path_list): | |
| frame = cv2.imread(os.path.join(img_path, frame_name)) | |
| mask = Image.open(os.path.join(anno_path, mask_name)) | |
| gt_mask = np.array(mask) | |
| # 将类别为2的区域设为1,其他设为0 | |
| tmp_masks = np.zeros_like(gt_mask) | |
| tmp_masks[gt_mask == 4] = 1 | |
| gt_mask = tmp_masks | |
| # 创建红色掩码图层 (BGR) | |
| red_mask = np.zeros_like(frame) | |
| red_mask[:, :, 0] = 0 # B通道 | |
| red_mask[:, :, 1] = 0 # G通道 | |
| red_mask[:, :, 2] = 255 # R通道 | |
| # 掩码区域透明叠加到原图上 | |
| alpha = 0.3 | |
| mask_3c = np.stack([gt_mask]*3, axis=-1) # 扩展成3通道 | |
| overlayed = np.where(mask_3c == 1, cv2.addWeighted(frame, 1 - alpha, red_mask, alpha, 0), frame) | |
| # 保存 | |
| save_path = os.path.join(output_dir, frame_name) | |
| cv2.imwrite(save_path, overlayed) |