dataset-toolkit 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dataset_toolkit-0.1.0/LICENSE +22 -0
- dataset_toolkit-0.1.0/MANIFEST.in +23 -0
- dataset_toolkit-0.1.0/PKG-INFO +273 -0
- dataset_toolkit-0.1.0/README.md +240 -0
- dataset_toolkit-0.1.0/dataset_toolkit/__init__.py +79 -0
- dataset_toolkit-0.1.0/dataset_toolkit/exporters/__init__.py +0 -0
- dataset_toolkit-0.1.0/dataset_toolkit/exporters/coco_exporter.py +64 -0
- dataset_toolkit-0.1.0/dataset_toolkit/exporters/txt_exporter.py +32 -0
- dataset_toolkit-0.1.0/dataset_toolkit/loaders/__init__.py +0 -0
- dataset_toolkit-0.1.0/dataset_toolkit/loaders/local_loader.py +64 -0
- dataset_toolkit-0.1.0/dataset_toolkit/models.py +26 -0
- dataset_toolkit-0.1.0/dataset_toolkit/pipeline.py +176 -0
- dataset_toolkit-0.1.0/dataset_toolkit/processors/__init__.py +0 -0
- dataset_toolkit-0.1.0/dataset_toolkit/processors/merger.py +62 -0
- dataset_toolkit-0.1.0/dataset_toolkit/utils/__init__.py +0 -0
- dataset_toolkit-0.1.0/dataset_toolkit/utils/coords.py +15 -0
- dataset_toolkit-0.1.0/dataset_toolkit.egg-info/PKG-INFO +273 -0
- dataset_toolkit-0.1.0/dataset_toolkit.egg-info/SOURCES.txt +29 -0
- dataset_toolkit-0.1.0/dataset_toolkit.egg-info/dependency_links.txt +1 -0
- dataset_toolkit-0.1.0/dataset_toolkit.egg-info/requires.txt +7 -0
- dataset_toolkit-0.1.0/dataset_toolkit.egg-info/top_level.txt +1 -0
- dataset_toolkit-0.1.0/examples/basic_usage.py +161 -0
- dataset_toolkit-0.1.0/pyproject.toml +51 -0
- dataset_toolkit-0.1.0/requirements.txt +6 -0
- dataset_toolkit-0.1.0/setup.cfg +4 -0
- dataset_toolkit-0.1.0/setup.py +49 -0
- dataset_toolkit-0.1.0/tests/__init__.py +5 -0
- dataset_toolkit-0.1.0/tests/conftest.py +44 -0
- dataset_toolkit-0.1.0/tests/test_exporters.py +160 -0
- dataset_toolkit-0.1.0/tests/test_loaders.py +87 -0
- dataset_toolkit-0.1.0/tests/test_processors.py +135 -0
@@ -0,0 +1,22 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 Your Name
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
22
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# 包含额外的文件到分发包中
|
2
|
+
include README.md
|
3
|
+
include LICENSE
|
4
|
+
include requirements.txt
|
5
|
+
include pyproject.toml
|
6
|
+
|
7
|
+
# 包含示例代码
|
8
|
+
recursive-include examples *.py
|
9
|
+
|
10
|
+
# 包含测试文件
|
11
|
+
recursive-include tests *.py
|
12
|
+
|
13
|
+
# 排除不需要的文件
|
14
|
+
global-exclude __pycache__
|
15
|
+
global-exclude *.py[co]
|
16
|
+
global-exclude .DS_Store
|
17
|
+
global-exclude *.so
|
18
|
+
global-exclude *.dylib
|
19
|
+
|
20
|
+
# 排除输出目录
|
21
|
+
prune outputs
|
22
|
+
prune .git
|
23
|
+
|
@@ -0,0 +1,273 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: dataset-toolkit
|
3
|
+
Version: 0.1.0
|
4
|
+
Summary: 一个用于加载、处理和导出计算机视觉数据集的工具包
|
5
|
+
Home-page: https://github.com/yourusername/dataset-toolkit
|
6
|
+
Author: Your Name
|
7
|
+
Author-email: Your Name <your.email@example.com>
|
8
|
+
License: MIT
|
9
|
+
Project-URL: Homepage, https://github.com/yourusername/dataset-toolkit
|
10
|
+
Project-URL: Documentation, https://dataset-toolkit.readthedocs.io
|
11
|
+
Project-URL: Repository, https://github.com/yourusername/dataset-toolkit
|
12
|
+
Project-URL: Bug Tracker, https://github.com/yourusername/dataset-toolkit/issues
|
13
|
+
Keywords: computer vision,dataset,yolo,coco,machine learning
|
14
|
+
Classifier: Development Status :: 3 - Alpha
|
15
|
+
Classifier: Intended Audience :: Developers
|
16
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
17
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
18
|
+
Classifier: License :: OSI Approved :: MIT License
|
19
|
+
Classifier: Programming Language :: Python :: 3
|
20
|
+
Requires-Python: >=3.7
|
21
|
+
Description-Content-Type: text/markdown
|
22
|
+
License-File: LICENSE
|
23
|
+
Requires-Dist: Pillow>=8.0.0
|
24
|
+
Provides-Extra: dev
|
25
|
+
Requires-Dist: pytest>=6.0; extra == "dev"
|
26
|
+
Requires-Dist: pytest-cov>=2.0; extra == "dev"
|
27
|
+
Requires-Dist: black>=21.0; extra == "dev"
|
28
|
+
Requires-Dist: flake8>=3.9; extra == "dev"
|
29
|
+
Dynamic: author
|
30
|
+
Dynamic: home-page
|
31
|
+
Dynamic: license-file
|
32
|
+
Dynamic: requires-python
|
33
|
+
|
34
|
+
# Dataset Toolkit
|
35
|
+
|
36
|
+
一个功能强大、易于使用的 Python 工具包,用于处理计算机视觉数据集。支持多种数据格式的加载、合并、转换和导出。
|
37
|
+
|
38
|
+
## ✨ 特性
|
39
|
+
|
40
|
+
- 🔄 **多格式支持**:支持 YOLO、COCO 等常见格式
|
41
|
+
- 🔗 **数据集合并**:轻松合并多个数据集,支持类别重映射
|
42
|
+
- 📤 **灵活导出**:导出为 COCO JSON、TXT 等多种格式
|
43
|
+
- 🛠️ **工具函数**:提供坐标转换等实用工具
|
44
|
+
- 📦 **标准化数据模型**:统一的内部数据表示,方便扩展
|
45
|
+
|
46
|
+
## 📦 安装
|
47
|
+
|
48
|
+
### 从 PyPI 安装(推荐)
|
49
|
+
|
50
|
+
```bash
|
51
|
+
pip install dataset-toolkit
|
52
|
+
```
|
53
|
+
|
54
|
+
### 从源码安装
|
55
|
+
|
56
|
+
```bash
|
57
|
+
git clone https://github.com/yourusername/dataset-toolkit.git
|
58
|
+
cd dataset-toolkit
|
59
|
+
pip install -e .
|
60
|
+
```
|
61
|
+
|
62
|
+
### 开发模式安装
|
63
|
+
|
64
|
+
```bash
|
65
|
+
pip install -e ".[dev]"
|
66
|
+
```
|
67
|
+
|
68
|
+
## 🚀 快速开始
|
69
|
+
|
70
|
+
### 基本用法
|
71
|
+
|
72
|
+
```python
|
73
|
+
from dataset_toolkit import load_yolo_from_local, merge_datasets, export_to_coco
|
74
|
+
|
75
|
+
# 1. 加载 YOLO 格式数据集
|
76
|
+
dataset1 = load_yolo_from_local(
|
77
|
+
"/path/to/dataset1",
|
78
|
+
categories={0: 'cat', 1: 'dog'}
|
79
|
+
)
|
80
|
+
|
81
|
+
dataset2 = load_yolo_from_local(
|
82
|
+
"/path/to/dataset2",
|
83
|
+
categories={0: 'car', 1: 'bicycle'}
|
84
|
+
)
|
85
|
+
|
86
|
+
# 2. 合并数据集(带类别重映射)
|
87
|
+
final_categories = {0: 'animal', 1: 'vehicle'}
|
88
|
+
category_mapping = {
|
89
|
+
'cat': 'animal',
|
90
|
+
'dog': 'animal',
|
91
|
+
'car': 'vehicle',
|
92
|
+
'bicycle': 'vehicle'
|
93
|
+
}
|
94
|
+
|
95
|
+
merged = merge_datasets(
|
96
|
+
datasets=[dataset1, dataset2],
|
97
|
+
category_mapping=category_mapping,
|
98
|
+
final_categories=final_categories,
|
99
|
+
new_dataset_name="merged_dataset"
|
100
|
+
)
|
101
|
+
|
102
|
+
# 3. 导出为 COCO 格式
|
103
|
+
export_to_coco(merged, "output/merged.json")
|
104
|
+
```
|
105
|
+
|
106
|
+
### 链式 API 用法
|
107
|
+
|
108
|
+
```python
|
109
|
+
from dataset_toolkit import DatasetPipeline
|
110
|
+
|
111
|
+
# 使用管道模式处理数据集
|
112
|
+
pipeline = DatasetPipeline()
|
113
|
+
result = (pipeline
|
114
|
+
.load_yolo("/path/to/dataset1", {0: 'cat', 1: 'dog'})
|
115
|
+
.load_yolo("/path/to/dataset2", {0: 'car'})
|
116
|
+
.merge(
|
117
|
+
category_mapping={'cat': 'animal', 'dog': 'animal', 'car': 'vehicle'},
|
118
|
+
final_categories={0: 'animal', 1: 'vehicle'}
|
119
|
+
)
|
120
|
+
.export_coco("output/merged.json")
|
121
|
+
.execute())
|
122
|
+
```
|
123
|
+
|
124
|
+
## 📚 API 文档
|
125
|
+
|
126
|
+
### 数据加载器
|
127
|
+
|
128
|
+
#### `load_yolo_from_local(dataset_path, categories)`
|
129
|
+
|
130
|
+
从本地文件系统加载 YOLO 格式的数据集。
|
131
|
+
|
132
|
+
**参数:**
|
133
|
+
- `dataset_path` (str): 数据集根目录路径,应包含 `images/` 和 `labels/` 子目录
|
134
|
+
- `categories` (Dict[int, str]): 类别ID到类别名的映射
|
135
|
+
|
136
|
+
**返回:**
|
137
|
+
- `Dataset`: 标准化的数据集对象
|
138
|
+
|
139
|
+
**示例:**
|
140
|
+
```python
|
141
|
+
dataset = load_yolo_from_local(
|
142
|
+
"/data/my_dataset",
|
143
|
+
categories={0: 'person', 1: 'car'}
|
144
|
+
)
|
145
|
+
```
|
146
|
+
|
147
|
+
### 数据处理器
|
148
|
+
|
149
|
+
#### `merge_datasets(datasets, category_mapping, final_categories, new_dataset_name)`
|
150
|
+
|
151
|
+
合并多个数据集,支持类别重映射。
|
152
|
+
|
153
|
+
**参数:**
|
154
|
+
- `datasets` (List[Dataset]): 要合并的数据集列表
|
155
|
+
- `category_mapping` (Dict[str, str]): 旧类别名到新类别名的映射
|
156
|
+
- `final_categories` (Dict[int, str]): 最终的类别体系
|
157
|
+
- `new_dataset_name` (str, optional): 合并后数据集的名称
|
158
|
+
|
159
|
+
**返回:**
|
160
|
+
- `Dataset`: 合并后的数据集对象
|
161
|
+
|
162
|
+
### 数据导出器
|
163
|
+
|
164
|
+
#### `export_to_coco(dataset, output_path)`
|
165
|
+
|
166
|
+
导出为 COCO JSON 格式。
|
167
|
+
|
168
|
+
**参数:**
|
169
|
+
- `dataset` (Dataset): 要导出的数据集
|
170
|
+
- `output_path` (str): 输出文件路径
|
171
|
+
|
172
|
+
#### `export_to_txt(dataset, output_path, use_relative_paths, base_path)`
|
173
|
+
|
174
|
+
导出为 TXT 格式。
|
175
|
+
|
176
|
+
**参数:**
|
177
|
+
- `dataset` (Dataset): 要导出的数据集
|
178
|
+
- `output_path` (str): 输出文件路径
|
179
|
+
- `use_relative_paths` (bool, optional): 是否使用相对路径
|
180
|
+
- `base_path` (str, optional): 相对路径的基准目录
|
181
|
+
|
182
|
+
## 🏗️ 架构设计
|
183
|
+
|
184
|
+
```
|
185
|
+
dataset_toolkit/
|
186
|
+
├── models.py # 数据模型定义
|
187
|
+
├── loaders/ # 数据加载器
|
188
|
+
│ ├── local_loader.py # 本地文件系统加载器
|
189
|
+
│ └── remote_loader.py # 远程数据源加载器(待开发)
|
190
|
+
├── processors/ # 数据处理器
|
191
|
+
│ ├── merger.py # 数据集合并
|
192
|
+
│ └── filter.py # 数据过滤(待开发)
|
193
|
+
├── exporters/ # 数据导出器
|
194
|
+
│ ├── coco_exporter.py # COCO格式导出
|
195
|
+
│ └── txt_exporter.py # TXT格式导出
|
196
|
+
└── utils/ # 工具函数
|
197
|
+
└── coords.py # 坐标转换
|
198
|
+
```
|
199
|
+
|
200
|
+
## 🔧 高级用法
|
201
|
+
|
202
|
+
### 自定义数据加载器
|
203
|
+
|
204
|
+
```python
|
205
|
+
from dataset_toolkit.models import Dataset, ImageAnnotation
|
206
|
+
from dataset_toolkit.loaders import BaseLoader
|
207
|
+
|
208
|
+
class CustomLoader(BaseLoader):
|
209
|
+
def load(self, path, **kwargs):
|
210
|
+
# 实现你的自定义加载逻辑
|
211
|
+
dataset = Dataset(name="custom")
|
212
|
+
# ... 加载数据 ...
|
213
|
+
return dataset
|
214
|
+
```
|
215
|
+
|
216
|
+
### 批量处理
|
217
|
+
|
218
|
+
```python
|
219
|
+
from pathlib import Path
|
220
|
+
from dataset_toolkit import load_yolo_from_local, export_to_coco
|
221
|
+
|
222
|
+
# 批量处理多个数据集
|
223
|
+
dataset_dirs = [
|
224
|
+
"/data/dataset1",
|
225
|
+
"/data/dataset2",
|
226
|
+
"/data/dataset3"
|
227
|
+
]
|
228
|
+
|
229
|
+
categories = {0: 'object'}
|
230
|
+
|
231
|
+
for dataset_dir in dataset_dirs:
|
232
|
+
ds = load_yolo_from_local(dataset_dir, categories)
|
233
|
+
output_name = Path(dataset_dir).name + ".json"
|
234
|
+
export_to_coco(ds, f"output/{output_name}")
|
235
|
+
```
|
236
|
+
|
237
|
+
## 🧪 测试
|
238
|
+
|
239
|
+
运行测试:
|
240
|
+
|
241
|
+
```bash
|
242
|
+
pytest
|
243
|
+
```
|
244
|
+
|
245
|
+
生成测试覆盖率报告:
|
246
|
+
|
247
|
+
```bash
|
248
|
+
pytest --cov=dataset_toolkit --cov-report=html
|
249
|
+
```
|
250
|
+
|
251
|
+
## 📝 开发计划
|
252
|
+
|
253
|
+
- [ ] 支持更多数据格式(Pascal VOC、YOLO v8等)
|
254
|
+
- [ ] 添加数据增强功能
|
255
|
+
- [ ] 支持远程数据源(S3、HTTP等)
|
256
|
+
- [ ] 添加数据统计和可视化功能
|
257
|
+
- [ ] 提供命令行工具
|
258
|
+
- [ ] 支持视频数据集
|
259
|
+
|
260
|
+
## 🤝 贡献
|
261
|
+
|
262
|
+
欢迎提交 Issue 和 Pull Request!
|
263
|
+
|
264
|
+
## 📄 许可证
|
265
|
+
|
266
|
+
MIT License
|
267
|
+
|
268
|
+
## 📧 联系方式
|
269
|
+
|
270
|
+
如有问题,请通过以下方式联系:
|
271
|
+
- Email: your.email@example.com
|
272
|
+
- GitHub Issues: https://github.com/yourusername/dataset-toolkit/issues
|
273
|
+
|
@@ -0,0 +1,240 @@
|
|
1
|
+
# Dataset Toolkit
|
2
|
+
|
3
|
+
一个功能强大、易于使用的 Python 工具包,用于处理计算机视觉数据集。支持多种数据格式的加载、合并、转换和导出。
|
4
|
+
|
5
|
+
## ✨ 特性
|
6
|
+
|
7
|
+
- 🔄 **多格式支持**:支持 YOLO、COCO 等常见格式
|
8
|
+
- 🔗 **数据集合并**:轻松合并多个数据集,支持类别重映射
|
9
|
+
- 📤 **灵活导出**:导出为 COCO JSON、TXT 等多种格式
|
10
|
+
- 🛠️ **工具函数**:提供坐标转换等实用工具
|
11
|
+
- 📦 **标准化数据模型**:统一的内部数据表示,方便扩展
|
12
|
+
|
13
|
+
## 📦 安装
|
14
|
+
|
15
|
+
### 从 PyPI 安装(推荐)
|
16
|
+
|
17
|
+
```bash
|
18
|
+
pip install dataset-toolkit
|
19
|
+
```
|
20
|
+
|
21
|
+
### 从源码安装
|
22
|
+
|
23
|
+
```bash
|
24
|
+
git clone https://github.com/yourusername/dataset-toolkit.git
|
25
|
+
cd dataset-toolkit
|
26
|
+
pip install -e .
|
27
|
+
```
|
28
|
+
|
29
|
+
### 开发模式安装
|
30
|
+
|
31
|
+
```bash
|
32
|
+
pip install -e ".[dev]"
|
33
|
+
```
|
34
|
+
|
35
|
+
## 🚀 快速开始
|
36
|
+
|
37
|
+
### 基本用法
|
38
|
+
|
39
|
+
```python
|
40
|
+
from dataset_toolkit import load_yolo_from_local, merge_datasets, export_to_coco
|
41
|
+
|
42
|
+
# 1. 加载 YOLO 格式数据集
|
43
|
+
dataset1 = load_yolo_from_local(
|
44
|
+
"/path/to/dataset1",
|
45
|
+
categories={0: 'cat', 1: 'dog'}
|
46
|
+
)
|
47
|
+
|
48
|
+
dataset2 = load_yolo_from_local(
|
49
|
+
"/path/to/dataset2",
|
50
|
+
categories={0: 'car', 1: 'bicycle'}
|
51
|
+
)
|
52
|
+
|
53
|
+
# 2. 合并数据集(带类别重映射)
|
54
|
+
final_categories = {0: 'animal', 1: 'vehicle'}
|
55
|
+
category_mapping = {
|
56
|
+
'cat': 'animal',
|
57
|
+
'dog': 'animal',
|
58
|
+
'car': 'vehicle',
|
59
|
+
'bicycle': 'vehicle'
|
60
|
+
}
|
61
|
+
|
62
|
+
merged = merge_datasets(
|
63
|
+
datasets=[dataset1, dataset2],
|
64
|
+
category_mapping=category_mapping,
|
65
|
+
final_categories=final_categories,
|
66
|
+
new_dataset_name="merged_dataset"
|
67
|
+
)
|
68
|
+
|
69
|
+
# 3. 导出为 COCO 格式
|
70
|
+
export_to_coco(merged, "output/merged.json")
|
71
|
+
```
|
72
|
+
|
73
|
+
### 链式 API 用法
|
74
|
+
|
75
|
+
```python
|
76
|
+
from dataset_toolkit import DatasetPipeline
|
77
|
+
|
78
|
+
# 使用管道模式处理数据集
|
79
|
+
pipeline = DatasetPipeline()
|
80
|
+
result = (pipeline
|
81
|
+
.load_yolo("/path/to/dataset1", {0: 'cat', 1: 'dog'})
|
82
|
+
.load_yolo("/path/to/dataset2", {0: 'car'})
|
83
|
+
.merge(
|
84
|
+
category_mapping={'cat': 'animal', 'dog': 'animal', 'car': 'vehicle'},
|
85
|
+
final_categories={0: 'animal', 1: 'vehicle'}
|
86
|
+
)
|
87
|
+
.export_coco("output/merged.json")
|
88
|
+
.execute())
|
89
|
+
```
|
90
|
+
|
91
|
+
## 📚 API 文档
|
92
|
+
|
93
|
+
### 数据加载器
|
94
|
+
|
95
|
+
#### `load_yolo_from_local(dataset_path, categories)`
|
96
|
+
|
97
|
+
从本地文件系统加载 YOLO 格式的数据集。
|
98
|
+
|
99
|
+
**参数:**
|
100
|
+
- `dataset_path` (str): 数据集根目录路径,应包含 `images/` 和 `labels/` 子目录
|
101
|
+
- `categories` (Dict[int, str]): 类别ID到类别名的映射
|
102
|
+
|
103
|
+
**返回:**
|
104
|
+
- `Dataset`: 标准化的数据集对象
|
105
|
+
|
106
|
+
**示例:**
|
107
|
+
```python
|
108
|
+
dataset = load_yolo_from_local(
|
109
|
+
"/data/my_dataset",
|
110
|
+
categories={0: 'person', 1: 'car'}
|
111
|
+
)
|
112
|
+
```
|
113
|
+
|
114
|
+
### 数据处理器
|
115
|
+
|
116
|
+
#### `merge_datasets(datasets, category_mapping, final_categories, new_dataset_name)`
|
117
|
+
|
118
|
+
合并多个数据集,支持类别重映射。
|
119
|
+
|
120
|
+
**参数:**
|
121
|
+
- `datasets` (List[Dataset]): 要合并的数据集列表
|
122
|
+
- `category_mapping` (Dict[str, str]): 旧类别名到新类别名的映射
|
123
|
+
- `final_categories` (Dict[int, str]): 最终的类别体系
|
124
|
+
- `new_dataset_name` (str, optional): 合并后数据集的名称
|
125
|
+
|
126
|
+
**返回:**
|
127
|
+
- `Dataset`: 合并后的数据集对象
|
128
|
+
|
129
|
+
### 数据导出器
|
130
|
+
|
131
|
+
#### `export_to_coco(dataset, output_path)`
|
132
|
+
|
133
|
+
导出为 COCO JSON 格式。
|
134
|
+
|
135
|
+
**参数:**
|
136
|
+
- `dataset` (Dataset): 要导出的数据集
|
137
|
+
- `output_path` (str): 输出文件路径
|
138
|
+
|
139
|
+
#### `export_to_txt(dataset, output_path, use_relative_paths, base_path)`
|
140
|
+
|
141
|
+
导出为 TXT 格式。
|
142
|
+
|
143
|
+
**参数:**
|
144
|
+
- `dataset` (Dataset): 要导出的数据集
|
145
|
+
- `output_path` (str): 输出文件路径
|
146
|
+
- `use_relative_paths` (bool, optional): 是否使用相对路径
|
147
|
+
- `base_path` (str, optional): 相对路径的基准目录
|
148
|
+
|
149
|
+
## 🏗️ 架构设计
|
150
|
+
|
151
|
+
```
|
152
|
+
dataset_toolkit/
|
153
|
+
├── models.py # 数据模型定义
|
154
|
+
├── loaders/ # 数据加载器
|
155
|
+
│ ├── local_loader.py # 本地文件系统加载器
|
156
|
+
│ └── remote_loader.py # 远程数据源加载器(待开发)
|
157
|
+
├── processors/ # 数据处理器
|
158
|
+
│ ├── merger.py # 数据集合并
|
159
|
+
│ └── filter.py # 数据过滤(待开发)
|
160
|
+
├── exporters/ # 数据导出器
|
161
|
+
│ ├── coco_exporter.py # COCO格式导出
|
162
|
+
│ └── txt_exporter.py # TXT格式导出
|
163
|
+
└── utils/ # 工具函数
|
164
|
+
└── coords.py # 坐标转换
|
165
|
+
```
|
166
|
+
|
167
|
+
## 🔧 高级用法
|
168
|
+
|
169
|
+
### 自定义数据加载器
|
170
|
+
|
171
|
+
```python
|
172
|
+
from dataset_toolkit.models import Dataset, ImageAnnotation
|
173
|
+
from dataset_toolkit.loaders import BaseLoader
|
174
|
+
|
175
|
+
class CustomLoader(BaseLoader):
|
176
|
+
def load(self, path, **kwargs):
|
177
|
+
# 实现你的自定义加载逻辑
|
178
|
+
dataset = Dataset(name="custom")
|
179
|
+
# ... 加载数据 ...
|
180
|
+
return dataset
|
181
|
+
```
|
182
|
+
|
183
|
+
### 批量处理
|
184
|
+
|
185
|
+
```python
|
186
|
+
from pathlib import Path
|
187
|
+
from dataset_toolkit import load_yolo_from_local, export_to_coco
|
188
|
+
|
189
|
+
# 批量处理多个数据集
|
190
|
+
dataset_dirs = [
|
191
|
+
"/data/dataset1",
|
192
|
+
"/data/dataset2",
|
193
|
+
"/data/dataset3"
|
194
|
+
]
|
195
|
+
|
196
|
+
categories = {0: 'object'}
|
197
|
+
|
198
|
+
for dataset_dir in dataset_dirs:
|
199
|
+
ds = load_yolo_from_local(dataset_dir, categories)
|
200
|
+
output_name = Path(dataset_dir).name + ".json"
|
201
|
+
export_to_coco(ds, f"output/{output_name}")
|
202
|
+
```
|
203
|
+
|
204
|
+
## 🧪 测试
|
205
|
+
|
206
|
+
运行测试:
|
207
|
+
|
208
|
+
```bash
|
209
|
+
pytest
|
210
|
+
```
|
211
|
+
|
212
|
+
生成测试覆盖率报告:
|
213
|
+
|
214
|
+
```bash
|
215
|
+
pytest --cov=dataset_toolkit --cov-report=html
|
216
|
+
```
|
217
|
+
|
218
|
+
## 📝 开发计划
|
219
|
+
|
220
|
+
- [ ] 支持更多数据格式(Pascal VOC、YOLO v8等)
|
221
|
+
- [ ] 添加数据增强功能
|
222
|
+
- [ ] 支持远程数据源(S3、HTTP等)
|
223
|
+
- [ ] 添加数据统计和可视化功能
|
224
|
+
- [ ] 提供命令行工具
|
225
|
+
- [ ] 支持视频数据集
|
226
|
+
|
227
|
+
## 🤝 贡献
|
228
|
+
|
229
|
+
欢迎提交 Issue 和 Pull Request!
|
230
|
+
|
231
|
+
## 📄 许可证
|
232
|
+
|
233
|
+
MIT License
|
234
|
+
|
235
|
+
## 📧 联系方式
|
236
|
+
|
237
|
+
如有问题,请通过以下方式联系:
|
238
|
+
- Email: your.email@example.com
|
239
|
+
- GitHub Issues: https://github.com/yourusername/dataset-toolkit/issues
|
240
|
+
|
@@ -0,0 +1,79 @@
|
|
1
|
+
"""
|
2
|
+
Dataset Toolkit - 计算机视觉数据集处理工具包
|
3
|
+
|
4
|
+
这个工具包提供了一套完整的解决方案,用于加载、处理和导出计算机视觉数据集。
|
5
|
+
|
6
|
+
主要功能:
|
7
|
+
- 加载多种格式的数据集(YOLO、COCO等)
|
8
|
+
- 合并和转换数据集
|
9
|
+
- 导出为标准格式
|
10
|
+
- 坐标转换等工具函数
|
11
|
+
|
12
|
+
基本用法:
|
13
|
+
>>> from dataset_toolkit import load_yolo_from_local, export_to_coco
|
14
|
+
>>> dataset = load_yolo_from_local("/path/to/dataset", {0: 'cat'})
|
15
|
+
>>> export_to_coco(dataset, "output.json")
|
16
|
+
"""
|
17
|
+
|
18
|
+
__version__ = "0.1.0"
|
19
|
+
__author__ = "Your Name"
|
20
|
+
__email__ = "your.email@example.com"
|
21
|
+
|
22
|
+
# 导入核心类和函数,提供简洁的顶层API
|
23
|
+
from dataset_toolkit.models import (
|
24
|
+
Dataset,
|
25
|
+
ImageAnnotation,
|
26
|
+
Annotation
|
27
|
+
)
|
28
|
+
|
29
|
+
from dataset_toolkit.loaders.local_loader import (
|
30
|
+
load_yolo_from_local
|
31
|
+
)
|
32
|
+
|
33
|
+
from dataset_toolkit.processors.merger import (
|
34
|
+
merge_datasets
|
35
|
+
)
|
36
|
+
|
37
|
+
from dataset_toolkit.exporters.coco_exporter import (
|
38
|
+
export_to_coco
|
39
|
+
)
|
40
|
+
|
41
|
+
from dataset_toolkit.exporters.txt_exporter import (
|
42
|
+
export_to_txt
|
43
|
+
)
|
44
|
+
|
45
|
+
from dataset_toolkit.utils.coords import (
|
46
|
+
yolo_to_absolute_bbox
|
47
|
+
)
|
48
|
+
|
49
|
+
from dataset_toolkit.pipeline import (
|
50
|
+
DatasetPipeline
|
51
|
+
)
|
52
|
+
|
53
|
+
# 定义公共API
|
54
|
+
__all__ = [
|
55
|
+
# 版本信息
|
56
|
+
"__version__",
|
57
|
+
|
58
|
+
# 数据模型
|
59
|
+
"Dataset",
|
60
|
+
"ImageAnnotation",
|
61
|
+
"Annotation",
|
62
|
+
|
63
|
+
# 加载器
|
64
|
+
"load_yolo_from_local",
|
65
|
+
|
66
|
+
# 处理器
|
67
|
+
"merge_datasets",
|
68
|
+
|
69
|
+
# 导出器
|
70
|
+
"export_to_coco",
|
71
|
+
"export_to_txt",
|
72
|
+
|
73
|
+
# 工具函数
|
74
|
+
"yolo_to_absolute_bbox",
|
75
|
+
|
76
|
+
# 管道API
|
77
|
+
"DatasetPipeline",
|
78
|
+
]
|
79
|
+
|
File without changes
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# dataset_toolkit/exporters/coco_exporter.py
|
2
|
+
import json
|
3
|
+
import datetime
|
4
|
+
from dataset_toolkit.models import Dataset
|
5
|
+
|
6
|
+
def export_to_coco(dataset: Dataset, output_path: str):
|
7
|
+
"""
|
8
|
+
将数据集对象导出为COCO JSON格式。
|
9
|
+
|
10
|
+
Args:
|
11
|
+
dataset (Dataset): 内部标准数据集对象。
|
12
|
+
output_path (str): .json文件的输出路径。
|
13
|
+
"""
|
14
|
+
coco_format = {
|
15
|
+
"info": {
|
16
|
+
"description": f"Exported from dataset_toolkit: {dataset.name}",
|
17
|
+
"date_created": datetime.datetime.utcnow().isoformat()
|
18
|
+
},
|
19
|
+
"licenses": [],
|
20
|
+
"images": [],
|
21
|
+
"annotations": [],
|
22
|
+
"categories": []
|
23
|
+
}
|
24
|
+
|
25
|
+
# 1. 填充 categories
|
26
|
+
for cat_id, cat_name in dataset.categories.items():
|
27
|
+
coco_format["categories"].append({
|
28
|
+
"id": cat_id,
|
29
|
+
"name": cat_name,
|
30
|
+
"supercategory": "none"
|
31
|
+
})
|
32
|
+
|
33
|
+
# 2. 遍历图片和标注,填充 images 和 annotations
|
34
|
+
annotation_id_counter = 1
|
35
|
+
for image_id_counter, image_ann in enumerate(dataset.images, 1):
|
36
|
+
# 添加 image 条目
|
37
|
+
coco_format["images"].append({
|
38
|
+
"id": image_id_counter,
|
39
|
+
"file_name": image_ann.image_id, # 使用原始文件名
|
40
|
+
"width": image_ann.width,
|
41
|
+
"height": image_ann.height
|
42
|
+
})
|
43
|
+
|
44
|
+
# 添加 annotation 条目
|
45
|
+
for ann in image_ann.annotations:
|
46
|
+
x_min, y_min, width, height = ann.bbox
|
47
|
+
area = width * height
|
48
|
+
|
49
|
+
coco_format["annotations"].append({
|
50
|
+
"id": annotation_id_counter,
|
51
|
+
"image_id": image_id_counter,
|
52
|
+
"category_id": ann.category_id,
|
53
|
+
"bbox": [round(c, 2) for c in ann.bbox],
|
54
|
+
"area": round(area, 2),
|
55
|
+
"iscrowd": 0,
|
56
|
+
"segmentation": [] # 对于bbox,segmentation通常为空
|
57
|
+
})
|
58
|
+
annotation_id_counter += 1
|
59
|
+
|
60
|
+
# 3. 写入JSON文件
|
61
|
+
with open(output_path, 'w') as f:
|
62
|
+
json.dump(coco_format, f, indent=4)
|
63
|
+
|
64
|
+
print(f"成功将数据集导出为COCO格式: {output_path}")
|