Created
November 13, 2015 20:21
-
-
Save MorikoHandford/e47a258cc2ae7a9a44c8 to your computer and use it in GitHub Desktop.
Example of using a frame detector from Affectiva's Affdex Unity asset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Unity derives Camera Input Component UI from this file | |
using UnityEngine; | |
using System.Collections; | |
namespace Affdex | |
{ | |
/// <summary> | |
/// Provides WebCam access to the detector. Sample rate set per second. Use | |
/// </summary> | |
[RequireComponent(typeof(Detector))] | |
public class CameraInput : MonoBehaviour, IDetectorInput | |
{ | |
/// <summary> | |
/// Number of frames per second to sample. Use 0 and call ProcessFrame() manually to run manually. | |
/// Enable/Disable to start/stop the sampling | |
/// </summary> | |
public float sampleRate = 20; | |
protected WebCamDevice[] devices; | |
protected WebCamDevice device; | |
/// <summary> | |
/// Should the selected camera be front facing? | |
/// </summary> | |
public bool isFrontFacing = true; | |
/// <summary> | |
/// Desired width for capture | |
/// </summary> | |
public int targetWidth = 640; | |
/// <summary> | |
/// Desired height for capture | |
/// </summary> | |
public int targetHeight = 480; | |
/// <summary> | |
/// Web Cam texture | |
/// </summary> | |
private WebCamTexture webcamTexture; | |
/// <summary> | |
/// The detector that is on this game object | |
/// </summary> | |
public Detector detector | |
{ | |
get; private set; | |
} | |
/// <summary> | |
/// The texture that is being modified for processing | |
/// </summary> | |
public Texture Texture | |
{ | |
get | |
{ | |
return webcamTexture; | |
} | |
} | |
void Awake() | |
{ | |
detector = GetComponent<Detector>(); | |
detector.Initialize(false); | |
devices = WebCamTexture.devices; | |
SelectCamera(isFrontFacing); | |
webcamTexture = new WebCamTexture(device.name, targetWidth, targetHeight,(int)sampleRate); | |
webcamTexture.Play(); | |
} | |
/// <summary> | |
/// Set the target device based on orientation | |
/// </summary> | |
/// <param name="isFrontFacing">Should the device be forward facing?</param> | |
private void SelectCamera(bool isFrontFacing) | |
{ | |
foreach(WebCamDevice d in devices) | |
{ | |
if(d.isFrontFacing == isFrontFacing) | |
{ | |
device = d; | |
return; | |
} | |
} | |
} | |
void OnEnable() | |
{ | |
//get the selected camera! | |
if (sampleRate > 0) | |
StartCoroutine(SampleRoutine()); | |
} | |
/// <summary> | |
/// Coroutine to sample frames from the camera | |
/// </summary> | |
/// <returns></returns> | |
private IEnumerator SampleRoutine() | |
{ | |
while (enabled) | |
{ | |
yield return new WaitForSeconds(1 / sampleRate); | |
ProcessFrame(); | |
} | |
} | |
/// <summary> | |
/// Sample an individual frame from the webcam and send to detector for processing. | |
/// </summary> | |
public void ProcessFrame() | |
{ | |
if(webcamTexture != null) | |
{ | |
Frame frame = new Frame(webcamTexture.GetPixels32(),webcamTexture.width, webcamTexture.height, Time.realtimeSinceStartup); | |
detector.ProcessFrame(frame); | |
} | |
} | |
void OnDestroy() | |
{ | |
if (webcamTexture != null) | |
{ | |
webcamTexture.Stop(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment