Search Unity

[RELEASED] OpenCV for Unity

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

  1. EnoxSoftware

    EnoxSoftware

    Joined:
    Oct 29, 2014
    Posts:
    1,564
    Thank you!!
     
  2. Popplio

    Popplio

    Joined:
    Sep 9, 2016
    Posts:
    1
    Hey, Enox.

    How would one go around adding face recognition to your face detection from webcam sample?
    https://github.com/EnoxSoftware/Ope...ctFaceSample/WebCamTextureDetectFaceSample.cs

    I tried starting off from the WebCamTextureDetectFaceSample but I'm not sure if I should use the grayMat in place of the testsamplemat when using Predict(). Are there any specifications I need to have for the images I add to the Mat list images when training the model? I'm using a regular grayscale of my face but always getting 0 as predicted label and confidence, even when I change the order of the images in the list.
     
  3. EnoxSoftware

    EnoxSoftware

    Joined:
    Oct 29, 2014
    Posts:
    1,564
    I think that this sample would be helpful.
    MasteringOpenCV - Chapter8_FaceRecognition
    https://github.com/MasteringOpenCV/code/tree/master/Chapter8_FaceRecognition
    https://github.com/MasteringOpenCV/code/blob/master/Chapter8_FaceRecognition/screenshot.png
     
  4. Kelekkis

    Kelekkis

    Joined:
    Sep 8, 2014
    Posts:
    2
    Tweaked the code a bit and got it working perfectly!

    Code (CSharp):
    1.                 Matrix4x4 transCenterM =
    2.                     Matrix4x4.TRS(new Vector3(((float)rightRect.center.x) - webWidth / 2, (webHeight - (float)rightRect.center.y) - webHeight / 2, 0), Quaternion.identity, new Vector3(1, 1, 1));
    3.  
    4.                 Vector3 point3DVec = new Vector3(0, 0, 0);
    5.                 point3DVec = transCenterM.MultiplyPoint3x4(point3DVec);
    Thanks!
     
  5. cel

    cel

    Joined:
    Feb 15, 2011
    Posts:
    46
    Hi, would you guys consider developing actions for playmaker?
     
  6. JohnSmith13

    JohnSmith13

    Joined:
    Jun 8, 2013
    Posts:
    4
    I try to port MarkerlessAR code in MasteringOpenCV (https://github.com/MasteringOpenCV/code/blob/master/Chapter3_MarkerlessAR) to OpenCV for Unity.
    But at last step I trapped in this method:
    Especially I can't find definition of pose3d.r() and pose3d.getInverted() in OpenCV document or Google result.
    Could someone help show me some expamples or code snaps ?
     
  7. EnoxSoftware

    EnoxSoftware

    Joined:
    Oct 29, 2014
    Posts:
    1,564
    GeometryTypes class does not seem to be a OpenCV's class.
    https://github.com/MasteringOpenCV/code/blob/master/Chapter3_MarkerlessAR/src/GeometryTypes.cpp
     
  8. SinisterDex

    SinisterDex

    Joined:
    Jul 2, 2013
    Posts:
    3
    Hi Enox,

    i just bought OpenCV for Unity and Dlib FaceLandmark Detector and have been testing out your samples. Everything
    works great! So thanks for that.

    I'm a bit of a newbie when it comes to OpenCV, so here is what i intend to do:

    I would like to recognize Faces via webcam and then apply an "aging" effect to make them look old.

    I'm not sure yet which approach would be better:
    • Detect Faces and transform directly on the texture/mat via image processing or
    • Apply a mask over the face via image processing or
    • Use a 3d face model with a texture and apply on top of the face (like your WebCamTextureARSample )

    Which one do you think would make more sense?

    How would i retrieve just the face so i could manipulate it or how would i apply a mask over a face with your asset?

    Any hints/tips/links would be very appreciated.
     
  9. tumeo1993

    tumeo1993

    Joined:
    Jul 3, 2015
    Posts:
    1
    Hi Enox.
    Im working on a project about Object Tracking.
    I found your MultiObjectTrackingBasedOnColorSample in package. But i have some problems. Here is my code:
    Code (CSharp):
    1.  
    2.         Mat threshold = new Mat ();
    3.         Mat temp = new Mat ();
    4.         inputMat.copyTo (temp);
    5.            
    6.         Imgproc.blur (temp, threshold, new Size (10, 10));
    7.         Imgproc.threshold (threshold, threshold, 80, 160, Imgproc.THRESH_BINARY);
    8.         Imgproc.Canny (threshold, threshold, 80, 160);
    9.  
    10.         List<FilterObject> objects = new List<FilterObject> ();
    11.         //these two vectors needed for output of findContours
    12.         Mat hierarchy = new Mat ();
    13.         List<MatOfPoint> contours = new List<MatOfPoint> ();
    14.            
    15.         //find contours of filtered image using openCV findContours function
    16.         Imgproc.findContours (threshold, contours, hierarchy, Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_SIMPLE);
    17.         //use moments method to find our filtered object
    18.         bool objectFound = false;
    19.         for (int index = 0; index < hierarchy.size().width; index++) {
    20.             Moments moment = Imgproc.moments ((Mat)contours [index]);
    21.  
    22.             double area = Imgproc.contourArea (contours [index]);
    23.  
    24.             if (area > MIN_OBJECT_AREA) {
    25.                 FilterObject foundObj = new FilterObject (theObject.objectName);
    26.                 foundObj.xPos = ((int)moment.get_m10 () / area);
    27.                 foundObj.yPos = ((int)moment.get_m01 () / area);
    28.                 foundObjects.Add (foundObj);
    29.                 break;
    30.             }
    31.         }
    foundObject.xPos and foundObject.yPos always returns 0.
    Can you help me. I have a presentation on this Friday.
     
  10. voladorfly

    voladorfly

    Joined:
    Sep 22, 2016
    Posts:
    15
    I would like to ask a question。
    Texture2D imgTexture = Resources.Load ("mario") as Texture2D;
    Can not change the other folder?
     
  11. voladorfly

    voladorfly

    Joined:
    Sep 22, 2016
    Posts:
    15
    @EnoxSoftware
     
  12. EnoxSoftware

    EnoxSoftware

    Joined:
    Oct 29, 2014
    Posts:
    1,564
    Sorry, I do not know which approach is better.
    FaceSwapperSample will be helpful about how to manipulate only the face.
    https://www.assetstore.unity3d.com/en/#!/content/66602
     
  13. Reeven

    Reeven

    Joined:
    Mar 24, 2015
    Posts:
    1
    Hello Enox,

    It seems your Unity wrap of OpenCV shares the same problem as the Java wrap regarding SVMs: it forgets to wrap the load method, which means that it is impossible to load and use an already trained SVM. See this one year old bug. The function is documented in your API, but load cannot be accessed in actual code.

    For image recognition, I don't see many people using Unity for actual training when training can be much better tuned in its own OpenCV C++ application. Being able to load and use a pre-trained SVM, on the other hand, I think is pretty much the core of SVM prediction use cases in mobile / real time applications where a Unity plugin makes sense.

    Will there be an update to fix the loading method for SVM? From the bug's timestamp, the OpenCV / Java people do not seem in a hurry to fix it themselves.

    Thanks!

    EDIT: Also, Bag of Words are missing as well. The BOWKMeansTrainer and BOWImgDescriptorExtractor classes are nowhere to be found.
     
    Last edited: Sep 22, 2016
  14. EnoxSoftware

    EnoxSoftware

    Joined:
    Oct 29, 2014
    Posts:
    1,564
    The following code works fine in my environment.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. #if UNITY_5_3 || UNITY_5_3_OR_NEWER
    6. using UnityEngine.SceneManagement;
    7. #endif
    8. using OpenCVForUnity;
    9.  
    10. namespace OpenCVForUnitySample
    11. {
    12.     /// <summary>
    13.     /// Texture2D to mat sample.
    14.     /// </summary>
    15.     public class ObjectTrackingSample : MonoBehaviour
    16.     {
    17.         /// <summary>
    18.         /// max number of objects to be detected in frame
    19.         /// </summary>
    20.         const int MAX_NUM_OBJECTS = 50;
    21.        
    22.         /// <summary>
    23.         /// minimum and maximum object area
    24.         /// </summary>
    25.         const int MIN_OBJECT_AREA = 20 * 20;
    26.  
    27.         // Use this for initialization
    28.         void Start ()
    29.         {
    30.             Utils.setDebugMode(true);
    31.  
    32.             Texture2D imgTexture = Resources.Load ("lena") as Texture2D;
    33.  
    34.             Mat imgMat = new Mat (imgTexture.height, imgTexture.width, CvType.CV_8UC1);
    35.  
    36.             Utils.texture2DToMat (imgTexture, imgMat);
    37.             Debug.Log ("imgMat.ToString() " + imgMat.ToString ());
    38.  
    39.  
    40.  
    41.  
    42.             Mat threshold = new Mat ();
    43.             Mat temp = new Mat ();
    44.             imgMat.copyTo (temp);
    45.            
    46.             Imgproc.blur (temp, threshold, new Size (10, 10));
    47.             Imgproc.threshold (threshold, threshold, 80, 160, Imgproc.THRESH_BINARY);
    48.             Imgproc.Canny (threshold, threshold, 80, 160);
    49.            
    50. //            List<FilterObject> objects = new List<FilterObject> ();
    51.             //these two vectors needed for output of findContours
    52.             Mat hierarchy = new Mat ();
    53.             List<MatOfPoint> contours = new List<MatOfPoint> ();
    54.            
    55.             //find contours of filtered image using openCV findContours function
    56.             Imgproc.findContours (threshold, contours, hierarchy, Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_SIMPLE);
    57.             //use moments method to find our filtered object
    58.             bool objectFound = false;
    59.             for (int index = 0; index < hierarchy.size().width; index++) {
    60.                 Moments moment = Imgproc.moments ((Mat)contours [index]);
    61.                
    62.                 double area = Imgproc.contourArea (contours [index]);
    63.  
    64.                
    65.                 if (area > MIN_OBJECT_AREA) {
    66.                     Debug.Log ("area " + area);
    67.                    
    68.                     Debug.Log ("xpos " + ((int)moment.get_m10 () / area));
    69.                     Debug.Log ("ypos " + ((int)moment.get_m01 () / area));
    70. //                    FilterObject foundObj = new FilterObject (theObject.objectName);
    71. //                    foundObj.xPos = ((int)moment.get_m10 () / area);
    72. //                    foundObj.yPos = ((int)moment.get_m01 () / area);
    73. //                    foundObjects.Add (foundObj);
    74.                     break;
    75.                 }
    76.             }
    77.  
    78.  
    79.  
    80.  
    81.  
    82.             Texture2D texture = new Texture2D (imgMat.cols (), imgMat.rows (), TextureFormat.RGBA32, false);
    83.  
    84.             Utils.matToTexture2D (threshold, texture);
    85.  
    86.             gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
    87.  
    88.  
    89.             Utils.setDebugMode(false);
    90.         }
    91.    
    92.         // Update is called once per frame
    93.         void Update ()
    94.         {
    95.    
    96.         }
    97.  
    98.         public void OnBackButton ()
    99.         {
    100.             #if UNITY_5_3 || UNITY_5_3_OR_NEWER
    101.             SceneManager.LoadScene ("OpenCVForUnitySample");
    102.             #else
    103.             Application.LoadLevel ("OpenCVForUnitySample");
    104.             #endif
    105.         }
    106.     }
    107. }
    console output
    imgMat.ToString() Mat [ 512*512*CV_8UC1, isCont=True, isSubmat=False, nativeObj=0x669136912, dataAddr=0x675455008 ]
    area 952.5
    xpos 165.090813648294
    ypos 290.333858267717
     
  15. EnoxSoftware

    EnoxSoftware

    Joined:
    Oct 29, 2014
    Posts:
    1,564
  16. voladorfly

    voladorfly

    Joined:
    Sep 22, 2016
    Posts:
    15
    I need updated in real time images,
    Resources. The Load
    And then export the android can't use
    @EnoxSoftware
     
  17. voladorfly

    voladorfly

    Joined:
    Sep 22, 2016
    Posts:
    15
    Texture2D imgTexture = Resources.Load ("lena") as Texture2D;
    Only this kind of method to load images, are there any other method for the image?
     
  18. EnoxSoftware

    EnoxSoftware

    Joined:
    Oct 29, 2014
    Posts:
    1,564
    Because "OpenCV for Unity v2.0.6" is a clone of OpenCV3.1.0(commit) Java Wrapper, this asset shares the same problem as the Java wrapper.

    Also, BOWKMeansTrainer class is scheduled to be implemented in the next version OpenCV for Unity v2.0.7 based on OpenCV3.1.0(commit).
     
  19. EnoxSoftware

    EnoxSoftware

    Joined:
    Oct 29, 2014
    Posts:
    1,564
  20. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    1,085
    It looks like Unity WebGL video is possible https://www.assetstore.unity3d.com/en/#!/content/38369
    Will OpenCV for Unity support be possible?
     
  21. EnoxSoftware

    EnoxSoftware

    Joined:
    Oct 29, 2014
    Posts:
    1,564
    for now, Since the Unity WebGL does not support the native plugin, there is no plan to support WebGL platform.
     
  22. voladorfly

    voladorfly

    Joined:
    Sep 22, 2016
    Posts:
    15
    opencv for unity support shape context?
     
  23. voladorfly

    voladorfly

    Joined:
    Sep 22, 2016
    Posts:
    15
    findContours(currentQuery, _contoursQuery, RETR_LIST, CHAIN_APPROX_NONE);
    for (size_t border = 0; border<_contoursQuery.size(); border++)
    {
    for (size_t p = 0; p<_contoursQuery[border].size(); p++)
    {
    contoursQuery.push_back(_contoursQuery[border][p]);
    }
    }

    // In case actual number of points is less than n
    int dummy = 0;
    for (int add = (int)contoursQuery.size() - 1; add<n; add++)
    {
    contoursQuery.push_back(contoursQuery[dummy++]); //adding dummy values
    }

    // Uniformly sampling
    random_shuffle(contoursQuery.begin(), contoursQuery.end());
    vector<Point> cont;
    for (int i = 0; i<n; i++)
    {
    cont.push_back(contoursQuery);
    }
    return cont;
    }
    How to translate into c # code?
    @EnoxSoftware
     
  24. EnoxSoftware

    EnoxSoftware

    Joined:
    Oct 29, 2014
    Posts:
    1,564
  25. voladorfly

    voladorfly

    Joined:
    Sep 22, 2016
    Posts:
    15
    Have any examples about shape context? Because c + + data format conversion for c # is different
     
  26. voladorfly

    voladorfly

    Joined:
    Sep 22, 2016
    Posts:
    15
    All the outline of how to extract the image on a point set?
     
  27. voladorfly

    voladorfly

    Joined:
    Sep 22, 2016
    Posts:
    15
    Hello, I bought a opencv for unity plug-in: version 2.07.There is a shapecontext method.But I don't know how to write.Is there a case?
    I know opencv c + + code, but I don't know how to turn to c #.
     
  28. EnoxSoftware

    EnoxSoftware

    Joined:
    Oct 29, 2014
    Posts:
    1,564
    Because "OpenCV for Unity v2.0.7" is a clone of OpenCV3.1.0(commit) Java Wrapper, this asset shares the same problem as the Java wrapper.
    The ShapeContextDistanceExtractor.computeDistance() method seems to have such a bug.
    http://answers.opencv.org/question/60974/matching-shapes-with-hausdorff-and-shape-context-distance/
    Unfortunately, When I tested test code, this bug has occurred on "OpenCV for Unity2.0.7".
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. #if UNITY_5_3 || UNITY_5_3_OR_NEWER
    6. using UnityEngine.SceneManagement;
    7. #endif
    8. using OpenCVForUnity;
    9.  
    10. namespace OpenCVForUnitySample
    11. {
    12.     /// <summary>
    13.     /// Texture2D to mat sample.
    14.     /// </summary>
    15.     public class ShapeDistanceExtractorSample : MonoBehaviour
    16.     {
    17.  
    18.         // Use this for initialization
    19.         void Start ()
    20.         {
    21.  
    22.             Mat img1 = Imgcodecs.imread (Utils.getFilePath ("shape_sample/1.png"), Imgcodecs.IMREAD_GRAYSCALE);
    23.             Mat img2 = Imgcodecs.imread (Utils.getFilePath ("shape_sample/2.png"), Imgcodecs.IMREAD_GRAYSCALE);
    24.  
    25.             Debug.Log ("img1.ToString() " + img1.ToString ());
    26.             Debug.Log ("img2.ToString() " + img2.ToString ());
    27.  
    28.             if (img1.empty () || img2.empty ()) {
    29.                 Debug.Log ("Failed to open image file.");
    30.                 return;
    31.             }
    32.            
    33.            
    34.             Mat contour1 = simpleContour (img1);
    35.             Mat contour2 = simpleContour (img2);
    36.  
    37.            
    38.             ShapeContextDistanceExtractor mysc =
    39.                 Shape.createShapeContextDistanceExtractor ();
    40. //            HausdorffDistanceExtractor mysc =
    41. //                Shape.createHausdorffDistanceExtractor();
    42.             float distance = mysc.computeDistance (contour1, contour2);
    43.  
    44.             Debug.Log ("distance " + distance);
    45.  
    46.  
    47.             Texture2D texture = new Texture2D (img1.cols (), img1.rows (), TextureFormat.RGBA32, false);
    48.  
    49.             Utils.matToTexture2D (img1, texture);
    50.  
    51.             gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
    52.  
    53.         }
    54.    
    55.         // Update is called once per frame
    56.         void Update ()
    57.         {
    58.    
    59.         }
    60.  
    61.         public void OnBackButton ()
    62.         {
    63.             #if UNITY_5_3 || UNITY_5_3_OR_NEWER
    64.             SceneManager.LoadScene ("OpenCVForUnitySample");
    65.             #else
    66.             Application.LoadLevel ("OpenCVForUnitySample");
    67.             #endif
    68.         }
    69.  
    70.         static Mat simpleContour (Mat src)
    71.         {
    72.             List<MatOfPoint> contours = new List<MatOfPoint> ();
    73.             Mat srcHierarchy = new Mat ();
    74.  
    75. //            Imgproc.findContours(src, contours, srcHierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_NONE);
    76.             Imgproc.findContours (src, contours, srcHierarchy, Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_TC89_KCOS);
    77.  
    78.  
    79.  
    80. //            size_t contours_size = contours.size();
    81. //          
    82. //            for (size_t border = 0; border < contours_size; border++)
    83. //            {
    84. //                size_t contour_size = contours[border].size();
    85. //                for (size_t p = 0; p < contour_size; p++)
    86. //                {
    87. //                    contour_points.push_back(contours[border][p]);
    88. //                }
    89. //            }
    90.  
    91.  
    92.             List<Point> list = new List<Point> ();
    93.             foreach (var item in contours) {
    94.                 Debug.Log ("contours.Count " + item.ToString ());
    95.                 list.AddRange (item.toList ());
    96.             }
    97.  
    98.             MatOfPoint contour_points = new MatOfPoint ();
    99.             contour_points.fromList (list);
    100.  
    101.             Debug.Log ("contour_points.ToString() " + contour_points.ToString ());
    102.             Debug.Log ("contour_points.ToString() " + contour_points.dump ());
    103.  
    104.             return contour_points;
    105.         }
    106.     }
    107. }
    If you want a refund request, please email here(store@enoxsoftware.com).
     
  29. voladorfly

    voladorfly

    Joined:
    Sep 22, 2016
    Posts:
    15
    I to buy opencv for unity 2.06 or 2.05, shapecontext this method still could go wrong?
     
  30. voladorfly

    voladorfly

    Joined:
    Sep 22, 2016
    Posts:
    15
    // Uniformly sampling
    random_shuffle(contoursQuery.begin(), contoursQuery.end());

    vector<Point> cont;



    for (int i = 0; i<n; i++)
    {
    cont.push_back(contoursQuery);
    }
    How the opencv for choose the number of feature points of unity for distance transformation?
     
    chengnuoni likes this.
  31. EnoxSoftware

    EnoxSoftware

    Joined:
    Oct 29, 2014
    Posts:
    1,564
    This bug is present from the old version.
    Code (CSharp):
    1.        static Mat simpleContour (Mat currentQuery, int n=300)
    2.         {
    3. //            vector<vector<Point> > _contoursQuery;
    4. //            vector <Point> contoursQuery;
    5. //            findContours(currentQuery, _contoursQuery, RETR_LIST, CHAIN_APPROX_NONE);
    6. //            for (size_t border=0; border<_contoursQuery.size(); border++)
    7. //            {
    8. //                for (size_t p=0; p<_contoursQuery[border].size(); p++)
    9. //                {
    10. //                    contoursQuery.push_back( _contoursQuery[border][p] );
    11. //                }
    12. //            }
    13.             List<MatOfPoint> _contoursQuery = new List<MatOfPoint> ();
    14.             Mat srcHierarchy = new Mat ();
    15.             MatOfPoint contoursQuery = new MatOfPoint ();
    16.             Imgproc.findContours (currentQuery, _contoursQuery, srcHierarchy, Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_NONE);
    17.    
    18.             List<Point> contoursQueryList = new List<Point> ();
    19.             foreach (var item in _contoursQuery) {
    20.                 contoursQueryList.AddRange (item.toList ());
    21.             }
    22.  
    23.             // In case actual number of points is less than n
    24. //            int dummy=0;
    25. //            for (int add=(int)contoursQuery.size()-1; add<n; add++)
    26. //            {
    27. //                contoursQuery.push_back(contoursQuery[dummy++]); //adding dummy values
    28. //            }
    29.             int dummy = 0;
    30.             for (int add=(int)contoursQueryList.Count-1; add<n; add++) {
    31.                 contoursQueryList.Add (contoursQueryList [dummy++]); //adding dummy values
    32.             }
    33.  
    34.             // Uniformly sampling
    35. //            random_shuffle(contoursQuery.begin(), contoursQuery.end());
    36. //            vector<Point> cont;
    37. //            for (int i=0; i<n; i++)
    38. //            {
    39. //                cont.push_back(contoursQuery[i]);
    40. //            }
    41.             contoursQueryList.Shuffle ();
    42.  
    43.  
    44.             contoursQuery.fromList (contoursQueryList);
    45.  
    46.             Debug.Log ("contoursQuery.ToString() " + contoursQuery.ToString ());
    47.             Debug.Log ("contoursQuery.ToString() " + contoursQuery.dump ());
    48.  
    49.             return contoursQuery;
    50.         }
    51.  
    52.  
    53.     }
    54.  
    55.     static class MyExtensions
    56.     {
    57.        
    58.         private static System.Random rng = new System.Random ();
    59.        
    60.         public static void Shuffle<T> (this IList<T> list)
    61.         {
    62.             int n = list.Count;
    63.             while (n > 1) {
    64.                 n--;
    65.                 int k = rng.Next (n + 1);
    66.                 T value = list [k];
    67.                 list [k] = list [n];
    68.                 list [n] = value;
    69.             }
    70.         }
    71.     }
     
  32. voladorfly

    voladorfly

    Joined:
    Sep 22, 2016
    Posts:
    15
    I think should add a piece of code, to limit the number of points matching. Not all points back?
    List<Point> contoursQueryList1= new List<Point> ();
    for (int i=0; i<n; i++) {
    contoursQueryList1.Add (contoursQueryList [i++]);
    }
    Do you think is that right?
     
  33. voladorfly

    voladorfly

    Joined:
    Sep 22, 2016
    Posts:
    15
    Estimate the error estimate when can repair?
     
  34. rahmathidayat

    rahmathidayat

    Joined:
    Oct 2, 2016
    Posts:
    1
    Hi Enox.
    I've been trying out the Augmented Reality sample, and I found that it's not very accurate in several cases.
    I'm not very good in C# nor have a deep understanding c#, do you have any best practice methodology that I could follow?
    Thank you,
     
  35. EnoxSoftware

    EnoxSoftware

    Joined:
    Oct 29, 2014
    Posts:
    1,564
    This code is correct.
     
  36. EnoxSoftware

    EnoxSoftware

    Joined:
    Oct 29, 2014
    Posts:
    1,564
    Until the official OpenCV Java wrapper is fixed, it is difficult to fix this problem.
     
  37. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    1,085
    For WebGL, is it possible to call JavaCV from Unity instead of using the plugin system?
     
  38. EnoxSoftware

    EnoxSoftware

    Joined:
    Oct 29, 2014
    Posts:
    1,564
    I think that probably it is impossible.
     
  39. ina

    ina

    Joined:
    Nov 15, 2010
    Posts:
    1,085
  40. anthony-deschamps

    anthony-deschamps

    Joined:
    Oct 5, 2016
    Posts:
    1
    I can confirm that this plugin works on the actual Hololens device as well. Note, however, that the opencv_contrib modules are not yet available for UWP.

    Are there plans to provide UWP support for contribution modules in the near future? In particular, I'd really like to be able to use ArCuo.
     
  41. Quiet-Pixel

    Quiet-Pixel

    Joined:
    Aug 23, 2013
    Posts:
    48
    Do your DLL and C# scripts forward CV_Error messages from inside of OpenCV to the Unity debug panel? If not, is there a way to find out the last error message thrown inside of OpenCV itself?
     
  42. EnoxSoftware

    EnoxSoftware

    Joined:
    Oct 29, 2014
    Posts:
    1,564
    UWP build of "OpenCV for Unity" has based on this repository.
    https://blogs.msdn.microsoft.com/lucian/2015/11/27/opencv-building-uwp-binaries/
    opencv_contrib modules are not supported in this repository.
    Please wait until the UWP build is supported by the official OpenCV repository.
     
  43. EnoxSoftware

    EnoxSoftware

    Joined:
    Oct 29, 2014
    Posts:
    1,564
  44. voladorfly

    voladorfly

    Joined:
    Sep 22, 2016
    Posts:
    15
    You can ask a question?How to create the shapecontext emgucv for unity?Its shapecontext this method should be no errors?
     
  45. voladorfly

    voladorfly

    Joined:
    Sep 22, 2016
    Posts:
    15
    Any image retrieval based on shape sample, or can you provide some idea?
    @EnoxSoftware
     
  46. EnoxSoftware

    EnoxSoftware

    Joined:
    Oct 29, 2014
    Posts:
    1,564
  47. paul_h

    paul_h

    Joined:
    Sep 13, 2014
    Posts:
    29
    Hi Enox,
    I am planning to build an interactive floor rig using Unity and OpenCVfor unity, and I'm just preparing my shopping list.
    My dream would be to set up the whole thing to run on a Raspberry Pi3.
    In your website you confirm you have tested OpenCV for Unity on Pi2, did you also test it on Pi3?
    If you or (someboby else) have tried this setup.. how was it performance-wise?
    I've read some scary news about Pi3's performance under Windows10, perhaps it's a good idea to stick to android?
    Thanks!
     
  48. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    Hi, I'm interested in this plugin, specifically the VideoWriter. Do you have sample code demonstrating writing a video with Texture2D's? Does this feature work on all platforms?
     
  49. EnoxSoftware

    EnoxSoftware

    Joined:
    Oct 29, 2014
    Posts:
    1,564
    I haven't tested on Raspberry Pi3.
    Since OpenCVforUnity's SampleScene is not a satisfactory performance in Raspberry Pi2, the creation of interactive app might be difficult.
     
  50. EnoxSoftware

    EnoxSoftware

    Joined:
    Oct 29, 2014
    Posts:
    1,564
    VideoWriter class works on Windows and Mac platform.
    https://enoxsoftware.com/opencvforunity/documentation/support-modules/
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using OpenCVForUnity;
    4. namespace OpenCVForUnitySample
    5. {
    6.     /// <summary>
    7.     /// VideoCapture sample.
    8.     /// </summary>
    9.     public class VideoWriterSample : MonoBehaviour
    10.     {
    11.         private int fps = 10;
    12.         private double frameWidth = 768;
    13.         private double frameHeight = 576;
    14.         VideoCapture capture;
    15.         VideoWriter writer;
    16.         Mat rgbaMat;
    17.         Texture2D texture;
    18.      
    19.         // Use this for initialization
    20.         void Start ()
    21.         {
    22.          
    23.          
    24.             rgbaMat = new Mat ();
    25.          
    26.             #if UNITY_PRO_LICENSE || ((UNITY_ANDROID || UNITY_IPHONE) && !UNITY_EDITOR) || UNITY_5
    27.          
    28.          
    29.             capture = new VideoCapture ();
    30.             capture.open(Utils.getFilePath("768x576_mjpeg.mjpeg"));
    31.          
    32.             if(capture.isOpened()){
    33.                 Debug.Log ("capture.isOpened() true");
    34.             }else{
    35.                 Debug.Log ("capture.isOpened() false");
    36.             }
    37.             #endif
    38.             Debug.Log("format: " + capture.get(8));
    39.             Debug.Log("preview format: " + capture.get(Videoio.CV_CAP_PROP_PREVIEW_FORMAT));
    40.             Debug.Log("CAP_PROP_POS_MSEC: " + capture.get(Videoio.CAP_PROP_POS_MSEC));
    41.             Debug.Log("CAP_PROP_POS_FRAMES: " + capture.get(Videoio.CAP_PROP_POS_FRAMES));
    42.             Debug.Log("CAP_PROP_POS_AVI_RATIO: " + capture.get(Videoio.CAP_PROP_POS_AVI_RATIO));
    43.             Debug.Log("CAP_PROP_FRAME_COUNT: " + capture.get(Videoio.CAP_PROP_FRAME_COUNT));
    44.             Debug.Log("CAP_PROP_FPS: " + capture.get(Videoio.CAP_PROP_FPS));
    45.             Debug.Log("CAP_PROP_FRAME_WIDTH: " + capture.get(Videoio.CAP_PROP_FRAME_WIDTH));
    46.             Debug.Log("CAP_PROP_FRAME_HEIGHT: " + capture.get(Videoio.CAP_PROP_FRAME_HEIGHT));
    47.             texture = new Texture2D((int)(frameWidth), (int)(frameHeight), TextureFormat.RGBA32, false);
    48.             gameObject.GetComponent<Renderer>().material.mainTexture = texture;
    49.          
    50.             #if (UNITY_ANDROID || UNITY_IPHONE || UNITY_WP_8_1) && !UNITY_EDITOR
    51.             gameObject.transform.eulerAngles = new Vector3 (0, 0, -90);
    52.             #endif
    53.          
    54.             gameObject.transform.localScale = new Vector3((float)frameWidth, (float)frameHeight, 1);
    55.          
    56.          
    57.             #if (UNITY_ANDROID || UNITY_IPHONE || UNITY_WP_8_1) && !UNITY_EDITOR
    58.             Camera.main.orthographicSize = (float)frameWidth / 2;
    59.             #else
    60.             Camera.main.orthographicSize = (float)frameHeight / 2;
    61.             #endif
    62.          
    63.          
    64.             double ex = capture.get(Videoio.CAP_PROP_FOURCC);
    65.             //char EXT[] = {ex & 0XFF , (ex & 0XFF00) >> 8,(ex & 0XFF0000) >> 16,(ex & 0XFF000000) >> 24, 0};
    66.             Debug.Log("CAP_PROP_FOURCC: " + (char)((int)ex & 0XFF) + (char)(((int)ex & 0XFF00) >> 8) + (char)(((int)ex & 0XFF0000) >> 16) + (char)(((int)ex & 0XFF000000) >> 24));
    67.             writer = new VideoWriter();
    68.             Debug.Log ("persistentDataPath "+ Application.persistentDataPath);
    69. #if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
    70.             writer.open(Application.persistentDataPath + "/output.mjpeg", VideoWriter.fourcc('a','v','c','1'), fps, new OpenCVForUnity.Size(frameWidth,frameHeight));
    71. #else
    72.             writer.open(Application.persistentDataPath + "/output.mjpeg", VideoWriter.fourcc('M','J','P','G'), fps, new OpenCVForUnity.Size(frameWidth,frameHeight));
    73. #endif
    74.             if(writer.isOpened()){
    75.                 Debug.Log ("writer.isOpened() true");
    76.             }else{
    77.                 Debug.LogError ("writer.isOpened() false");
    78.             }
    79.          
    80.         }
    81.      
    82.         // Update is called once per frame
    83.         void Update ()
    84.         {
    85.             //error PlayerLoop called recursively! on iOS.reccomend WebCamTexture.
    86.             if (capture.grab ()) {
    87.              
    88.                 #if UNITY_PRO_LICENSE || ((UNITY_ANDROID || UNITY_IPHONE) && !UNITY_EDITOR) || UNITY_5
    89.                 capture.retrieve (rgbaMat, 0);
    90.                 writer.write(rgbaMat);
    91.                 Imgproc.cvtColor (rgbaMat, rgbaMat, Imgproc.COLOR_BGR2RGB);
    92.                 #endif
    93.              
    94.                 //                                Debug.Log ("Mat toString " + rgbaMat.ToString ());
    95.              
    96.                 Utils.matToTexture2D (rgbaMat, texture);
    97.              
    98.                 gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
    99.              
    100.             }
    101.         }
    102.      
    103.         void OnDestroy ()
    104.         {
    105.             capture.release ();
    106.             writer.release();
    107.         }
    108.      
    109.         public void OnBackButton ()
    110.         {
    111.             Application.LoadLevel ("OpenCVForUnitySample");
    112.         }
    113.     }
    114.  
    115. }
     
    User340 likes this.