Skip to content

Commit 59522c8

Browse files
committed
Java: MultiDataSource 导出 CVAuto 数据集用实际的 label 作为 category
1 parent 77eb75c commit 59522c8

2 files changed

Lines changed: 52 additions & 19 deletions

File tree

‎APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/DatasetUtil.java‎

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,24 @@ public static void createCocoDirectoryStructure(String baseDir, String type) thr
9898
Files.createDirectories(Paths.get(baseDir + "annotations"));
9999
Files.createDirectories(Paths.get(baseDir + "images"));
100100

101+
Files.createDirectories(Paths.get(baseDir + "train"));
102+
Files.createDirectories(Paths.get(baseDir + "val"));
103+
Files.createDirectories(Paths.get(baseDir + "test"));
104+
105+
Files.createDirectories(Paths.get(baseDir, "annotations", "train"));
106+
Files.createDirectories(Paths.get(baseDir, "annotations", "val"));
107+
Files.createDirectories(Paths.get(baseDir, "annotations", "test"));
108+
109+
Files.createDirectories(Paths.get(baseDir, "images", "train"));
110+
Files.createDirectories(Paths.get(baseDir, "images", "val"));
111+
Files.createDirectories(Paths.get(baseDir, "images", "test"));
112+
101113
// 根据类型创建特定目录 detection, classification, segmentation, keypoints, face_keypoints 使用标准结构
102114
if (TaskType.OCR.getType().equals(type) || TaskType.ROTATED_DETECTION.getType().equals(type)) {
103-
Files.createDirectories(Paths.get(baseDir + "labels"));
115+
Files.createDirectories(Paths.get(baseDir, "labels"));
116+
Files.createDirectories(Paths.get(baseDir, "labels", "train"));
117+
Files.createDirectories(Paths.get(baseDir, "labels", "val"));
118+
Files.createDirectories(Paths.get(baseDir, "labels", "test"));
104119
}
105120
}
106121

@@ -526,14 +541,32 @@ public static void generate(List<JSONObject> data, Set<TaskType> tasks, String o
526541

527542
// --- 1. 初始化构建器和通用信息 ---
528543
DatasetBuilder builder = new DatasetBuilder()
529-
.withInfo("Dataset from JSONObject", "1.0", "2025")
530-
.withCategory(1, "person", "person")
531-
.withCategory(2, "car", "vehicle")
532-
.withCategory(3, "dog", "animal")
533-
.withKeypointCategory(1, "person", "person",
534-
Arrays.asList("nose", "left_eye", "right_eye"),
535-
Arrays.asList(Arrays.asList(1, 2), Arrays.asList(1, 3))
536-
);
544+
.withInfo("Dataset from JSONObject", "1.0", "2025");
545+
546+
// 从数据中提取实际的categories
547+
JSONArray extractedCategories = extractCategoriesFromApiJson(data);
548+
549+
// 动态添加categories到builder
550+
if (extractedCategories != null) {
551+
boolean hasPose = tasks.contains(TaskType.POSE_KEYPOINTS);
552+
553+
for (int i = 0; i < extractedCategories.size(); i++) {
554+
JSONObject categoryObj = extractedCategories.getJSONObject(i);
555+
int id = categoryObj.getIntValue("id");
556+
String name = categoryObj.getString("name");
557+
String supercategory = categoryObj.getString("supercategory");
558+
559+
// 如果是关键点任务且包含person类别,添加关键点信息
560+
if (hasPose && ("person".equals(name) || name.contains("人"))) {
561+
builder.withKeypointCategory(id, name, supercategory,
562+
Arrays.asList("nose", "left_eye", "right_eye"),
563+
Arrays.asList(Arrays.asList(1, 2), Arrays.asList(1, 3))
564+
);
565+
} else {
566+
builder.withCategory(id, name, supercategory);
567+
}
568+
}
569+
}
537570

538571
// 用于跟踪图片ID映射
539572
Map<String, Integer> imgNameIdMap = new HashMap<>();

‎APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/boot/FileController.java‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,8 @@ public ResponseEntity<Object> downloadCVReport(@PathVariable(name = "id") String
246246

247247
{ // [] <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
248248
JSONObject item = new JSONObject();
249-
item.put("count", 0);
250-
//item.put("count", 3);
251-
item.put("join", "@/TestRecord");
252-
//item.put("join", "&/TestRecord");
249+
item.put("count", Log.DEBUG ? 10 : 0);
250+
item.put("join", Log.DEBUG ? "&/TestRecord" : "@/TestRecord");
253251

254252
{ // Random <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
255253
JSONObject random = new JSONObject();
@@ -476,8 +474,9 @@ public ResponseEntity<Object> downloadDataset(
476474
throw new IllegalArgumentException("测试集比例 ratio 必须为 0 ~ 100 范围内的有效整数!");
477475
}
478476

479-
String dataset = StringUtil.isNotEmpty(datasetName) ? datasetName : "CVAuto_" + (StringUtil.isNotEmpty(type) ? type + "_" : "") + "dataset_" + repOrDocId;
480-
String exportDir = fileUploadRootDir + dataset + "/";
477+
String dataset = StringUtil.isNotEmpty(datasetName) ? datasetName
478+
: "CVAuto_" + (StringUtil.isNotEmpty(type) ? type + "_" : "") + "dataset_" + repOrDocId;
479+
String exportDir = fileUploadRootDir + dataset + File.separator;
481480
String name = dataset + ".zip";
482481
String path = fileUploadRootDir + name;
483482

@@ -503,9 +502,8 @@ public ResponseEntity<Object> downloadDataset(
503502

504503
{ // [] <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
505504
JSONObject item = new JSONObject();
506-
item.put("count", 0);
507-
//item.put("count", 3);
508-
item.put("join", "@/TestRecord");
505+
item.put("count", Log.DEBUG ? 10 : 0);
506+
item.put("join", Log.DEBUG ? "&/TestRecord" : "@/TestRecord");
509507

510508
{ // Random <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
511509
JSONObject random = new JSONObject();
@@ -549,7 +547,9 @@ public ResponseEntity<Object> downloadDataset(
549547
//long documentId = lastTr == null ? 0 : lastTr.getLongValue("documentId");
550548
//long randomId = lastTr == null ? 0 : lastTr.getLongValue("randomId");
551549
if (reportId != repOrDocId) {
552-
dataset = StringUtil.isNotEmpty(datasetName) ? datasetName : "CVAuto_" + (StringUtil.isNotEmpty(type) ? type + "_" : "") + "dataset_" + (reportId > 0 ? reportId : repOrDocId + "_last");
550+
dataset = StringUtil.isNotEmpty(datasetName) ? datasetName : "CVAuto_"
551+
+ (StringUtil.isNotEmpty(type) ? type + "_" : "") + "dataset_"
552+
+ (reportId > 0 ? reportId : repOrDocId + "_last");
553553
exportDir = fileUploadRootDir + dataset + "/";
554554
name = dataset + ".zip";
555555
path = fileUploadRootDir + name;

0 commit comments

Comments
 (0)