2019-07-07 11:26:16 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2019-07-06 22:17:26 +00:00
|
|
|
|
using UnityEngine;
|
|
|
|
|
using Windows.Kinect;
|
|
|
|
|
|
|
|
|
|
namespace VideoInput
|
|
|
|
|
{
|
|
|
|
|
public interface IInfraredCamera
|
|
|
|
|
{
|
|
|
|
|
Texture2D Data { get; }
|
2019-07-07 07:39:46 +00:00
|
|
|
|
|
|
|
|
|
void Update();
|
2019-07-06 22:17:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class InfraredCamera : IInfraredCamera
|
|
|
|
|
{
|
|
|
|
|
public Texture2D Data { get; }
|
|
|
|
|
|
|
|
|
|
private Windows.Kinect.KinectSensor sensor_;
|
|
|
|
|
private readonly InfraredFrameReader reader_;
|
2019-07-07 07:39:46 +00:00
|
|
|
|
private readonly ushort [] irData_;
|
2019-07-06 22:17:26 +00:00
|
|
|
|
|
2019-07-07 11:26:16 +00:00
|
|
|
|
|
2019-07-06 22:17:26 +00:00
|
|
|
|
public InfraredCamera()
|
|
|
|
|
{
|
|
|
|
|
sensor_ = Windows.Kinect.KinectSensor.GetDefault();
|
|
|
|
|
if (sensor_ == null)
|
|
|
|
|
{
|
|
|
|
|
throw new IOException("cannot find Kinect Sensor ");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reader_ = sensor_.InfraredFrameSource.OpenReader();
|
|
|
|
|
|
|
|
|
|
var frameDesc = sensor_.InfraredFrameSource.FrameDescription;
|
|
|
|
|
irData_ = new ushort[frameDesc.LengthInPixels];
|
2019-07-07 11:26:16 +00:00
|
|
|
|
Data = new Texture2D(frameDesc.Width, frameDesc.Height, TextureFormat.R16, false);
|
2019-07-06 22:17:26 +00:00
|
|
|
|
|
|
|
|
|
if (!sensor_.IsOpen)
|
|
|
|
|
sensor_.Open();
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-07 07:39:46 +00:00
|
|
|
|
public void Update()
|
2019-07-06 22:17:26 +00:00
|
|
|
|
{
|
|
|
|
|
if (reader_ == null)
|
|
|
|
|
throw new IOException("Kinect reader not opened");
|
|
|
|
|
|
|
|
|
|
var frame = reader_.AcquireLatestFrame();
|
2019-07-07 07:39:46 +00:00
|
|
|
|
if (frame == null) return;
|
2019-07-06 22:17:26 +00:00
|
|
|
|
|
2019-07-07 11:26:16 +00:00
|
|
|
|
frame.CopyFrameDataToArray(irData_);
|
|
|
|
|
|
|
|
|
|
unsafe
|
|
|
|
|
{
|
|
|
|
|
fixed(ushort* irDataPtr = irData_)
|
|
|
|
|
{
|
|
|
|
|
Data.LoadRawTextureData((IntPtr) irDataPtr, sizeof(ushort) * irData_.Length);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-07-06 22:17:26 +00:00
|
|
|
|
Data.Apply();
|
|
|
|
|
frame.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|