はじめに
Unityを使って、カメラでobject detectを行うための組み込み方を調べていたので覚書
必要なツールとパフォーマンスについての参考
この動画がとても参考になった
パフォーマンス的にBaracudaよりもTFLiteの推論ランタイムがおすすめ
..ということで、UnityでTensorflowLiteを使うために下記あたりを参考に組み込んでみようと調べてみる
他にもalia-sdkなどいくつかフレームワークは存在していたり。(未実装)
手順
manifest.json
{
"scopedRegistries": [
{
"name": "package.openupm.com",
"url": "https://package.openupm.com",
"scopes": [
"com.cysharp.unitask"
]
},
{
"name": "npm",
"url": "https://registry.npmjs.com",
"scopes": [
"com.github.asus4"
]
}
],
"dependencies": {
"com.github.asus4.tflite": "2.13.0",
"com.github.asus4.tflite.common": "2.13.0",
"com.github.asus4.mediapipe": "2.13.0",
...
}
}
modelのインポート
ファイルのインポート
MainCamera -> Camera Texture Background (Tensorflow Lite)
Demo -> Virtual Texture Source
WebCamTexture
using UnityEngine;
public class CamScript : MonoBehaviour
{
void Start()
{
WebCamTexture webCamTexture = new WebCamTexture();
Renderer renderer = GetComponent<Renderer>();
renderer.material.mainTexture = webCamTexture;
webCamTexture.Play();
}
}
Bugなど
Library/PackageCache/com.unity.xr.management@4.4.1/Editor/Metadata/KnownPackages.cs(12,70): error CS0117: 'BuildTargetGroup' does not contain a definition for 'VisionOS'
Unityを使ってデバイスのカメラインプットを利用する場合、大体検索にヒットする方法はCanvasにrawImageを置いて、そのtextureにWebCamTextureを流し込む方法です。たしかにラップトップでは問題なくサンプルコードも動きますが、iOSで動作させた瞬間にアスペクト比、オリエンテーション(向き)がむちゃくちゃになります。
WebCamTexture rotated and flipped on iPhone when applied to renderer.material.texture