Skip to content

Conversation

@fuji44
Copy link
Contributor

@fuji44 fuji44 commented Mar 6, 2023

see: https://github.com/fastlabel/fastlabel-application/issues/3475

補足

WebのExportの動作と極力合わせていますが、一部異なります。
問題がありそうなら指摘をお願いします。特に1。

  1. Webではアノテーションがないタスクでもアノテーションファイルを出力していますが、SDKではアノテーションがないタスクのアノテーションファイルは出力しません。
    1. やはり問題ありそうだったので、修正しました。
  2. Webでは出力対象アノテーションを指定した場合、対象アノテーションを含む動画タスクのフレーム別画像のみ出力されますが、SDKではすべての動画タスクのフレーム別画像が出力されます。
  3. 同一フレームに同一アノテーションクラスで複数のアノテーションが指定されている場合、エクスポートしたアノテーションファイル内の記述順序が同一でない場合があります。ソートを指定して極力同一となるように対処していますが、完全に制御するにはAPI側とも合わせた修正が必要です。

また、いくつか既存のバグを発見し影響があるものは修正しましたが、以下は大きな影響がないため ueno さんと相談してコメントを残すのみとしています。

  • アノテーションクラスが2つ以上あるプロジェクトをアノテーションを指定せずにCOCO形式でエクスポートしたとき、 categoriescolor がすべて同じになります。

テスト

WebとSDKのぞれぞれで、動画タスクをCOCO、YOLO、PascalVOCでエクスポートし、Diffツールで差分を確認しました。

@fuji44 fuji44 self-assigned this Mar 6, 2023
@fuji44 fuji44 requested a review from ueno-k-work March 6, 2023 16:00

def export_coco(
self,
project: str,
Copy link
Contributor Author

@fuji44 fuji44 Mar 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

タスクを動画と画像のいずれで処理すべきかを判断するために、プロジェクトタイプを用いることにしました。

そのため、SDK利用者に新たにプロジェクトスラグを指定してもらうようにしました。

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちら今後project_slugが必須になると思うので、READMEの方もそれに合わせて変えておいてもらえると助かります!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

READMEを修正しました!

}


def _generate_coco_images(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ここだけの話ではありませんが、Pythonでメソッドをプライベートにしたいだけの場合に、先頭のアンダースコア2つ __ を用いるべきではなく、単純にアンダースコア1つにすることが推奨されています。アンダースコア2つはマングリングという特別な動作のために用いられるものであるため、それを意図した場合にのみ使うべきです。

そのため、追加したプライベートメソッドはアンダースコア1つにしています。

Comment on lines +216 to 218
if annotation_type != AnnotationType.pose_estimation.value and (
not points or (len(points) == 0)
):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

既知のバグの修正。

Comment on lines +234 to +240
def __get_coco_category_by_name(categories: list, name: str) -> Optional[dict]:
matched_categories = [
category for category in categories if category["name"] == name
]
if len(matched_categories) >= 1:
return matched_categories[0]
return None
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

既知のバグの修正

Comment on lines +1035 to +1041
@contextmanager
def VideoCapture(*args, **kwds):
videoCapture = cv2.VideoCapture(*args, **kwds)
try:
yield videoCapture
finally:
videoCapture.release()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

withを使った自動リソース開放のためContextManagerを使います。

videoCapture.release()


def _download_file(url: str, output_file_path: str, chunk_size: int = 8192) -> str:
Copy link
Contributor Author

@fuji44 fuji44 Mar 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

チャンクサイズは適切なものが推測しづらいので、ひとまずの値です。将来的には、利用者が指定できるようにしてもよいかもしれません。

Comment on lines +1086 to +1089
with NamedTemporaryFile(prefix="fastlabel-sdk-") as video_file:
video_file_path = _download_file(
url=video_task["url"], output_file_path=video_file.name
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

動画ファイルは一時ファイルとして書き込み、withを抜けたら削除されるようにしています。
OSやPythonランタイムによりますが、xnuxでCPythonならおおよそ /tmp/fastlabel-sdk-xxxx になるはずです。

Comment on lines +63 to +64
def is_video_project_type(project_type: str):
return type(project_type) is str and project_type.startswith("video_")
Copy link
Contributor Author

@fuji44 fuji44 Mar 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

文字の前方一致で判断します。

他の類似コードではすべてのタイプを定数化して一致判定していることがほとんどですが、定数の管理コストを考えて前方一致で判定することにしました。問題があるようなら指摘してください。

@fuji44 fuji44 marked this pull request as ready for review March 6, 2023 16:57
@ueno-k-work ueno-k-work merged commit 45831a4 into main Mar 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants