Skip to main content

kotoha

kotoha 是一个面向计算机视觉工程的 Python 工具库,提供统一的几何类型、标注结果类型、文件读写、图像可视化和评估工具。它适合用在数据处理脚本、标注转换、模型推理后处理,以及日常的视觉算法开发中。

核心能力

  • 统一的二维几何类型:Rect、Polygon、KeyPoint、Line、Circle、Affine2D
  • 统一的视觉任务结果:Classification、Detection、SegmentationDetection、PoseDetection、ObbDetection、SemanticSegmentation
  • 常用几何计算:面积、旋转、点与线段关系、线段相交、点在多边形内
  • 工程向 I/O 工具:JSON、YAML、CSV、TXT、Pickle、XML、YOLO 标注读写
  • OpenCV 图像与视频工具:imread、imwrite、draw_bbox、draw_keypoints、draw_masks、VideoReader
  • 评估能力:分类、检测、姿态、重识别和文本任务的常用指标
  • 可选的 ONNX 模型适配:内置 Ultralytics YOLO 系列推理封装

安装

需要 Python 3.12 或更高版本。

安装基础功能:

pip install kotoha

安装 ONNX 模型相关功能:

pip install "kotoha[model]"

如果你在本地参与开发,可以安装完整依赖:

uv sync --all-extras --dev

快速开始

1. 使用几何类型描述区域

from kotoha import Rect

box = Rect.from_xyxy(10, 20, 110, 80)
polygon = box.to_polygon()
rotated = polygon.rotate_deg(30, pivot=(60, 50))

print(box.to_list())
print(rotated.to_list())

使用带离群点的关键点对估计仿射变换:

from kotoha import Affine2D

affine = Affine2D.from_points(
    source_points,
    target_points,
    ransac_reproj_threshold=2.0,
)
aligned_points = affine.transform_points(source_points)

默认估计器是 RANSAC;也可以选择 method="lmeds" 或在确认没有离群点时使用 method="least_squares"。完整示例见 examples/affine2d_ransac.py。

2. 使用统一的检测结果结构

from kotoha import Detection, Rect

detection = Detection(
    box=Rect.from_xyxy(10, 20, 110, 180),
    score=0.95,
    class_id=0,
    label="person",
)

print(detection.to_dict())
print(detection.to_numpy())

3. 读取图像并绘制检测框

from kotoha import draw_bbox, imread, imwrite

image = imread("input.jpg")
if image is None:
    raise RuntimeError("failed to read input.jpg")

draw_bbox(
    image,
    box=[10, 20, 110, 180],
    score=0.95,
    obj_id="person",
)
imwrite("result.jpg", image)

4. 读写标注和配置文件

from kotoha import read_json, read_yaml, save_json

dataset = read_json("annotations.json")
config = read_yaml("config.yaml")

save_json("preview.json", dataset)
print(type(config))

5. 运行 YOLO ONNX 推理

安装 model 扩展后,可以直接加载导出的 ONNX 模型:

from kotoha.model.ultralytics import YOLOv8
from kotoha import imread, imwrite

model = YOLOv8("yolov8n.onnx")
image = imread("input.jpg")
if image is None:
    raise RuntimeError("failed to read input.jpg")

detections = model.detect(image, conf_thres=0.4)
rendered = model.draw_bbox(image, detections)
imwrite("result.jpg", rendered)

当前提供的适配器包括:

  • YOLOv5
  • YOLOv8
  • YOLOv8Cls
  • YOLOv8Seg
  • YOLOv8Pose
  • YOLOv8Obb
  • YOLO26Sem

常用模块

模块 说明
kotoha 顶层导出,适合直接导入常用类型和工具函数
kotoha.annotation 分类、检测、分割、姿态、旋转框等结果类型
kotoha.geo 几何计算、坐标变换、栅格和多边形处理
kotoha.io 文件读写、标注格式转换、文件枚举
kotoha.visual 图像读写、结果绘制、颜色工具、视频读取
kotoha.eval 分类、检测、姿态、重识别、文本评估指标
kotoha.model.ultralytics YOLO ONNX 推理适配
kotoha.image_filter 感知哈希与图像去重过滤
kotoha.experiments 一些实验性或任务型工具

顶层导入示例

顶层 kotoha 已经导出了大部分常用接口,适合在业务脚本中直接使用:

from kotoha import Detection, Rect, draw_bbox, imread, polygon_area, read_json

如果你希望按功能拆分导入,也可以使用对应子模块:

from kotoha.geo import polygon_area, rotate_points
from kotoha.io import read_json, save_yaml
from kotoha.visual import draw_bbox, imread

开发与验证

运行测试:

uv run pytest

运行代码检查:

uv run ruff check .
uv run ruff format --check .

License

项目许可证见 license 文件。

Metadata

Release files for kotoha 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kotoha 0.2.1
File Size Uploaded Provenance Actions
kotoha-0.2.1.tar.gz 135.4 kB Without provenance Details for kotoha-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for kotoha 0.2.1
File Interpreter ABI Platform Actions
kotoha-0.2.1-py3-none-any.whl Python 3 none any Details for kotoha-0.2.1-py3-none-any.whl

Total release size: 301.1 kB

Release files / kotoha-0.2.1.tar.gz

Download URL kotoha-0.2.1.tar.gz
Size 135.4 kB
Tags Source
SHA-256 checksum
How to use checksums
005254c4c924520ad42e395e7dd32f1b432c67d8778bda0bc36e153924b8cee2
BLAKE2b-256 checksum
How to use checksums
9f6bbaa515e31d721537eba9ed005af238c0d5deeff4270ec72a452ffa233506
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Release files / kotoha-0.2.1-py3-none-any.whl

Download URL kotoha-0.2.1-py3-none-any.whl
Size 165.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0f4741da56780694fb75f69f7324b14126d6ac657ac7e28d4f7592cd01976f1e
BLAKE2b-256 checksum
How to use checksums
48edb559961d91be2cf703316ad3748e0d79ce3c3e60b772de7ba19c11b9daf8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.14

Security for kotoha 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page