@@ -456,25 +456,29 @@ public ResponseEntity<Object> downloadCVReport(@PathVariable(name = "id") String
456456public ResponseEntity <Object > downloadDataset (
457457@ PathVariable ("id" ) String idStr ,
458458@ RequestParam (name = "type" , required = false ) String type ,
459- @ RequestParam (name = "datasetName" , required = false ) String datasetName
459+ @ RequestParam (name = "ratio" , required = false ) String ratioStr ,
460+ @ RequestParam (name = "name" , required = false ) String datasetName
460461) throws IOException {
461462try {
462- // 参数验证
463+ long repOrDocId = Long .parseLong (idStr );
464+ if (repOrDocId <= 0 ) {
465+ throw new IllegalArgumentException ("id 必须为 > 0 的 reportId 或 documentId 有效整数!" );
466+ }
467+
463468if (StringUtil .isNotEmpty (type )) {
464469validateCocoType (type );
465470} else {
466471type = "" ;
467472}
468473
469- String dataset = StringUtil .isEmpty (datasetName ) ? type + "_dataset" : datasetName ;
470- String exportDir = fileUploadRootDir + dataset + "/" ;
471-
472- long repOrDocId = Long .parseLong (idStr );
473- if (repOrDocId <= 0 ) {
474- throw new IllegalArgumentException ("id 必须为 > 0 的 reportId 或 documentId 有效整数!" );
474+ int ratio = StringUtil .isEmpty (ratioStr ) ? 20 : Integer .parseInt (ratioStr );
475+ if (ratio < 0 || ratio > 100 ) {
476+ throw new IllegalArgumentException ("测试集比例 ratio 必须为 0 ~ 100 范围内的有效整数!" );
475477}
476478
477- String name = "CVAuto_" + dataset + repOrDocId + ".zip" ;
479+ String dataset = StringUtil .isNotEmpty (datasetName ) ? datasetName : "CVAuto_" + (StringUtil .isNotEmpty (type ) ? type + "_" : "" ) + "dataset_" + repOrDocId ;
480+ String exportDir = fileUploadRootDir + dataset + "/" ;
481+ String name = dataset + ".zip" ;
478482String path = fileUploadRootDir + name ;
479483
480484File file = new File (path );
@@ -499,8 +503,8 @@ public ResponseEntity<Object> downloadDataset(
499503
500504{ // [] <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
501505JSONObject item = new JSONObject ();
502- // item.put("count", 0);
503- item .put ("count" , 3 );
506+ item .put ("count" , 0 );
507+ // item.put("count", 3);
504508item .put ("join" , "@/TestRecord" );
505509
506510{ // Random <<<<<<<<<<<<<<<<<<<<<<<<<<<<<
@@ -545,17 +549,29 @@ public ResponseEntity<Object> downloadDataset(
545549//long documentId = lastTr == null ? 0 : lastTr.getLongValue("documentId");
546550//long randomId = lastTr == null ? 0 : lastTr.getLongValue("randomId");
547551if (reportId != repOrDocId ) {
548- name = "CVAuto_" + dataset + (reportId > 0 ? reportId : repOrDocId + "_last" ) + ".zip" ;
552+ dataset = StringUtil .isNotEmpty (datasetName ) ? datasetName : "CVAuto_" + (StringUtil .isNotEmpty (type ) ? type + "_" : "" ) + "dataset_" + (reportId > 0 ? reportId : repOrDocId + "_last" );
553+ exportDir = fileUploadRootDir + dataset + "/" ;
554+ name = dataset + ".zip" ;
549555path = fileUploadRootDir + name ;
550556}
551557
552558JSONArray array = response .getJSONArray ("[]" );
553559
554- List <JSONObject > list = new ArrayList <>();
560+ List <JSONObject > trainList = new ArrayList <>();
561+ List <JSONObject > validList = new ArrayList <>();
555562if (array != null ) {
556- for (int i = 0 ; i < array .size (); i ++) {
563+ int len = array .size ();
564+ for (int i = 0 ; i < len ; i ++) {
557565JSONObject item = array .getJSONObject (i );
558- list .add (item );
566+ if (item == null || item .isEmpty ()) {
567+ continue ;
568+ }
569+
570+ if (ratio <= 0 || ratio <= 100 - 100.0 *i /len ) {
571+ trainList .add (item );
572+ } else {
573+ validList .add (item );
574+ }
559575
560576//JSONObject random = item == null ? null : item.getJSONObject("Random");
561577//JSONObject testRecord = item == null ? null : item.getJSONObject("TestRecord");
@@ -586,7 +602,9 @@ public ResponseEntity<Object> downloadDataset(
586602//generateCocoDatasetFromApiJson(exportDir, type, dataset, list);
587603
588604Set <DatasetUtil .TaskType > detectionTasks = new HashSet <>(Collections .singletonList (DatasetUtil .TaskType .DETECTION ));
589- DatasetUtil .generate (exportDir , detectionTasks , list );
605+
606+ DatasetUtil .generate (trainList , detectionTasks , exportDir , "train" );
607+ DatasetUtil .generate (validList , detectionTasks , exportDir , "val" );
590608
591609createZipFromDirectory (exportDir , path );
592610
0 commit comments