annofab-api-python-client
AnnoFab Web APIのPythonクライアントライブラリです。
- AnnoFab Web API Documentation: https://annofab.com/docs/api/
- Reference Documentation: https://annofab-api-python-client.readthedocs.io/en/latest/
- annofab-cli https://github.com/kurusugawa-computer/annofab-cli
- 「タスクの一括差し戻し」や、「プロジェクト間の差分表示」など、AnnoFabの画面で実施するには時間がかかる操作を、CLIツールとして提供しています。
- 開発者用ドキュメント: https://github.com/kurusugawa-computer/annofab-api-python-client/blob/master/README_for_developer.md
注意
- 作者または著作権者は、ソフトウェアに関してなんら責任を負いません。
- 現在、APIは開発途上版です。予告なく互換性のない変更がある可能性をご了承ください。
- put, post, delete系のメソッドを間違えて実行してしまわないよう、注意してください。特に「プロジェクト削除」や「アノテーション仕様更新」のAPIには十分注意してください。
廃止予定
wraapper.py get_annotation_specs_from_url- 廃止理由:過去のアノテーション仕様を
get_annotation_specsメソッドから取得できるようになったため - 廃止予定日:2019/11/01以降
- 廃止理由:過去のアノテーション仕様を
Features
cURLやPostmanなどよりも簡単にAnnoFab Web APIにアクセスできます。
- ログインを意識せずに、APIを利用できます。
- アクセス過多などで失敗した場合は、リトライされます。
- 「画像を入力データとして登録する」機能など、APIを組み合わせた機能も利用できます。
Requirements
- Python 3.6+
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)
.netrcに記載されたuser_id, passwordから生成
.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
service.apiのサンプルコード
service.apiには、Web APIに対応するメソッドが定義されています。- メソッド名は、AnnoFab Web APIのOpenAPI specificationに記載されている
operationIdを、スネークケースに変換したものです。 - 各メソッドの戻り値の型は
Tupple[Content, Response]です。 ResponseはrequestsモジュールのReponseオブジェクトです。 ContentはReponseの中身です。
project_id = "ZZZZZZ"
# `status`が`complete`のタスクを取得する
content, response = service.api.get_tasks(project_id, query_params={'status': 'complete'})
print(content)
# {'list': [{'project_id': ...
# simpleアノテーションzipのダウンロード用URLを取得する
content, response = service.api.get_annotation_archive(project_id)
url = response.headers['Location']
service.wrapperのサンプルコード
service.wrapperには、server.apiを組み合わせたメソッドが定義されています。
# `status`が`complete`のタスクすべてを取得する
tasks = service.wrapper.get_all_tasks(project_id, query_params={'status': 'complete'})
print(tasks)
# [{'project_id': ...
# 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')
src_project_id = "AAAAAA"
dest_project_id = "BBBBBB"
# プロジェクトメンバをコピー(誤って実行しないように注意すること)
service.wrapper.copy_project_members(src_project_id, dest_project_id)
# アノテーション仕様のコピー(誤って実行しないように注意すること)
service.wrapper.copy_annotation_specs(src_project_id, dest_project_id)
アノテーション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
# 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内の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)
DataClass
annofabapi.dataclassに、データ構造用のクラスがあります。
これらのクラスを利用すれば、属性で各値にアクセスできます。
from annofabapi.dataclass.task import Task, TaskHistory
dict_task, _ = service.api.get_task(project_id, task_id)
task = Task.from_dict(dict_task)
print(task.task_id)
print(task.task_status)
備考
annofabapiのログを出力する方法(サンプル)
import logging
logging_formatter = '%(levelname)-8s : %(asctime)s : %(filename)s : %(name)s : %(funcName)s : %(message)s'
logging.basicConfig(format=logging_formatter)
logging.getLogger("annofabapi").setLevel(level=logging.DEBUG)
Metadata
Release files for annofabapi 0.22.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| annofabapi-0.22.0.tar.gz | 60.5 kB | Without provenance | Details for annofabapi-0.22.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| annofabapi-0.22.0-py3-none-any.whl | Python 3 | none | any | Details for annofabapi-0.22.0-py3-none-any.whl |
Total release size: 135.6 kB
Release files / annofabapi-0.22.0.tar.gz
| Download URL | annofabapi-0.22.0.tar.gz |
|---|---|
| Size | 60.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fd55412978c387b824407d997333fe0dc02155c70dc3f351a842255c72300c2b
|
|
BLAKE2b-256 checksum How to use checksums |
8b17f1f0c10e66d79e9e5916cc6ff999cd39c8a84a5bc4c611d4b2715e5600fc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4
|
Release files / annofabapi-0.22.0-py3-none-any.whl
| Download URL | annofabapi-0.22.0-py3-none-any.whl |
|---|---|
| Size | 75.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ea976a988b85b1e779f357b3052cdaa99ab93e7a50819ccb63be025d191de2a1
|
|
BLAKE2b-256 checksum How to use checksums |
fecbb8ccd2c57c8dbba991313114d767af96de722cad6feb0561ef8d67e57938
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4
|
Security for annofabapi 0.22.0
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.