Search Unity

  1. All Pro and Enterprise subscribers: find helpful & inspiring creative, tech, and business know-how in the new Unity Success Hub. Sign in to stay up to date.
    Dismiss Notice
  2. Dismiss Notice

[RELEASED] OpenCV for Unity

Discussion in 'Assets and Asset Store' started by EnoxSoftware, Oct 30, 2014.

  1. lobombero

    lobombero

    Joined:
    Sep 22, 2020
    Posts:
    1
    Hello everyone!

    Does anyone know how I can add faces from image files and then do the recognition from the webcam?
     
  2. SavedByZero

    SavedByZero

    Joined:
    May 23, 2013
    Posts:
    69
    Hi there -- all the examples of object detection I'm finding are about faces. However, we need something that would find other random objects in the environment from an AR camera feed. Is it possible to use the OpenCV training functions to do our own object detection training with this plugin? like opencv_traincascade to build the xml file from a bunch of images, then custom_cascade.DetectMultiScale()? Are these or something like them exposed with OpenCVForUnity? (the api showed me some methods with "train" in the name, but I didn't see those)
     
    Last edited: Jun 2, 2021
  3. EnoxSoftware

    EnoxSoftware

    Joined:
    Oct 29, 2014
    Posts:
    1,344
  4. EnoxSoftware

    EnoxSoftware

    Joined:
    Oct 29, 2014
    Posts:
    1,344
    OpenCVForUnity itself does not have the ability to train cascade files.
    Of course, it is possible to use OpenCVForUnity for object detection with cascade files trained using opencv_traincascade.
    https://github.com/opencv/opencv/bl...c3/doc/tutorials/others/traincascade.markdown
     
  5. Philkrom

    Philkrom

    Joined:
    Dec 26, 2015
    Posts:
    65
    Hello,
    Your asset seems very intersting, and I have a question : how complicated would it be to achieve this :
    ? Maybe someone already made something like that ?
    Best regards, Phil
     
  6. SavedByZero

    SavedByZero

    Joined:
    May 23, 2013
    Posts:
    69
  7. EnoxSoftware

    EnoxSoftware

    Joined:
    Oct 29, 2014
    Posts:
    1,344
    YoloObjectDetectionWebCamTextureExample is included in OpenCVForUnity. This is an example of detecting a car from a WebCam feed.
    opencvforunity2.4.0_feature.png
     
  8. EnoxSoftware

    EnoxSoftware

    Joined:
    Oct 29, 2014
    Posts:
    1,344
    That’s correct.
     
  9. BigToe

    BigToe

    Joined:
    Nov 1, 2010
    Posts:
    189
    I am trying to convert a RenderTexture directly to a Mat using a Ptr or NativeArray, but haven't had any luck. I was hoping you might be able to point me in the right direction.

    The pasted code is a sample of some of my attempts.

    The second bit is for a Texture2D which I was using to try and simplify the problem. It works, but not sure how to get a similar native array for render textures.

    Code (CSharp):
    1.  
    2. private RenderTexture _myRenderTexture;
    3. private Texture2D _myTexture2D;
    4. public void PtrToMat()
    5.  {
    6.       var targetMat = new Mat(1920, 1080, CvType.CV_8UC4);
    7.        
    8.       //This doesn't work and RenderTextures don't seem to support GetRawTextureData
    9.       var intPtr = _myRenderTexture.GetNativeTexturePtr();
    10.       MatUtils.copyToMat(intPtr,targetMat);
    11.        
    12.       //This works for a Texture2D
    13.       var nativeArray = _myTexture2D.GetRawTextureData<Color32>();
    14.       MatUtils.copyToMat(nativeArray,targetMat);
    15.  
    16.  }
    Thanks in advance.

    Also, I just tried the NativeArrawUnsafeUtility and probably don't know what I'm doing...Which seems safe.
    Code (CSharp):
    1. var ptr = _myRenderTexture.GetNativeTexturePtr();
    2.  
    3. NativeArray<Color32> rtNative = NativeArrayUnsafeUtility.ConvertExistingDataToNativeArray<Color32>((void*) ptr, 1920 * 1080 * 4,Allocator.Persistent);
    4.  
    5. MatUtils.copyToMat(rtNative,targetMat);
    6.  
    The rtNative array never seems to get populated.
     
    Last edited: Jun 10, 2021
  10. EnoxSoftware

    EnoxSoftware

    Joined:
    Oct 29, 2014
    Posts:
    1,344

    The ponters obtained from the GetNativeTexturePtr() method cannot be used for MatUtils.copyToMat(intPtr, mat) since they do not indicate the first pointer of the pixel buffer in memory.
    We looked into the latest information on this subject some time ago, but unfortunately we have not found a more efficient way to get pixel data from a RenderTexture other than "ReadPixels" or "AsyncGPUReadback".
     
  11. ScottMeyers

    ScottMeyers

    Joined:
    Aug 10, 2017
    Posts:
    5
    Need Help !!!

    Unity Editor crash because of opencvforunity.dll many times.

    upload_2021-6-11_23-29-15.png
     

    Attached Files:

  12. BigToe

    BigToe

    Joined:
    Nov 1, 2010
    Posts:
    189
    Thank you!
     
  13. EnoxSoftware

    EnoxSoftware

    Joined:
    Oct 29, 2014
    Posts:
    1,344
    Thank you very much for reporting.

    Could you tell me the environment you tested?
    OpenCV for Unity version :
    Unity version :
    Windows OS version :
     
  14. ScottMeyers

    ScottMeyers

    Joined:
    Aug 10, 2017
    Posts:
    5
    Unity version: 2020.3.11f1 and 2021.1.9f1
    OpenCV for Unity version: v2.4.4
    Windows OS version: Windows 10 20H2 19042.985
     
  15. EnoxSoftware

    EnoxSoftware

    Joined:
    Oct 29, 2014
    Posts:
    1,344
    In my environment, OpenCVForUnity is running on multiple PCs without any problems.
    Unity version: 2020.3.11f1
    OpenCV for Unity version: v2.4.4
    Windows OS version: Windows 10 20H2 19042.1052

    Also make sure that you have enough free disk space.
     
unityunity