Search Unity

NatDevice - Media Device API

Discussion in 'Assets and Asset Store' started by Lanre, Dec 17, 2015.

?

Should we add exposure controls in v1.3? This means dropping support for iOS 7

Poll closed Jun 10, 2016.
  1. Yes

    9 vote(s)
    75.0%
  2. No

    3 vote(s)
    25.0%
  1. Cromfeli

    Cromfeli

    Joined:
    Oct 30, 2014
    Posts:
    202
    "By Android C++, I'm referring to the C++ code we wrote ourselves. It is very extensive, and will require special instructions for handling once we include it. We aren't including it in v1.1 because we have to investigate whether we can squeeze out more performance on OpenGLES3 devices when performing a readback by using Pixel Buffer Objects"

    Edit: Good to hear that sources are coming!
     
    Last edited: Mar 22, 2016
  2. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    We just discovered an issue when building NatCam on iOS 64 bit devices in XCode 7: linker errors. As a result, we are uploading NatCam v1.0.1 today with the issue fixed, and with iOS and Android Java sources. Work still continues on v1.1
     
  3. ra_rasmus

    ra_rasmus

    Joined:
    May 23, 2013
    Posts:
    4
    Hey, kudos on the plugin. Looks to be what everybody needs. :)

    I was wondering, do you happen to know whether NatCam would work together with the Vuforia Augmented Reality plugin for Unity?

    Vuforia lacks the feature to access the physical device camera and zoom, which is why I found NatCam.
     
  4. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Hi @snotts I don't think it is possible. NatCam can supply the camera frames a Texture2D or byte array but I don't think Vuforia allows you to supplement the camera input.

    In the later weeks, we are planning to have AR functionality to work with OpenCVForUnity.
     
  5. ra_rasmus

    ra_rasmus

    Joined:
    May 23, 2013
    Posts:
    4
    Ok, that's a shame. Thanks for the quick reply though, @Lanre :)
     
  6. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Just sent v1.1 in for review. It sure was a busy weekend.
     
  7. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    NatCam v1.1 should be out soon. We are waiting for Asset Store review. Here is some information about the new version:

    FEATURES:
    -FocusMode enum. Now you can specify the active camera's focus mode from one of the four FocusModes: AutoFocus, TapToFocus, HybridFocus, and Off.
    -Orientation support. NatCam now supports all orientations. The preview will always be correctly rotated on all orientations.
    -Native Sources. iOS sources and Android Java sources are now included. Instructions on how to compile both are also included. Android C++ sources will be coming in the next release.
    -Native Plugin Callback for access to the camera preview data in native C-based code (C, C++, Objective-C, and C#). A code example for how to do this is included in the documentation (under NatCam>NatCamNativePluginUpdateEvent).
    -Torch while preview is running.

    IMPROVEMENTS:
    -The documentation is now very rich in code examples.
    -There is now documentation on the OpenCV support API (under NatCam>PreviewMatrix).
    -Added ResolutionPreset.Medium which defaults to 640x480 (VGA).

    FIXES:
    -Fixed iOS front camera preview horizontal mirroring.


    The documentation for NatCam v1.1 can be found here.
     
    Cromfeli likes this.
  8. Cromfeli

    Cromfeli

    Joined:
    Oct 30, 2014
    Posts:
    202
    Lanre, looking super good!
     
  9. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    We just realized a critical issue with v1.1 where the preview might not work when setting FocusMode on Android. The fix has been submitted to Asset Store review, but the patch is attached to this post. Sorry for the inconvenience.
     

    Attached Files:

    Last edited: Mar 22, 2016
    Cromfeli likes this.
  10. shoveltoolsinc

    shoveltoolsinc

    Joined:
    Aug 16, 2015
    Posts:
    12
    Natcam crashes on Android with Multithreaded Rendering checked in PlayerSettings FYI, at least 1.0 did.
     
  11. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Thank you for bringing this to my attention. I PM'ed you.
     
  12. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    We are currently working on NatCam v1.2 with numerous fixes including:

    -EXC_BAD_ACCESS crash on start on any iOS version less than iOS 9.
    -Readable preview causing preview not to show up on some Android devices.
    -Jagged scan lines when capturing more than one photo on Android.
    -Editor camera preview being horizontally flipped on OSX.
    -iOS build linker errors when Accelerate.framework and AssetsLibrary.framework haven't been added.
    -SetFocus crashing on iOS devices which don't support focusing
    -CapturePhoto now returning a photo when a new scene has been loaded

    We plan to complete and submit it on Thursday. Please hold tight, and if you need early access (or you are interested in being a beta tester), contact me at olokobayusuf@gmail.com. Thank you!
     
    Last edited: Mar 28, 2016
    FaberVi likes this.
  13. Packedbox

    Packedbox

    Joined:
    Jun 20, 2013
    Posts:
    20
    Hi Lanre, the preview is rotated 180° on Nexus 5x device, I think its a common problem since the sensors is mounted upside down ( same issue with Unity WebCamTexture )
     
    FaberVi likes this.
  14. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Hi @Packedbox, thank you for bringing this to my attention. It is indeed because the sensor is flipped. We will add a fix.
     
  15. ThomasVandenberghe

    ThomasVandenberghe

    Joined:
    Feb 28, 2014
    Posts:
    22
    The EXC_BAD_ACCESS I'm having (
    ProductName was compiled with optimization - stepping may behave oddly; variables may not be available.) is that the same one as the one you're fixing in v1.2?

    Also, for users that are having the linker errors in iOS due to Accelerate and AssetsLibrary frameworks can perm enable them by going to Natcam -> Plugins -> iOS -> libNatCam.a and under Platform settings -> Rarely used frameworks, clicking those 2 frameworks. I don't know if you could enable this when submitting to the asset store, or if those settings aren't included in the package.
     
  16. ThomasVandenberghe

    ThomasVandenberghe

    Joined:
    Feb 28, 2014
    Posts:
    22
    I'm also having issues taking photo's when switching scenes. It seems on iOS NatCam.CapturedPhoto becomes null after switching a scene (this does work correct on my mac). Any idea what this might be? Otherwise I'm going to save the photo in my own scripts and use it from there.

    Here are the xcode logs
    2016-03-25 11:56:14.670 ProductName[396:79912] NatCam: Captured photo
    NatCam Logging: Received captured photo from iOS Native
    UnityEngine.Logger:Log(LogType, Object)
    UnityEngine.Debug:Log(Object)
    NatCamU.NatCamNativeInterface:UpdatePhoto()


    (Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 37)

    Photo resolution : 720 x 1280
    UnityEngine.Logger:Log(LogType, Object)
    UnityEngine.Debug:Log(Object)
    TakePicture:NatCam_OnCapturedPhotoEvent(Texture2D)

    (Filename: /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineDebugBindings.gen.cpp Line: 37)

    Unloading 3 Unused Serialized files (Serialized files now loaded: 0)
    UnloadTime: 7.770625 ms

    Unloading 3 unused Assets to reduce memory usage. Loaded Objects now: 252.
    Total: 5.155916 ms (FindLiveObjects: 0.068708 ms CreateObjectMapping: 0.036458 ms MarkObjects: 3.195708 ms DeleteObjects: 1.851000 ms)

    Other scene is loaded here

    SendMessage: object NatCamHelper not found!
    OpenGLES error 0x0502 in .../Classes/Unity/CMVideoSampling.mm:57
    SendMessage: object NatCamHelper not found!
     
    Last edited: Mar 25, 2016
  17. jharper

    jharper

    Joined:
    Jul 9, 2015
    Posts:
    3
    I am having a problem with iOS and iOS only, works great in Android, but the camera is delayed. When I move the phone it takes about a second to catch up. Please help!
     
  18. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Hello guys. Sorry I have been out of comms for a while. On Friday, my workstation's hard drive crashed unexpectedly, so I spent the whole day in data recovery (everything was recovered thankfully). A new SSD gets delivered on Wednesday so till then I can't work on NatCam, and am behind on my emails.

    @ThomasVandenberghe Thank you for bringing the issue to my attention. The EXC_BAD_ACCESS you are facing is not because of NatCam, see here. Also, the framework enabling in Unity Editor doesn't work for me, i think it's a bug with my version (5.3.1f1). It goes off once I click Apply. The captured photo issue will be fixed in v1.2; it isn't supposed to do that but we will make sure.
     
  19. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Hi @jharper . What device does this happen on? And have you tested this with PreviewType.NonReadable? Thank you for letting me know of it.
     
  20. jharper

    jharper

    Joined:
    Jul 9, 2015
    Posts:
    3
    @Lanre, I am using an iPhone 6s and I just tried the PreviewType.NonReadable and it is still lagging. About a second to catch up. Thanks.
     
  21. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Hi @jharper Sorry for the late response. I am prepping for resuming work tomorrow. I PM'ed you.
     
  22. jenosys

    jenosys

    Joined:
    Nov 18, 2014
    Posts:
    1
    crash report.

    Android build with multithreaded rendering option will crash when call NatCam.Stop()
     
  23. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Hi @jenosys . I just PM'ed you, it has been added to the list for v1.2.
     
  24. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    I'm back! Or more specifically, my workstation.
     
    Cromfeli likes this.
  25. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Hi guys. NatCam v1.2 is meant to fix a lot of issues that eluded us during testing for v1.0 and v1.1, but have inevitably been surfaced. Below is the list. Issues marked with + have been fixed, while issues marked with - will be fixed.

    NatCam v1.2 Feature List:
    + Added the NatCam rendering pipeline to NatCam iOS.
    + Fixed the camera preview in the OSX editor being horizontally flipped.
    + Fixed the captured photo in the OSX editor being horizontally flipped.
    + Fix camera preview lagging on iOS.
    + OpenCV PreviewMatrix now updates from the native pixel buffer. This gives some memory savings and performance increase.
    + Captured photo on iOS is now ARGB32. This means you can use Get/SetPixels(s), EncodeToJPG/PNG, and Apply.
    + Fix "Error Creating Optimization Context" when using Readable preview on Galaxy S6 and Galaxy Tab.
    + Fix rare scan line jitter when NatCam corrects padding with Readable preview on some Android devices.
    + Added ALLOCATE_NEW_CAPTURED_PHOTO_TEXTURES macro for optimizing memory usage. Comment it to write over an already-allocated photo texture instead of allocating a new one each time.
    + Added ALLOCATE_NEW_PREVIEW_FRAME_TEXTURES macro for optimizing memory usage. Comment it to write over an already-allocated preview frame texture instead of allocating a new one each time NatCam.PreviewFrame is called.
    + Added partial support for SetDebugMode() on iOS.
    + Fixed Android crash on Stop().
    + Fixed error when scene is changed--something along the lines of "SendMessage: NatCamHelper not found".
    + Added editor-serialized variables for Unitygram. Now, you can set Unitygram's variables from the editor instead of code.
    + Fixed linker errors in iOS when Accelerate.framework and AssetsLibrary.framework have not been linked. Now, you don't have to do anything; NatCam automatically adds its required frameworks.
    + Deprecated OPTIMIZATION_USE_NATIVE_BUFFER macro, and as a result, direct support for Unity 5.2 has been stopped.
    + NatCamPreviewUIPanelScaler and NatCamPreviewUIPanelZoomer have been renamed to NatCamPreviewScaler and NatCamPreviewZoomer respectively.
    + Fixed Stop() being ignored when Pause() is called immediately before.
    + Fixed NatCamPreviewScaler not correctly scaling HD and FullHD preview on iOS.
    - Fix camera preview being rotated 180 degrees in landscape mode on Nexus 5x due to camera sensor being mounted upside down.
    - Fix slow camera switching on Android and iOS.
    - Fix preview not working after switching camera more than once on Android and iOS.
     
    Last edited: Apr 12, 2016
    Cromfeli likes this.
  26. ElectroSphere

    ElectroSphere

    Joined:
    Nov 17, 2012
    Posts:
    50
    Nice !! Can you use openCV with VR camera mode ? (cardboard for example)
     
  27. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Hi @ElectroSphere . I have not tested this personally, but it should be very much possible. In fact, I had a developer use NatCam's readable preview with Google Cardboard. We are looking at creating more examples and uploading them to Github, and this will be a good one to include.
     
  28. Markbotics

    Markbotics

    Joined:
    Apr 1, 2016
    Posts:
    1
    Hi, are there some examples or tutorials?

    Especially examples with grabbing the video frame to pass to native code (C for me). I see the NatCamNativePluginCallback callback on https://abfa31284539dde9981c6be9d12.../host/0B5rPOCTjzOsgYVdhZkFaZmxzakE/index.html

    I have my own C image processing code and simply want an unsigned char* pointer, ideally greyscale (1 byte per pixel). I've wrote my own ios and Android versions before, but now want to make it work with Unity and try be platform independent ideally. Can Natcam give me the raw YUV420 feed? Since for greyscale, the first width*height bytes are simply the greyscale image, and to convert it to RGB and then back to greyscale is a lot of unnecessary processing.

    I'm making some AR apps too, where I want a background live video. Does Natcam and Unity allow Unity to just be transparent to the fast video preview underneath the app'store layer? That approach gives less lag for video, and doesnt require format conversion from YUV to RGB for textures, but makes content fall behind video. Alternatively using the traditional method of a textured quad containing the video at the back of the virtual camera frustum allows the video to line up with the augmented game objects, i.e. they have the same latency.

    Also, can I get hold of the Cardboard demo?

    Lots of questions... thanks in advance for answers on any.
     
  29. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Hi @Markbotics. There is some example code in the documentation. Here is the code:
    Code (CSharp):
    1. //--------------Unity Code----------------
    2.  
    3. //Define the C++ method in a library, libNativeCodeExample.so
    4. [DllImport("NativeCodeExample")]
    5. static extern void PerformCameraPreviewOperationNatively (IntPtr previewDataPointer, int previewDataSize);
    6.  
    7. //In initialization code
    8. void Start () {
    9.     //Initialize NatCam
    10.     NatCam.Initialize();
    11.     //Register for the native pixel buffer update event
    12.     NatCam.NatCamNativePluginUpdateEvent += PerformCameraPreviewOperationNatively;
    13.     //Play the preview
    14.     NatCam.Play();
    15. }
    16.  
    17.  
    18. //--------------C/C++/Obj-C Code----------------
    19.  
    20. extern "C" void PerformCameraPreviewOperationNatively (void* previewDataPointer, int previewDataSize) {
    21.     //Copy out the data //Don't worry, this is pretty fast even on low end phones and high resolution previews
    22.     unsigned char * data = new unsigned char[previewDataSize]; //In C, use malloc
    23.     memcpy(data, previewDataPointer, previewDataSize);
    24.     //Do stuff with the preview data copy //Don't forget to keep track of the size of the buffer
    25.     DoAnotherThingWithThePreviewData(data);
    26. }
    Unfortunately, NatCam doesn't provide access to YUV data on Android. On iOS, we get BGRA data so that we don't have to perform any conversions. But considering that it might provide a memory optimization, we are looking into changing the rendering pipeline on iOS to support YUV processing. We are also considering a class within NatCam that will serve as an interface for lower level operations like getting access to Y data, and disabling rendering.

    Regarding your question on whether NatCam can make Unity transparent, this is not possible. NatCam serves as an API for Unity and as such, we don't plan on overriding anything that Unity does. NatCam will simply provide the camera preview as a texture (with virtually no lag on even high resolution previews) for your use.

    The cardboard demo will be the first thing we work on once v1.2 has been submitted. We will be uploading NatCam examples to Github. I planned to roll out the 3rd beta today but I discovered that a lot more needed to be done than I thought. I might still be able to roll it out this evening.
     
  30. Narlix

    Narlix

    Joined:
    Jul 3, 2009
    Posts:
    111
    Just a FYI, NatCam and Unity Cloud Build seem to be clashing

    2329: [xcode] export IPHONEOS_DEPLOYMENT_TARGET=9.1
    2330: [xcode] ld: warning: object file (/BUILD_PATH/sperosophia.vrdesk.vrdesk-ios/temp.UdqNXO/Libraries/NatCam/Plugins/iOS/libNatCam.a(NatCam.o)) was built for newer iOS version (9.2) than being linked (9.1)
    2331: [xcode] ld: warning: arm64 function not 4-byte aligned: ltmp0 from /BUILD_PATH/sperosophia.vrdesk.vrdesk-ios/temp.UdqNXO/Libraries/libiPhone-lib.a(PLCrashAsyncThread_current-5565F35D16E4818B.o)
    2332: [xcode] ld: warning: arm64 function not 4-byte aligned: _plcrash_async_thread_state_current from /BUILD_PATH/sperosophia.vrdesk.vrdesk-ios/temp.UdqNXO/Libraries/libiPhone-lib.a(PLCrashAsyncThread_current-5565F35D16E4818B.o)
    2333: [xcode] Undefined symbols for architecture arm64:
    2334: [xcode] "_ALAssetsGroupPropertyName", referenced from:
    2335: [xcode] ___30-[NatCam SaveImageToAppAlbum:]_block_invoke267 in libNatCam.a(NatCam.o)
    2336: [xcode] "_OBJC_CLASS_$_ALAssetsLibrary", referenced from:
    2337: [xcode] objc-class-ref in libNatCam.a(NatCam.o)
    2338: [xcode] "_vImageRotate90_ARGB8888", referenced from:
    2339: [xcode] -[NatCam RotateBuffer:Width:Height:Size:] in libNatCam.a(NatCam.o)
    2340: [xcode] "_vImageHorizontalReflect_ARGB8888", referenced from:
    2341: [xcode] -[NatCam RotateBuffer:Width:Height:Size:] in libNatCam.a(NatCam.o)
    2342: [xcode] ld: symbol(s) not found for architecture arm64
    2343: [xcode] clang: error: linker command failed with exit code 1 (use -v to see invocation)
    2344: [xcode] Ld build/Unity-iPhone.build/Release-iphoneos/Unity-iPhone.build/Objects-normal/armv7/vrdesk normal armv7
    2345: [xcode] cd /BUILD_PATH/sperosophia.vrdesk.vrdesk-ios/temp.UdqNXO
    2346: [xcode] export IPHONEOS_DEPLOYMENT_TARGET=9.1
    2347: [xcode] ld: warning: object file (/BUILD_PATH/sperosophia.vrdesk.vrdesk-ios/temp.UdqNXO/Libraries/NatCam/Plugins/iOS/libNatCam.a(NatCam.o)) was built for newer iOS version (9.2) than being linked (9.1)
    2348: [xcode] Undefined symbols for architecture armv7:
    2349: [xcode] "_ALAssetsGroupPropertyName", referenced from:
    2350: [xcode] ___30-[NatCam SaveImageToAppAlbum:]_block_invoke267 in libNatCam.a(NatCam.o)
    2351: [xcode] "_OBJC_CLASS_$_ALAssetsLibrary", referenced from:
    2352: [xcode] objc-class-ref in libNatCam.a(NatCam.o)
    2353: [xcode] "_vImageRotate90_ARGB8888", referenced from:
    2354: [xcode] -[NatCam RotateBuffer:Width:Height:Size:] in libNatCam.a(NatCam.o)
    2355: [xcode] "_vImageHorizontalReflect_ARGB8888", referenced from:
    2356: [xcode] -[NatCam RotateBuffer:Width:Height:Size:] in libNatCam.a(NatCam.o)
    2357: [xcode] ld: symbol(s) not found for architecture armv7
    2358: [xcode] clang: error: linker command failed with exit code 1 (use -v to see invocation)
    2359: [xcode] ** BUILD FAILED **

    Might be something to look in to.

    with armv7, mono
    and with Universal, il2cpp
     
  31. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Hi @Narlix . I'm not familiar with the Unity Cloud Build pipeline, but you need to link Accelerate.framework and AssetsLibrary.framework to the Xcode project that Unity generates. We have added a fix to it in the v1.2 beta, but we are not sure about Unity Cloud Build.
     
  32. Narlix

    Narlix

    Joined:
    Jul 3, 2009
    Posts:
    111
    Yeah, seems the issue was with unity cloud build, not automatically detecting it, you have to add this script to your project for unity cloud build to build properly.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3. using UnityEditor.Callbacks;
    4. using System.Collections;
    5. using UnityEditor.iOS.Xcode;
    6. using System.IO;
    7.  
    8. public class xcodepost
    9. {
    10.  
    11.     [PostProcessBuild]
    12.     public static void OnPostprocessBuild(BuildTarget buildTarget, string path)
    13.     {
    14.  
    15.         if (buildTarget == BuildTarget.iOS)
    16.         {
    17.             string projPath = path + "/Unity-iPhone.xcodeproj/project.pbxproj";
    18.  
    19.             PBXProject proj = new PBXProject();
    20.             proj.ReadFromString(File.ReadAllText(projPath));
    21.  
    22.             string target = proj.TargetGuidByName("Unity-iPhone");
    23.  
    24.             if (!proj.HasFramework("AssetsLibrary.framework"))
    25.             {
    26.                 proj.AddFrameworkToProject(target, "AssetsLibrary.framework", true);
    27.             }
    28.  
    29.             if (!proj.HasFramework("Accelerate.framework"))
    30.             {
    31.                 proj.AddFrameworkToProject(target, "Accelerate.framework", true);
    32.             }
    33.  
    34.             File.WriteAllText(projPath, proj.WriteToString());
    35.  
    36.  
    37.  
    38.             // Add url schema to plist file
    39.             string plistPath = path + "/Info.plist";
    40.             PlistDocument plist = new PlistDocument();
    41.             plist.ReadFromString(File.ReadAllText(plistPath));
    42.  
    43.             // Get root
    44.             PlistElementDict rootDict = plist.root;
    45.             rootDict.SetBoolean("UIRequiresFullScreen", true);
    46.             plist.WriteToFile(plistPath);
    47.         }
    48.     }
    49. }
     
  33. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Thanks @Narlix .It has been fixed in the new beta v1.2b2.
     
  34. iambrois

    iambrois

    Joined:
    Mar 13, 2013
    Posts:
    5
    Hi @Lanre are the fixes you listed on the Unity Asset Store yet? I'm having some problems regarding switching cameras in Android and having the camera preview play on the editor
     
  35. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Hi @iambrois , there isn't an ETA yet, but I'm looking to complete it by the end of this week. Unity's Asset Store review will take its time to review it. Switching cameras is being worked on, and the editor preview is also up next.
     
  36. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    The only major issues left for the NatCam v1.2 beta are:

    -Slow, unreliable camera switching on iOS and Android
    -Editor camera preview being black

    We have been investigating the preview lag on iOS and have found that this is not a NatCam issue. iOS devices seem to be bottlenecked when displaying high resolution previews, most probably a bandwidth bottleneck.

    We have made some progress on the camera switching on iOS, and have a good idea of implementing a better solution on Android. So thins are looking well for the beta.

    As for the editor preview being black, this is because we tried to implement a RenderTexture to horizontally flip the preview in realtime (because it is horizontally flipped on OSX in our findings). We are reverting the entire thing to what it was in v1.0. We are also renaming NatCam.PreviewTexture to NatCam.PreviewWebCamTexture (and changing the type from Texture to WebCamTexture) to avoid confusion and to provide direct access to the WebCamTexture when using the Fallback interface.

    Planned for the future is switching the preview format on iOS from BGRA32 to YUV and rendering it manually. This is what is done on Android. This will provide numerous advantages, but most significantly:
    -Memory. YUV data is 12 bits per pixel while ARGB/RGBA/BGRA32 data is 32 bits per pixel. Less than half the memory usage than that of full 4 channel data.
    -Speed. Performing any transformations like rotation and scaling on the GPU should be faster. Also, switching cameras will be much faster.

    EDIT:
    -We have discovered that the slow camera switching on iOS is because of the preview orientation step that happens before the preview is sent to Unity. As a result, we will be implementing YUV rendering immediately.
    This will delay v1.2's schedule by almost a week.
    -Also, the camera preview lag on iOS has been fixed. It turned out to be because of video stabilization.
     
    Last edited: Apr 5, 2016
  37. SFXpedror

    SFXpedror

    Joined:
    Dec 17, 2015
    Posts:
    2

    Is there any way you could add support for not auto orienting the camera preview, or at least has the same orientation as WebCamTexture does in all screen orientations?
     
  38. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    @SFXpedror How do you mean? If the preview is not auto-oriented, then if your device is in portrait for instance, the preview will be rotated 90 degrees. Is this what you mean?
     
  39. SFXpedror

    SFXpedror

    Joined:
    Dec 17, 2015
    Posts:
    2
    Yes, basically, have it behave the exact same way as WebCamTexture if I were to somehow disable auto orientation.
     
  40. piacentini

    piacentini

    Joined:
    May 27, 2014
    Posts:
    28
    Hi. I can not find the documentation for PreviewMatrix, listed in a previous message:

    -There is now documentation on the OpenCV support API (under NatCam>PreviewMatrix).

    Can you point me to any docs or samples about integration with OpenCVForIUnity?
     
  41. NKidd

    NKidd

    Joined:
    Sep 16, 2015
    Posts:
    22
    I'm not the author of this package, but I use Natcam .ft OpenCV too

    In OpenCV , you get the Mat of Webcamtexture by Utils.webCamTextureToMat.
    When using Natcam:
    - Open NatCam.cs and uncomment the line that says "//#define NATCAM_OPENCV_SUPPORT". This will allow you to call NatCam.PreviewMatrix which is an OpenCV matrix of the camera preview.
    - Get Mat by: NatCam.PreviewMatrix
    ex:
    Code (CSharp):
    1. public Mat GetMat ()    {
    2.         if (NatCam.PreviewMatrix == null) {
    3.             Debug.Log ("NatCam.PreviewMatrix NULL ");
    4.             return null;
    5.         } else {
    6.             return NatCam.PreviewMatrix;
    7.         }
    8.     }
     
    SMR83 and piacentini like this.
  42. NKidd

    NKidd

    Joined:
    Sep 16, 2015
    Posts:
    22
    I tested on Android Phone: HTC One X and HTC One Mini 2 and get white blank screen. Anyone get the same problem?
     
    piacentini likes this.
  43. sjmellor39

    sjmellor39

    Joined:
    Dec 5, 2013
    Posts:
    1
    Anyone have an issue with a delay in processing images? everytime i turn the camera it takes a second to respond. What am I missing?
     
  44. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Hi guys. @piacentini we will be creating samples immediately after completing NatCam v1.2. To use the OpenCV support wrapper, open NatCam.cs and uncomment this line:
    Code (CSharp):
    1. //#define NATCAM_OPENCV_SUPPORT
    Thanks @NKidd, spot on!
     
    piacentini likes this.
  45. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    @NKidd, Can you send me the logs? Also, call:
    Code (CSharp):
    1. NatCam.SetDebugMode();
    before you call NatCam.Initialize when you are creating the logs; 99% of the time, this will show what exactly is going wrong.
     
  46. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    @sjmellor39 Is this on iOS? If so, there is a fix that you can apply manually.

    On iOS, if you are experiencing lag:
    -Unzip Sources.zip and copy out NatCam.h and NatCam.mm and paste them in Assets>NatCam>Plugins>iOS folder
    -Delete libNatCam.a
    -Build your project to XCode
    -In the project folder directory browser on the left, go to Libraries>NatCam>Plugins>iOS and open NatCam.mm
    -Scroll down to this method:
    Code (CSharp):
    1. - (void)captureOutput:(AVCaptureOutput*)avcaptureoutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection*)connection {
    2. ...
    3. }
    -Delete the first couple of lines that deal with videoStabilization mode.

    Please note that you are advised to wait for the new version (or beta) instead of performing this yourself. Also, for safety, make a backup of libNatCam.a outside your Unity project.

    This issue will be fixed in the new version.
     
    Cromfeli likes this.
  47. Tinus

    Tinus

    Joined:
    Apr 6, 2009
    Posts:
    437
    I've been trying to use NatCam with OpenCVForUnity, but I'm not able to get a basic example to work on my target device (a new Galaxy s7).

    I can successfully run NatCam and show the preview texture as in the Unitygram example. I can successfully run other OpenCV code. I have enabled the #define NATCAM_OPENCV_SUPPORT define.

    Here's my test code: https://gist.github.com/anonymous/345c00b504833641829b0c0d78d7f8a9

    Its goal is simply to initialize NatCam, and in Update get the PreviewMat, convert it to a Texture2D and render it to a quad using an unlit shader. The code runs without exceptions being thrown or anything. The red color that the target texture is initialized with does indeed get overwritten, but all the resulting pixels are completely black.

    Something from initialization process in the log that might be relevant:

    Code (csharp):
    1.  
    2. 04-09 16:39:44.460 27939 27970 D Unity  : NatCam Native: Generated FBO
    3. 04-09 16:39:44.460 27939 27970 D Unity  : Hϖ١m Native: Error creating Optimization Context: EGL_BAD_PARAMETER
    4. 04-09 16:39:44.470 27939 27970 D Unity  : NatCam: Initialized
    5. 04-09 16:39:44.510 27939 27970 I Unity  : NatCam: Set photo resolution: 1280x720
    6.  
    Is there a bug happening somewhere or am I doing something silly?
     
  48. Lanre

    Lanre

    Joined:
    Dec 26, 2013
    Posts:
    3,973
    Hi @Tinus. We have come across this issue and have implemented a fix in the upcoming v1.2b3.

    The beta is taking longer than we expected as a result of YUV-ARGB conversion on iOS.
     
  49. NKidd

    NKidd

    Joined:
    Sep 16, 2015
    Posts:
    22
    @Lanre
    The NatCam.NatCamPreviewStartEvent is not excute. I tried to call it both before and after Initialize but not work...
    Code (CSharp):
    1. NatCam.NatCamPreviewStartEvent += OnFirstFrame;
    2. private void OnFirstFrame() {
    3.         Debug.Log ("On FIRST FRAME");
    4. }
    On Editor, currently I have to wait end of frame and then wait for second to get PreviewMatrix not null.
    Basically, it works well on Editor. But on my test phone, the performance when I use both OpenCV face tracker and Natcam is so not good :(

    Do you have any tip for performance? I tried the lowest resolution...
     
    Last edited: Apr 11, 2016
  50. Alvaro_OneTangoStudio

    Alvaro_OneTangoStudio

    Joined:
    Apr 11, 2016
    Posts:
    3
    @Lanre Alvaro from One Tango Studio here.
    Do you have any idea on when are you releasing v1.2b3?
    We are on the middle of a development for android and some NatCam bugs already reported are messing with our schedule.
    Thanks