OpenCV for Unityを使って顔認証→認識した顔の前にイメージを表示するアプリを作っています。
手順)
1、OpenCv for unityのインストール
2、OpenCvのセット(Set Plugin Import SettingとStreamingAssetsをAssets直下へ)
3、CanvasとQuadを作成して下記コードをQuadにセット
4、これでいいかと思っていましたがシュミレータで試すと下記エラーが出ます。
エラー内容はtexture2DがNullと言う意味で判断しています。
しかしOnWebCamTextureToMatHelperInitialized()でtexture2Dをnewしており、
Start時に_webCamTextureToMatHelper.Initialize()で実行しているので
Nullではないような気がしています。どこがおかしいのでしょうか?
環境)
PC: Windows10
Unity2019.4.0f1
言語:C#
error内容)
ArgumentNullException: Value cannot be null. Parameter name: texture2D OpenCVForUnity.UnityUtils.Utils.fastMatToTexture2D (OpenCVForUnity.CoreModule.Mat mat, UnityEngine.Texture2D texture2D, System.Boolean flip, System.Int32 flipCode, System.Boolean flipAfter, System.Boolean updateMipmaps, System.Boolean makeNoLongerReadable) (at Assets/OpenCVForUnity/org/opencv/unity/Utils.cs:321) WebCamera.Update () (at Assets/Scripts/WebCamera.cs:45)
WebCamera.cs
using System.Collections.Generic; using OpenCVForUnity; using OpenCVForUnityExample; using UnityEngine; using OpenCVForUnity.CoreModule; using OpenCVForUnity.ImgprocModule; using OpenCVForUnity.UnityUtils; using OpenCVForUnity.UnityUtils.Helper; using UnityEngine.UI; [RequireComponent(typeof(WebCamTextureToMatHelper), typeof(FpsMonitor))] public class WebCamera : MonoBehaviour { [SerializeField] Image _firePrefab; Texture2D _texture; WebCamTextureToMatHelper _webCamTextureToMatHelper; FpsMonitor _fpsMonitor; Image _fire; Mat webCamTextureMat; static readonly Scalar SKIN_LOWER = new Scalar(0, 70, 90); static readonly Scalar SKIN_UPPER = new Scalar(35, 255, 255); static readonly float FIRE_SCALE = 100f; static readonly float FIRE_Z_POS = -10f; void Start() { Input.backButtonLeavesApp = true; _fpsMonitor = GetComponent<FpsMonitor>(); _webCamTextureToMatHelper = GetComponent<WebCamTextureToMatHelper>(); _webCamTextureToMatHelper.Initialize(); _fire = Instantiate(_firePrefab, new Vector3(0f, 0f, FIRE_Z_POS), Quaternion.identity); _fire.transform.localScale = new Vector3(FIRE_SCALE, FIRE_SCALE, FIRE_SCALE); } void Update() { if (_webCamTextureToMatHelper.IsPlaying() && _webCamTextureToMatHelper.DidUpdateThisFrame()) { Mat rgbaMat = _webCamTextureToMatHelper.GetMat(); SetImage(rgbaMat); Utils.fastMatToTexture2D(rgbaMat, _texture); }else{ return; } } void SetImage(Mat webcamMat) { //RGBAをHSVに変換 using(var hsvMat = new Mat()) using(var handMask = new Mat()) using(var centroids = new Mat()) using(var stats = new Mat()){ //RGBAをHSVに変換 Imgproc.cvtColor(webcamMat, hsvMat, Imgproc.COLOR_RGBA2RGB); Imgproc.cvtColor(hsvMat, hsvMat, Imgproc.COLOR_RGB2HSV); //肌色領域を抽出 Core.inRange(hsvMat, SKIN_LOWER, SKIN_UPPER, handMask); //ラベリング var nLabels = Imgproc.connectedComponentsWithStats(handMask, new Mat(), stats, centroids); //最大の領域の重心を取得 var maxAreaLabel = 0; var maxArea = 0.0; for (int i = 1; i < nLabels; i++) { //0番目のラベルは背景のため飛ばす var area = stats.get(i, Imgproc.CC_STAT_AREA)[0]; if (area > maxArea) { maxArea = area; maxAreaLabel = i; } } //画像上の重心位置をワールド座標に変換 var ctrdOnImg = new Point(centroids.get(maxAreaLabel, 0)[0], centroids.get(maxAreaLabel, 1)[0]); var ctrdOnWorld = new Point((float)ctrdOnImg.x - webcamMat.width() / 2f, webcamMat.height() / 2f - (float)ctrdOnImg.y); _fire.transform.position = new Vector3((float)ctrdOnWorld.x, (float)ctrdOnWorld.y, FIRE_Z_POS); } } public void OnWebCamTextureToMatHelperInitialized() { Debug.Log ("OnWebCamTextureToMatHelperInitialized"); var webCamTextureMat = _webCamTextureToMatHelper.GetMat(); _texture = new Texture2D (webCamTextureMat.cols(), webCamTextureMat.rows(), TextureFormat.RGBA32, false); GetComponent<Renderer>().material.mainTexture = _texture; Debug.Log ("Screen.width " + Screen.width + " Screen.height " + Screen.height + " Screen.orientation " + Screen.orientation); if (_fpsMonitor != null){ _fpsMonitor.Add ("width", webCamTextureMat.width().ToString()); _fpsMonitor.Add ("height", webCamTextureMat.height().ToString()); _fpsMonitor.Add ("orientation", Screen.orientation.ToString()); } float width = webCamTextureMat.width(); float height = webCamTextureMat.height(); float widthScale = Screen.width / width; float heightScale = Screen.height / height; if (widthScale < heightScale) { Camera.main.orthographicSize = (width * Screen.height / Screen.width) / 2; } else { Camera.main.orthographicSize = height / 2; } //Quadを画面いっぱいにリサイズ ////https: //blog.narumium.net/2016/12/11/unityでスマホカメラを全面表示する/ var quadHeight = Camera.main.orthographicSize * 2; var quadWidth = quadHeight * Camera.main.aspect; transform.localScale = new Vector3(quadWidth, quadHeight, 1); } public void OnWebCamTextureToMatHelperDisposed () { Debug.Log ("OnWebCamTextureToMatHelperDisposed"); if (_texture != null) { Destroy(_texture); _texture = null; } } public void OnWebCamTextureToMatHelperErrorOccurred(WebCamTextureToMatHelper.ErrorCode errorCode){ Debug.Log ("OnWebCamTextureToMatHelperErrorOccurred " + errorCode); } void OnDestroy() { _webCamTextureToMatHelper.Dispose (); } }
回答1件
あなたの回答
tips
プレビュー