LoginSignup
5
3

More than 1 year has passed since last update.

FastLabel SDKの使い方(アノテーションがエクスポートできるようになる)

Last updated at Posted at 2023-02-03

FastLabel SDKを使って、FastLabelからアノテーション情報をエクスポートをする方法を紹介をします。

※ 実行環境 Google Colaboratory

アプリを追加する

  1. Google Driveを開き、Colabフォルダを作る。(Colabフォルダでなくとも、任意のフォルダでも良い)
    image.png
  2. 右クリックをして「その他」の欄をホバーする。「アプリの追加」を選択し「Colaboratory」を検索して、インストールする。

image.png

SDKを用いて、アノテーションをエクスポートする

  1. Google Driveで右クリックをしてGoogle Colaboratoryを選択する。(新しいGoogle Colaboratoryのフォルダが作成される。)
    image.png
  2. 以下のコードを入力して実行する。実行方法は右の三角の再生ボタンみたいなのを押す
    (ERRORが出るが一旦無視で良い)
!pip install fastlabel

FastLabelのSDKをインストールしている
image.png
3. 以下のコードをコピーして貼り付ける。アクセストークンとプロジェクトのスラッグを設定する


# $$$$$$$$$$$$$$$$$各自で設定する部分$$$$$$$$$$$$$$$$$
ACCESS_TOKEN="XXXXXXXXXXXXXXXXX" # 環境変数の設定
# アクセストークンのとってき方はこちら
# https://docs.fastlabel.ai/docs/api

PROJECT_SLUG="XXXXXXXXXXX"  # プロジェクトのスラグ
TAGS=[]  # タグ


# ここから下はいじらない
####################################################################
####################################################################

import json
import fastlabel
import os
import time
import urllib.request
from multiprocessing import Pool, cpu_count
from google.colab import drive
import datetime
drive.mount('/content/drive')


os.environ['FASTLABEL_ACCESS_TOKEN'] = ACCESS_TOKEN
client = fastlabel.Client()

t_delta = datetime.timedelta(hours=9)
JST = datetime.timezone(t_delta, 'JST')
now = datetime.datetime.now(JST)

def get_all_tasks() -> list:
    # Iterate pages until new tasks are empty.
    all_tasks = []
    offset = None
    while True:
        time.sleep(1)
        tasks = client.get_image_tasks(
            project=PROJECT_SLUG,
            limit=1000,
            offset=offset,
            tags=TAGS
        )
        all_tasks.extend(tasks)

        if len(tasks) > 0:
            offset = len(all_tasks)  # Set the offset
            print(offset)
        else:
            break

    return all_tasks

if __name__ == '__main__':
    tasks = get_all_tasks()
    output_path=os.path.join('/content/drive/My Drive',"annotations",PROJECT_SLUG,now.strftime('%Y%m%d%H%M'),"annotations.json")
    os.makedirs(os.path.dirname(output_path),exist_ok=True)
    with open(output_path, mode='wt', encoding='utf-8') as file:
      json.dump(tasks, file, ensure_ascii=False, indent=2)
    print("タスク数: ",len(tasks))
    print("保存パス :",output_path)
    print("######### finish #########")


4, 実行する

「Google Drive for desktop」がGoogleアカウントへのアクセスをリクエストしています

という画面が出るので許可をしていく。
以下の画面が出れば、実行が成功をしている。
image.png

アノテーションファイルが保存されている場所は、マイドライブ/annotations/プロジェクトのスラグ/日時/annotations.jsonに保存されている。

image.png

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3