Download AUG/read_obj&rel_classes.py from kk-Ducker/VR-data: direct link, hf CLI and curl.
- Browser
- Download file 5.53 kB
-
https://huggingface.co/datasets/kk-Ducker/VR-data/resolve/main/AUG/read_obj%26rel_classes.py
- Command line
-
hf download 'hf://datasets/kk-Ducker/VR-data/AUG/read_obj&rel_classes.py'
-
curl -L -o 'read_obj&rel_classes.py' https://huggingface.co/datasets/kk-Ducker/VR-data/resolve/main/AUG/read_obj%26rel_classes.py
5.53 kB
| #!/usr/bin/env python3 | |
| # -*- coding: utf-8 -*- | |
| """读取 CUAV 数据集的物体类别与关系类别并打印。 | |
| 数据来源: | |
| 1. CUAV/annotations.json -> COCO 格式,物体类别在 categories 字段 | |
| 2. CUAV/r_total/*.xlsx -> 每行 5 列: | |
| [subject, subject_id, predicate, object, object_id],第 3 列为关系类别 | |
| xlsx 使用标准库 zipfile + xml.etree 解析,不依赖 pandas / openpyxl。 | |
| 用法: | |
| python3 read_cuav_classes.py | |
| """ | |
| import json | |
| import os | |
| import zipfile | |
| from collections import Counter | |
| from xml.etree import ElementTree as ET | |
| BASE_DIR = os.path.dirname(os.path.abspath(__file__)) | |
| CUAV_DIR = os.path.join(BASE_DIR, "CUAV") | |
| ANNOTATIONS_FILE = os.path.join(CUAV_DIR, "annotations.json") | |
| EXCEL_DIR = os.path.join(CUAV_DIR, "r_total") | |
| NS = "{http://schemas.openxmlformats.org/spreadsheetml/2006/main}" | |
| PREDICATE_COL = 2 # 关系类别所在列(0-based) | |
| def read_xlsx_rows(path): | |
| """读取 xlsx 第一个工作表的全部行,返回 list[list[str | None]]。""" | |
| with zipfile.ZipFile(path) as zf: | |
| names = zf.namelist() | |
| # 共享字符串表:xlsx 里的文本通常存这里,单元格只存下标 | |
| shared = [] | |
| if "xl/sharedStrings.xml" in names: | |
| root = ET.fromstring(zf.read("xl/sharedStrings.xml")) | |
| for si in root.findall(NS + "si"): | |
| shared.append("".join(t.text or "" for t in si.iter(NS + "t"))) | |
| sheet_path = "xl/worksheets/sheet1.xml" | |
| if sheet_path not in names: | |
| candidates = sorted( | |
| n for n in names | |
| if n.startswith("xl/worksheets/sheet") and n.endswith(".xml") | |
| ) | |
| if not candidates: | |
| raise ValueError("xlsx 中未找到工作表: {}".format(path)) | |
| sheet_path = candidates[0] | |
| root = ET.fromstring(zf.read(sheet_path)) | |
| rows = [] | |
| for row in root.iter(NS + "row"): | |
| values = [] | |
| for cell in row.findall(NS + "c"): | |
| ctype = cell.get("t") | |
| v = cell.find(NS + "v") | |
| inline = cell.find(NS + "is") | |
| if ctype == "s" and v is not None: | |
| values.append(shared[int(v.text)]) | |
| elif ctype == "inlineStr" and inline is not None: | |
| values.append("".join(t.text or "" for t in inline.iter(NS + "t"))) | |
| elif v is not None: | |
| values.append(v.text) | |
| else: | |
| values.append(None) | |
| rows.append(values) | |
| return rows | |
| def print_object_classes(): | |
| """物体类别:按 annotations 中的标注数量降序,输出 list 和 dict。""" | |
| with open(ANNOTATIONS_FILE, "r", encoding="utf-8") as f: | |
| data = json.load(f) | |
| categories = data.get("categories", []) | |
| id2name = {c["id"]: c["name"] for c in categories} | |
| counter = Counter() | |
| for ann in data.get("annotations", []): | |
| name = id2name.get(ann["category_id"], str(ann["category_id"])) | |
| counter[name] += 1 | |
| # 补齐没有标注的类别(数量为 0),保证类别全集完整 | |
| for c in categories: | |
| counter.setdefault(c["name"], 0) | |
| ordered = counter.most_common() # 按数量从高到低 | |
| names_list = [name for name, _ in ordered] | |
| count_dict = dict(ordered) | |
| print("=" * 72) | |
| print("一、物体类别(annotations.json,共 {} 类)".format(len(ordered))) | |
| print("=" * 72) | |
| print("按数量从高到低排列的 list:") | |
| print(names_list) | |
| print() | |
| print("字典 {类别: 数量}:") | |
| print(count_dict) | |
| print() | |
| def strip_bracket_content(text): | |
| """删除中英文括号及其内容(支持嵌套),只保留括号外的文本。""" | |
| chars = [] | |
| depth = 0 | |
| for ch in text: | |
| if ch in "((": | |
| depth += 1 | |
| elif ch in "))": | |
| if depth > 0: | |
| depth -= 1 | |
| elif depth == 0: | |
| chars.append(ch) | |
| return "".join(chars) | |
| def print_relation_classes(): | |
| """打印 r_total 下所有 xlsx 中的关系类别(已去掉中文括号注释)。""" | |
| if not os.path.isdir(EXCEL_DIR): | |
| print("目录不存在:{}".format(EXCEL_DIR)) | |
| return | |
| excel_files = sorted( | |
| f for f in os.listdir(EXCEL_DIR) if f.lower().endswith(".xlsx") | |
| ) | |
| counter = Counter() | |
| for fname in excel_files: | |
| for row in read_xlsx_rows(os.path.join(EXCEL_DIR, fname)): | |
| if len(row) <= PREDICATE_COL: | |
| continue | |
| predicate = row[PREDICATE_COL] | |
| if predicate is None: | |
| continue | |
| # 去掉首尾空白、NBSP,并删除括号里的中文翻译 | |
| predicate = str(predicate).replace("\xa0", " ").strip() | |
| predicate = strip_bracket_content(predicate).strip() | |
| if predicate: | |
| counter[predicate] += 1 | |
| ordered = counter.most_common() # 按数量从高到低 | |
| pred_list = [predicate for predicate, _ in ordered] | |
| count_dict = dict(ordered) | |
| print("=" * 72) | |
| print("二、谓词类别(r_total 下 {} 个 xlsx,去重后 {} 类)".format( | |
| len(excel_files), len(ordered))) | |
| print("=" * 72) | |
| print("按数量从高到低排列的 list:") | |
| print(pred_list) | |
| print() | |
| print("字典 {类别: 数量}:") | |
| print(count_dict) | |
| print() | |
| def main(): | |
| print_object_classes() | |
| print_relation_classes() | |
| if __name__ == "__main__": | |
| main() | |