Search Unity

Hololens Photo Capturing Failing

Discussion in 'Editor & General Support' started by cbolles, Aug 6, 2018.

  1. cbolles

    cbolles

    Joined:
    Jun 25, 2018
    Posts:
    3
    I am trying to take a photo from the hololens, but I run into a few errors when I attempt it. Im following the tutorial located here. The code runs fine on an emulator, but as soon as I try to run the code on an emulator, I get errors.

    My code is below
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Linq;
    3. using UnityEngine.XR.WSA.WebCam;
    4. using System.Collections.Generic;
    5. using System;
    6. using Unity3dAzure.WebSockets;
    7.  
    8. public class CapturePhoto : MonoBehaviour {
    9.   PhotoCapture photoCaptureObject = null;
    10.   Texture2D targetTexture;
    11.  
    12.   [SerializeField]
    13.   public UnityWebSocket webSocket;
    14.  
    15.   private byte[] imgData;
    16.   private int width;
    17.   private int height;
    18.  
    19.   public void CaptureImage() {
    20.     Debug.Log("Get texture and resolution");
    21.     Resolution cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First();
    22.     targetTexture = new Texture2D(cameraResolution.width, cameraResolution.height);
    23.     // Create a PhotoCapture object
    24.     PhotoCapture.CreateAsync(false, delegate (PhotoCapture captureObject) {
    25.       Debug.Log("Starting photo capture");
    26.       photoCaptureObject = captureObject;
    27.       CameraParameters cameraParameters = new CameraParameters();
    28.       cameraParameters.hologramOpacity = 0.0f;
    29.       cameraParameters.cameraResolutionWidth = cameraResolution.width;
    30.       cameraParameters.cameraResolutionHeight = cameraResolution.height;
    31.       cameraParameters.pixelFormat = CapturePixelFormat.BGRA32;
    32.       width = cameraResolution.width;
    33.       height = cameraResolution.height;
    34.       Debug.Log("Begin taking photo");
    35.       // Activate the camera
    36.       photoCaptureObject.StartPhotoModeAsync(cameraParameters, delegate (PhotoCapture.PhotoCaptureResult result) {
    37.         // Take a picture
    38.         Debug.Log("here");
    39.         photoCaptureObject.TakePhotoAsync(OnCapturedPhotoToMemory);
    40.       });
    41.     });
    42.   }
    43.  
    44.   void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame) {
    45.     Debug.Log("Converting photo to byte");
    46.     Texture2D tex = new Texture2D(width, height, TextureFormat.BGRA32, false);
    47.     Debug.Log(photoCaptureFrame);
    48.     photoCaptureFrame.UploadImageDataToTexture(tex);
    49.     imgData = tex.EncodeToPNG();
    50.  
    51.  
    52.     // Deactivate the camera
    53.     photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode);
    54.   }
    55.  
    56.   void OnStoppedPhotoMode(PhotoCapture.PhotoCaptureResult result) {
    57.     Debug.Log("Sending photo");
    58.     // Shutdown the photo capture resource
    59.     photoCaptureObject.Dispose();
    60.     photoCaptureObject = null;
    61.     webSocket.SendText(Convert.ToBase64String(imgData));
    62.   }
    63. }
    64.  
    The first error,
    Code (csharp):
    1.  
    2. Exception thrown at 0x7723C7D2 (KernelBase.dll) in UnityWebSocketDemo.exe: WinRT originate error - 0xC00DABE0 : 'No capture devices are available.'.
    3.  
    4. Exception thrown at 0x127887B8 (UnityPlayer.dll) in UnityWebSocketDemo.exe: 0xC0000005: Access violation reading location 0x00000030.
    I can get pay the error sometimes by uninstalling my app before re uploading the app then manually allowing access to the microphone. Both webcam and microphone are enabled in player settings. The error comes just after the Debug.Log("Here").

    If I get past the first error I get

    Code (csharp):
    1. NullReferenceException: Object reference not set to an instance of an object.
    2.    at CapturePhoto.OnCapturedPhotoToMemory(PhotoCaptureResult result, PhotoCaptureFrame photoCaptureFrame)
    3.    at UnityEngine.XR.WSA.WebCam.PhotoCapture.InvokeOnCapturedPhotoToMemoryDelegate(OnCapturedToMemoryCallback callback, Int64 hResult, IntPtr photoCaptureFramePtr)
    4.    at UnityEngine.XR.WSA.WebCam.PhotoCapture.$Invoke10InvokeOnCapturedPhotoToMemoryDelegate(Int64 instance, Int64* args)
    5.    at UnityEngine.Internal.$MethodUtility.InvokeMethod(Int64 instance, Int64* args, IntPtr method)
    Not sure what is causing the second error, but it only happens on the actual hololens not the emulator.
    I have tried Unity 2018.1.6 and 2018.2.2. Also new to the forum so sorry if I'm going about asking about my error wrong.
     
  2. njssnjss

    njssnjss

    Joined:
    Aug 18, 2013
    Posts:
    2
    Same problem here.
    It is almost like the new unity version cannot access the setting of the Hololens camera.
    I also tried this example:https://docs.microsoft.com/en-us/wi...-302#chapter-6--create-the-imagecapture-class
    and it is even worst...I cannot pass this part:
    Resolution cameraResolution = PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).Last();

    Just hangs, no errors...cannot return from trying to get the camera resolutions...

    Any new ideas?
     
  3. cbolles

    cbolles

    Joined:
    Jun 25, 2018
    Posts:
    3
    I can get past the creation of the Resolution object, where I get the error is at photoCaptureObject.TakePhotoAsync(OnCapturedPhotoToMemory); Are you running in an emulator or on an actual hololens? I ask because the emulator with the newest version of Unity got hung up around there, but the actual hololens had no trouble at that point.
     
  4. cbolles

    cbolles

    Joined:
    Jun 25, 2018
    Posts:
    3
    So I found out what I did wrong.
    1. Make sure in the build settings to set Target Device to HoloLens
    2. Make sure to enable Virtual Reality Support under the XR Settings in the Player Settings
    I still think the returned error is odd since the user is still prompted to give the program access to the webcam and that the errors in the emulator do not match up with the errors in the actual HoloLens.
     
  5. njssnjss

    njssnjss

    Joined:
    Aug 18, 2013
    Posts:
    2
    Hi, no I cannot at all access the camera in Hololens...but I am using Unity 2018.2f1 :(
    Everything from vuforia and MR toolkit works on the new version except the access to the camera...I wonder i f this is a problem in unity or a problem with the latest updates on the Hololens.
     
  6. create_at

    create_at

    Joined:
    Dec 23, 2016
    Posts:
    1
    Hi, I have the same problem, can't access the Hololens camera at all.

    Please let me know when you found a solution!
    Thanks!
     
  7. rafaelsistrade

    rafaelsistrade

    Joined:
    Jan 29, 2018
    Posts:
    68
    Hello,
    I have the same problem, in the other version of unity i was all good, but now it's not working.

    The only thing i update is unity to version 2018.2.5f1.

    Gonna update the Hololens tomorrow and unity have a new update to version 2018.2.6f1.

    Hope this updates fix this problem.
     
  8. rafaelsistrade

    rafaelsistrade

    Joined:
    Jan 29, 2018
    Posts:
    68
    It keeps on crashing.
    Here it is the crash log.
    Bug report is number: 1076629. Hope they fix this soon, because it's impossible to work with Hololens without the camera!!!! We can't do Computer Vision!!!!!!!!!!!

    This is a critical problem for all the users of Hololens
     

    Attached Files:

    Last edited: Aug 31, 2018
  9. gwendalbroudinpro

    gwendalbroudinpro

    Joined:
    Sep 13, 2018
    Posts:
    6
  10. rafaelsistrade

    rafaelsistrade

    Joined:
    Jan 29, 2018
    Posts:
    68