Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:APIJSON APIJSON Demo DatasetUtil

From Leeroopedia
Knowledge Sources
Domains Computer_Vision, Dataset, Data_Pipeline
Last Updated 2026-10-14 00:00 GMT

Overview

Concrete tool for generating COCO-format computer vision datasets from JSON data for tasks including detection, segmentation, pose estimation, and OCR provided by the APIJSON Demo server.

Description

The DatasetUtil class is a standalone utility (1136 lines) that transforms APIJSON query results into COCO-format datasets suitable for training computer vision models. It supports seven task types and provides a fluent DatasetBuilder for constructing datasets programmatically.

Key features:

  • TaskType enum: CLASSIFICATION, DETECTION, SEGMENTATION, POSE_KEYPOINTS, FACE_KEYPOINTS, ROTATED_DETECTION, OCR.
  • DatasetBuilder: Fluent builder with withInfo(), withCategory(), withKeypointCategory(), addImage(), addAnnotation(), build().
  • Annotation types: DetectionAnnotation (bbox, segmentation), KeypointAnnotation (keypoints, num_keypoints), OcrAnnotation (attributes with transcription).
  • Image sources: Supports URL, base64, and local file path image sources.
  • Directory creation: createCocoDirectoryStructure() creates annotations/, images/, and optionally labels/ (for OCR/OBB).
  • Validation: validateCocoType() and isValidCocoType() check type strings against allowed values.
  • JSON data generation: generate(data, tasks, outputDir, part) builds datasets from List<JSONObject> data (from APIJSON queries).

Usage

  • Programmatic dataset generation: Use the generate(outputDir, tasks) static method with sample data.
  • From APIJSON query results: Use generate(data, tasks, outputDir, part) with List<JSONObject> from APIJSON queries.
  • Custom dataset building: Use DatasetBuilder directly for fine-grained control.

Code Reference

Source Location

  • Repository: APIJSON-Demo
  • File: APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/DatasetUtil.java

Signature

public class DatasetUtil {

    // Task type enum
    public static enum TaskType {
        CLASSIFICATION("cls"), DETECTION("det"), SEGMENTATION("seg"),
        POSE_KEYPOINTS("pose"), FACE_KEYPOINTS("face"),
        ROTATED_DETECTION("obb"), OCR("ocr");
        public String getType();
        public static List<String> getTypes();
    }

    // Validation
    public static void validateCocoType(String type);
    public static boolean isValidCocoType(String type);

    // Directory structure
    public static void createCocoDirectoryStructure(String baseDir, String type) throws IOException;

    // Dataset generation
    public static void generate(String outputDir, Set<TaskType> tasks) throws IOException;
    public static void generate(List<JSONObject> data, Set<TaskType> tasks,
        String outputDir, String part) throws IOException;

    // File output
    public static void writeToFile(CocoDataset dataset, String outputPath) throws IOException;

    // Dataset builder
    public static class DatasetBuilder {
        public DatasetBuilder withInfo(String description, String version, String year);
        public DatasetBuilder withCategory(int id, String name, String supercategory);
        public DatasetBuilder withKeypointCategory(int id, String name, String supercategory,
            List<String> keypoints, List<List<Integer>> skeleton);
        public DatasetBuilder addImage(String fileName, int width, int height, String urlOrBase64);
        public DatasetBuilder addAnnotation(DetectionAnnotation ann);
        public DatasetBuilder addAnnotation(KeypointAnnotation ann);
        public DatasetBuilder addAnnotation(OcrAnnotation ann);
        public CocoDataset build();
    }
}

Import

import apijson.DatasetUtil;
import apijson.DatasetUtil.TaskType;
import apijson.DatasetUtil.DatasetBuilder;

I/O Contract

Inputs

Name Type Required Description
outputDir String Yes Output directory path
tasks Set<TaskType> Yes Task types to generate
data List<JSONObject> No APIJSON query results with image/annotation data
part String No Train/val split name

Outputs

Name Type Description
COCO JSON file File Written to {outputDir}/annotations/instances_{taskName}.json
Image files Files Copied to {outputDir}/images/

Usage Examples

Generate Detection Dataset

Set<TaskType> tasks = new HashSet<>(Collections.singletonList(TaskType.DETECTION));
DatasetUtil.generate("./output/detection_dataset", tasks);
// Creates:
//   ./output/detection_dataset/annotations/instances_det.json
//   ./output/detection_dataset/images/

Generate from APIJSON Query Results

// Query data via APIJSON
List<JSONObject> data = ...; // From APIJSON query with Random and TestRecord keys

Set<TaskType> tasks = new HashSet<>(Collections.singletonList(TaskType.DETECTION));
DatasetUtil.generate(data, tasks, "./output/dataset/", "train");
// Parses bboxes from TestRecord response JSON, generates COCO annotations

Custom Dataset Builder

DatasetBuilder builder = new DatasetBuilder()
    .withInfo("My Dataset", "1.0", "2025")
    .withCategory(1, "person", "person")
    .withCategory(2, "car", "vehicle")
    .withKeypointCategory(1, "person", "person",
        Arrays.asList("nose", "left_eye", "right_eye"),
        Arrays.asList(Arrays.asList(1, 2), Arrays.asList(1, 3))
    )
    .addImage("00001.jpg", 640, 480, "http://example.com/img.jpg");

DetectionAnnotation ann = new DetectionAnnotation();
ann.setImage_id(1);
ann.setCategory_id(1);
ann.setBbox(Arrays.asList(100.0, 50.0, 80.0, 120.0));
ann.setArea(80.0 * 120.0);
builder.addAnnotation(ann);

CocoDataset dataset = builder.build();
DatasetUtil.writeToFile(dataset, "output/annotations/instances_det.json");

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment