Download plot_occlusions.py from SitongGong/Ref_LVOS: direct link, hf CLI and curl.
- Browser
- Download file 4.74 kB
-
https://huggingface.co/datasets/SitongGong/Ref_LVOS/resolve/main/plot_occlusions.py
- Command line
-
hf download hf://datasets/SitongGong/Ref_LVOS/plot_occlusions.py
-
curl -L -o plot_occlusions.py https://huggingface.co/datasets/SitongGong/Ref_LVOS/resolve/main/plot_occlusions.py
4.74 kB
| import os | |
| import json | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| from matplotlib import rcParams | |
| from PIL import Image | |
| # 设置全局字体为 Times New Roman | |
| plt.rcParams['font.family'] = 'serif' | |
| # 加载 JSON 数据 | |
| json_path = "/Users/gongsitong/Desktop/dataset_processing/Ref_LVOS/video_exp.json" | |
| with open(json_path, 'r') as f: | |
| json_dict = json.load(f)["videos"] | |
| # 获取每个视频的帧数 | |
| gt_mask_path = "/Users/gongsitong/Desktop/dataset_processing/Ref_LVOS/Annotations" | |
| occlusion_list = [] | |
| change_time_list = [] | |
| occlusion_frame_list = [] | |
| num_valid_masks = 0 | |
| for vid_name, video_dict in json_dict.items(): | |
| new_obj_id = 0 # 该视频中实例的数量 | |
| object_list = [] | |
| for exp_id, exp_dict in video_dict["expressions"].items(): | |
| object_list.extend(exp_dict["obj_id"]) | |
| # for obj_id in exp_dict["obj_id"]: | |
| # if int(obj_id) != new_obj_id: | |
| # new_obj_id = int(obj_id) | |
| # object_list.append(obj_id) | |
| vid_frames = video_dict["frames"] | |
| object_list = list(set(object_list)) | |
| # obj_ids = list(range(num_object)) | |
| if "LongVOS" in vid_name: | |
| for obj_id in object_list: | |
| total_frames = len(vid_frames) | |
| num_occlusion_frames = 0 | |
| per_obj_change_time_list = [] | |
| for frame_idx, frame_name in enumerate(vid_frames): | |
| # processing the gt mask | |
| gt_mask_ = np.array(Image.open(os.path.join(gt_mask_path, vid_name, frame_name + '.png'))) | |
| tmp_masks = np.zeros_like(gt_mask_) | |
| tmp_masks[gt_mask_ == int(obj_id)] = 1 | |
| gt_mask = tmp_masks | |
| gt_mask[gt_mask == 255] = 0 | |
| if np.all(gt_mask == 0): | |
| num_occlusion_frames += 1 | |
| per_obj_change_time_list.append(0) | |
| else: | |
| per_obj_change_time_list.append(1) | |
| num_valid_masks += 1 | |
| change_time_list.append(int(np.count_nonzero(np.diff(per_obj_change_time_list)))) | |
| occlusion_list.append(num_occlusion_frames / total_frames) | |
| occlusion_frame_list.append(num_occlusion_frames) | |
| else: | |
| anno_path = os.path.join(gt_mask_path, vid_name) | |
| mask_list = [os.path.join(anno_path, d) for d in os.listdir(anno_path)] | |
| mask_list.sort() | |
| for obj_id in object_list: | |
| mask_path = mask_list[int(obj_id) - 1] | |
| mask_path_list = sorted(os.listdir(mask_path)) | |
| total_frames = len(mask_path_list) | |
| per_obj_change_time_list = [] | |
| num_occlusion_frames = 0 | |
| for i, mask_path_ in enumerate(mask_path_list): | |
| mask_path_ = os.path.join(mask_path, mask_path_) | |
| mask = np.array(Image.open(mask_path_)) > 0 | |
| if np.all(mask == 0) : | |
| num_occlusion_frames += 1 | |
| per_obj_change_time_list.append(0) | |
| else: | |
| num_valid_masks += 1 | |
| per_obj_change_time_list.append(1) | |
| change_time_list.append(int(np.count_nonzero(np.diff(per_obj_change_time_list)))) | |
| occlusion_list.append(num_occlusion_frames / total_frames) | |
| occlusion_frame_list.append(num_occlusion_frames) | |
| print(occlusion_list) | |
| print(change_time_list) | |
| print(occlusion_frame_list) | |
| print("num_valid_masks: {}".format(num_valid_masks)) | |
| with open("/Users/gongsitong/Desktop/dataset_processing/Ref_LVOS/occlusion_list.txt", "w", encoding="utf-8") as f: | |
| for item in occlusion_list: | |
| f.write(f"{item}\n") | |
| print("List has been saved to occlusion_list.txt") | |
| with open("/Users/gongsitong/Desktop/dataset_processing/Ref_LVOS/change_time_list.txt", "w", encoding="utf-8") as f: | |
| for item in change_time_list: | |
| f.write(f"{item}\n") | |
| print("List has been saved to change_time_list.txt") | |
| with open("/Users/gongsitong/Desktop/dataset_processing/Ref_LVOS/occlusion_frame_list.txt", "w", encoding="utf-8") as f: | |
| for item in occlusion_frame_list: | |
| f.write(f"{item}\n") | |
| print("List has been saved to occlusion_frame_list.txt") | |
| # bins = np.arange(0, 1.1, 0.1) # 生成 [0.0, 0.1, ..., 1.0] | |
| # # 绘制直方图 | |
| # plt.figure(figsize=(6, 4)) | |
| # plt.hist(occlusion_list, bins=bins, color=(127 / 255, 255 / 255, 0), edgecolor='black') | |
| # # 设置标签与标题 | |
| # plt.xlabel("Occlusion Ratio") | |
| # plt.ylabel("Instance Count") | |
| # plt.title("Occlusion Ratio Distribution (bin=0.1)") | |
| # plt.xticks(bins) # 设置刻度更精确 | |
| # plt.tight_layout() | |
| # # 保存图像 | |
| # plt.savefig("occlusion_ratio_distribution.png", dpi=300) | |
| # plt.close() |