Download plot_category.py from SitongGong/Ref_LVOS: direct link, hf CLI and curl.
- Browser
- Download file 4.01 kB
-
https://huggingface.co/datasets/SitongGong/Ref_LVOS/resolve/main/plot_category.py
- Command line
-
hf download hf://datasets/SitongGong/Ref_LVOS/plot_category.py
-
curl -L -o plot_category.py https://huggingface.co/datasets/SitongGong/Ref_LVOS/resolve/main/plot_category.py
4.01 kB
| import json | |
| import os | |
| import matplotlib.pyplot as plt | |
| import numpy as np | |
| import pandas as pd | |
| from matplotlib.patches import Patch | |
| num_objects = 0 | |
| num_frames = 0 | |
| num_masks = 0 | |
| category_item = [] | |
| json_path = "/Users/gongsitong/Desktop/dataset_processing/Ref_LVOS/video_exp.json" | |
| json_dict = json.load(open(json_path, 'r'))["videos"] | |
| category_list = json.load(open("/Users/gongsitong/Desktop/dataset_processing/Ref_LVOS/category.json", "r"))["category"] | |
| category_dict = dict() | |
| for category in category_list: | |
| category_dict[category] = 0 | |
| category_list = [] | |
| for video_name, video_dict in json_dict.items(): | |
| new_obj_id = -1 | |
| num_frames += len(video_dict["frames"]) | |
| for exp_id, exp_dict in video_dict["expressions"].items(): | |
| if type(exp_dict["category"]) != list: | |
| category_list.append(exp_dict["category"]) | |
| category_dict[exp_dict["category"]] += 1 | |
| else: | |
| for category in exp_dict["category"]: | |
| category_dict[category] += 1 | |
| # print(set(category_list)) | |
| print(category_dict) | |
| # 分组字典 | |
| category_group_map = { | |
| "Transportation": ['car', 'bus', 'motorcycle', 'helicopter'], | |
| "Daily Object": [ | |
| 'bag', 'electronic', 'furniture', 'sign', 'rope', 'cup', 'bottle', 'can', 'tool', 'box', 'card', 'food', "accessory" | |
| ], | |
| # "Food": [ ], | |
| "Animal": [ | |
| 'bird', 'cattle', 'sheep', 'squirrel', 'cat', 'giraffe', 'elephant', | |
| 'kangaroo', 'rabbit', 'zebra', 'bear', 'dog', 'fish', 'primate' | |
| ], | |
| "Sports": [ 'dartboard', 'bowling', 'puck', 'billiards', 'basketball', 'frisbee', 'paddle', "pusher", "kite"], | |
| "Person": ['pedestrian', 'performer', 'exerciser', 'worker', 'observer', 'athlete', 'player', 'dancer', 'skator'] | |
| } | |
| # 创建 DataFrame | |
| df = pd.DataFrame(list(category_dict.items()), columns=['Category', 'Count']) | |
| # 颜色映射 | |
| color_map = { | |
| "Transportation": '#00BFFF', | |
| "Daily Object": '#F5516F', | |
| "Animal": '#ADFF2F', | |
| "Sports": '#BBBBBB', | |
| "Person": '#FFCD5C' | |
| # "Other": '#BBBBBB', | |
| # "Food": '#D94F4F', | |
| } | |
| # 添加 Group 列 | |
| def assign_group(cat): | |
| for group, members in category_group_map.items(): | |
| if cat in members: | |
| return group | |
| return 'Other' | |
| df['Group'] = df['Category'].apply(assign_group) | |
| df['Color'] = df['Group'].map(color_map) | |
| df = df.sort_values(by=['Group', 'Count'], ascending=[True, False]) | |
| # 排序 | |
| # df = df.sort_values(by='Count', ascending=False) | |
| # 绘图 | |
| # plt.rcParams['font.family'] = 'serif' | |
| # plt.figure(figsize=(24, 6)) | |
| # bars = plt.bar(df['Category'], df['Count'], color=df['Color']) #, edgecolor='black') | |
| # plt.yscale("log") | |
| # plt.xticks(rotation=90) | |
| # plt.ylabel("Expression Count (log scale)") | |
| # # plt.title("Expression References per Object Category by Group") | |
| # plt.margins(x=0.01) # ✅ 减少图像两边空白 | |
| # # 添加图例 | |
| # legend_elements = [Patch(facecolor=color_map[g], label=g) for g in color_map] | |
| # plt.legend(handles=legend_elements, loc='upper left', bbox_to_anchor=(1.01, 1)) | |
| # # 保存图像 | |
| # plt.tight_layout() | |
| # plt.savefig("category_expression.png", dpi=300, bbox_inches="tight") | |
| # plt.close() | |
| # ================= 修改部分开始 ================= | |
| # 1. 设置全局字体大小 (基础大小) | |
| plt.rcParams['font.family'] = 'serif' | |
| plt.rcParams['font.size'] = 14 # 调大基础字体 | |
| plt.figure(figsize=(24, 6)) | |
| bars = plt.bar(df['Category'], df['Count'], color=df['Color']) | |
| plt.yscale("log") | |
| # 2. 调大 X 轴刻度字体 | |
| plt.xticks(rotation=90, fontsize=18) | |
| # 3. 调大 Y 轴标签字体 | |
| plt.ylabel("Expression Count (log scale)", fontsize=16) | |
| plt.margins(x=0.01) | |
| # 4. 调大图例字体 | |
| legend_elements = [Patch(facecolor=color_map[g], label=g) for g in color_map] | |
| plt.legend(handles=legend_elements, loc='upper left', bbox_to_anchor=(1.01, 1), fontsize=14) | |
| plt.tight_layout() | |
| plt.savefig("category_expression_large_font.png", dpi=300, bbox_inches="tight") | |
| plt.close() # 显示图片 | |
| # ================= 修改部分结束 ================= |