Skip to main content

Python Client Library of Annofab WebAPI (https://annofab.com/docs/api/)

Project description

annofab-api-python-client

Annofab Web API のPythonクライアントライブラリです。

Build Status CodeQL PyPI version Python Versions Documentation Status

注意

  • 作者または著作権者は、ソフトウェアに関してなんら責任を負いません。
  • 現在、APIは開発途上版です。予告なく互換性のない変更がある可能性をご了承ください。
  • put, post, delete系のメソッドを間違えて実行してしまわないよう、注意してください。特に「プロジェクト削除」や「アノテーション仕様更新」のAPIには十分注意してください。

廃止予定

現在ありません。

Features

cURLやPostmanなどよりも簡単にAnnofab Web APIにアクセスできます。

  • ログインを意識せずに、APIを利用できます。
  • アクセス過多などで失敗した場合は、リトライされます。
  • 「画像を入力データとして登録する」機能など、APIを組み合わせた機能も利用できます。

Requirements

  • Python 3.10+

Install

$ pip install annofabapi

https://pypi.org/project/annofabapi/

Usage

インスタンス生成

user_id, passwordをコンストラクタ引数に渡す場合

# APIアクセス用のインスタンスを生成
from annofabapi import build


user_id = "XXXXXX"
password = "YYYYYY"

service = build(user_id, password)

PersonalAccessTokenをコンストラクタ引数に渡す場合

# APIアクセス用のインスタンスを生成
from annofabapi import build


pat = "XXXXXX"

service = build(pat = pat)

.netrcに認証情報を記載する場合

.netrcファイルに、AnnofabのユーザIDとパスワードを記載します。

machine annofab.com
login annofab_user_id
password annofab_password
from annofabapi import build_from_netrc
service = build_from_netrc()

For Linux

  • パスは$HOME/.netrc
  • $ chmod 600 $HOME/.netrcでパーミッションを変更する

For Windows

  • パスは%USERPROFILE%\.netrc

環境変数に認証情報を設定する場合

  • IDとパスワードで認証する場合
    • 環境変数ANNOFAB_USER_IDANNOFAB_PASSWORDにユーザIDとパスワードを設定します。
  • パーソナルアクセストークンで認証する場合
    • 環境変数ANNOFAB_PATにトークンを設定します。
    • ANNOFAB_PATが設定されている場合、ANNOFAB_USER_IDANNOFAB_PASSWORDは無視されます。
from annofabapi import build_from_env
service = build_from_env()

.netrcまたは環境変数に認証情報を設定する場合

build() を実行すると、環境変数または .netrc ファイルから認証情報を読み込みます。

from annofabapi import build
service = build()

優先順位は以下の通りです。

  1. 環境変数
    1. ANNOFAB_PAT
    2. ANNOFAB_USER_ID及びANNOFAB_PASSWORD
  2. .netrc

service.apiのサンプルコード

project_id = "ZZZZZZ"
# `status`が`complete`のタスクを取得する
content, response = service.api.get_tasks(project_id, query_params={"status": "complete"})

print(type(content))
# <class 'dict'>
print(content)
# {'list': [{'project_id': 'ZZZZZZ', 'task_id': '20190317_2', 'phase': 'acceptance', ...

print(type(response))
# <class 'requests.models.Response'>
print(response.headers["Content-Type"])
# application/json

service.wrapperのサンプルコード

service.wrapperには、server.apiを組み合わせたメソッドが定義されています。

# `status`が`complete`のタスクすべてを取得する
tasks = service.wrapper.get_all_tasks(project_id, query_params={"status": "complete"})
print(type(tasks))
# <class 'list'>
print(tasks)
# [{'project_id': 'ZZZZZZ', 'task_id': '20190317_2', 'phase': 'acceptance', ...


# simpleアノテーションzipのダウンロード
service.wrapper.download_annotation_archive(project_id, 'output_dir')

# 画像ファイルを入力データとして登録する
service.wrapper.put_input_data_from_file(project_id, 'sample_input_data_id', f'sample.png')

アノテーションzipの読み込み

ダウンロードしたアノテーションzipを、JSONファイルごとに読み込みます。 zipファイルを展開したディレクトリも読み込み可能です。

import zipfile
from pathlib import Path
from annofabapi.parser import lazy_parse_simple_annotation_dir, lazy_parse_simple_annotation_zip, SimpleAnnotationZipParser, SimpleAnnotationDirParser, lazy_parse_simple_annotation_zip_by_task


# Simpleアノテーションzipの読み込み
iter_parser = lazy_parse_simple_annotation_zip(Path("simple-annotation.zip"))
for parser in iter_parser:
    simple_annotation = parser.parse()
    print(simple_annotation)

# Simpleアノテーションzipを展開したディレクトリの読み込み
iter_parser = lazy_parse_simple_annotation_dir(Path("simple-annotation-dir"))
for parser in iter_parser:
    simple_annotation = parser.parse()
    print(simple_annotation)

# Simpleアノテーションzipをタスク単位で読み込む
task_iter_parser = lazy_parse_simple_annotation_zip_by_task(Path("simple-annotation.zip"))
for task_parser in task_iter_parser:
    print(task_parser.task_id)
    for parser in task_parser.lazy_parse():
        simple_annotation = parser.parse()
        print(simple_annotation)
        

# Simpleアノテーションzip内の1個のJSONファイルを読み込み
with zipfile.ZipFile('simple-annotation.zip', 'r') as zip_file:
    parser = SimpleAnnotationZipParser(zip_file, "task01/12345678-abcd-1234-abcd-1234abcd5678.json")
    simple_annotation = parser.parse()
    print(simple_annotation)

# Simpleアノテーションzip内を展開したディレクトリ内の1個のJSONファイルを読み込み
parser = SimpleAnnotationDirParser(Path("task01/12345678-abcd-1234-abcd-1234abcd5678.json"))
simple_annotation = parser.parse()
print(simple_annotation)

塗りつぶし画像の読み込み

annofabapi.segmentation には、アノテーションZIPに格納されている塗りつぶし画像を扱うための関数が用意されています。 利用する場合は、以下のコマンドを実行してください。

$ pip install annofabapi[segmentation]

DataClass

annofabapi.dataclassに、データ構造用のクラスがあります。 これらのクラスを利用すれば、属性で各値にアクセスできます。

from annofabapi.dataclass.task import Task
dict_task, _ = service.api.get_task(project_id, task_id)
task = Task.from_dict(dict_task)

print(task.task_id)
print(task.status)

備考

annofabapiのログを出力する方法(サンプル)

import logging
logging_formatter = '%(levelname)-8s : %(asctime)s : %(name)s : %(message)s'
logging.basicConfig(format=logging_formatter)
logging.getLogger("annofabapi").setLevel(level=logging.DEBUG)

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

annofabapi-1.7.1.tar.gz (531.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

annofabapi-1.7.1-py3-none-any.whl (805.3 kB view details)

Uploaded Python 3

File details

Details for the file annofabapi-1.7.1.tar.gz.

File metadata

  • Download URL: annofabapi-1.7.1.tar.gz
  • Upload date:
  • Size: 531.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.14

File hashes

Hashes for annofabapi-1.7.1.tar.gz
Algorithm Hash digest
SHA256 b0259d253941bf9232762b4c24748d8e1f2b5ccdb3fab730e8ecf29760972446
MD5 198824bf092249c3aec4a19d3a4a573f
BLAKE2b-256 870bb3d2c6eea1d68e4f55a878d4294f56cddda141eb3d77d1ccac3b86e6616d

See more details on using hashes here.

File details

Details for the file annofabapi-1.7.1-py3-none-any.whl.

File metadata

  • Download URL: annofabapi-1.7.1-py3-none-any.whl
  • Upload date:
  • Size: 805.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.14

File hashes

Hashes for annofabapi-1.7.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dad1d91a56235ec75b21a8e5d0f4d75e87bc6a725040c236072b51b984825cf4
MD5 f91e181c1301f30a4fef6688cdfe8718
BLAKE2b-256 f87d064712ff75bdca11b25797b0ced3a8a103e8405a4844171b5296bc79eb68

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page