This commit is contained in:
EnoxSoftware 2016-12-13 16:42:22 +09:00
Коммит 272f458369
7 изменённых файлов: 1816 добавлений и 0 удалений

Двоичные данные
AVProWithOpenCVForUnitySample.unitypackage Normal file

Двоичный файл не отображается.

Просмотреть файл

@ -0,0 +1,261 @@
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using RenderHeads.Media.AVProVideo;
using OpenCVForUnity;
public class AVProWithOpenCVForUnitySample : MonoBehaviour
{
/// <summary>
/// The media player.
/// </summary>
public MediaPlayer mediaPlayer;
/// <summary>
/// The target texture.
/// </summary>
Texture2D targetTexture;
/// <summary>
/// The texture.
/// </summary>
Texture2D texture;
/// <summary>
/// The colors.
/// </summary>
Color32[] colors;
/// <summary>
/// The rgba mat.
/// </summary>
Mat rgbaMat;
/// <summary>
/// The m sepia kernel.
/// </summary>
Mat mSepiaKernel;
/// <summary>
/// The m size0.
/// </summary>
Size mSize0;
/// <summary>
/// The m intermediate mat.
/// </summary>
Mat mIntermediateMat;
public enum modeType
{
original,
sepia,
pixelize,
}
/// <summary>
/// The mode.
/// </summary>
modeType mode;
/// <summary>
/// The original mode toggle.
/// </summary>
public Toggle originalModeToggle;
/// <summary>
/// The sepia mode toggle.
/// </summary>
public Toggle sepiaModeToggle;
/// <summary>
/// The pixelize mode toggle.
/// </summary>
public Toggle pixelizeModeToggle;
// Use this for initialization
void Start ()
{
if (originalModeToggle.isOn) {
mode = modeType.original;
}
if (sepiaModeToggle.isOn) {
mode = modeType.sepia;
}
if (pixelizeModeToggle.isOn) {
mode = modeType.pixelize;
}
// sepia
mSepiaKernel = new Mat (4, 4, CvType.CV_32F);
mSepiaKernel.put (0, 0, /* R */0.189f, 0.769f, 0.393f, 0f);
mSepiaKernel.put (1, 0, /* G */0.168f, 0.686f, 0.349f, 0f);
mSepiaKernel.put (2, 0, /* B */0.131f, 0.534f, 0.272f, 0f);
mSepiaKernel.put (3, 0, /* A */0.000f, 0.000f, 0.000f, 1f);
// pixelize
mIntermediateMat = new Mat ();
mSize0 = new Size ();
mediaPlayer.Events.AddListener (OnVideoEvent);
}
// Update is called once per frame
void Update ()
{
if (texture != null) {
//Convert AVPro's Texture to Texture2D
#if UNITY_ANDROID
Helper.GetReadableTexture (mediaPlayer.TextureProducer.GetTexture (), false, targetTexture);
#else
Helper.GetReadableTexture (mediaPlayer.TextureProducer.GetTexture (), true, targetTexture);
#endif
//Convert Texture2D to Mat
Utils.texture2DToMat (targetTexture, rgbaMat);
if (mode == modeType.original) {
} else if (mode == modeType.sepia) {
Core.transform (rgbaMat, rgbaMat, mSepiaKernel);
} else if (mode == modeType.pixelize) {
Imgproc.resize (rgbaMat, mIntermediateMat, mSize0, 0.1, 0.1, Imgproc.INTER_NEAREST);
Imgproc.resize (mIntermediateMat, rgbaMat, rgbaMat.size (), 0.0, 0.0, Imgproc.INTER_NEAREST);
}
Imgproc.putText (rgbaMat, "AVPro With OpenCV for Unity Sample", new Point (50, rgbaMat.rows () / 2), Core.FONT_HERSHEY_SIMPLEX, 2.0, new Scalar (255, 0, 0, 255), 5, Imgproc.LINE_AA, false);
Imgproc.putText (rgbaMat, "W:" + rgbaMat.width () + " H:" + rgbaMat.height () + " SO:" + Screen.orientation, new Point (5, rgbaMat.rows () - 10), Core.FONT_HERSHEY_SIMPLEX, 1.0, new Scalar (255, 255, 255, 255), 2, Imgproc.LINE_AA, false);
//Convert Mat to Texture2D
Utils.matToTexture2D (rgbaMat, texture, colors);
}
}
// Callback function to handle events
public void OnVideoEvent (MediaPlayer mp, MediaPlayerEvent.EventType et,
ErrorCode errorCode)
{
switch (et) {
case MediaPlayerEvent.EventType.ReadyToPlay:
mediaPlayer.Control.Play ();
break;
case MediaPlayerEvent.EventType.FirstFrameReady:
Debug.Log ("First frame ready");
OnNewMediaReady ();
break;
case MediaPlayerEvent.EventType.FinishedPlaying:
mediaPlayer.Control.Rewind ();
break;
}
Debug.Log ("Event: " + et.ToString ());
}
/// <summary>
/// Raises the new media ready event.
/// </summary>
private void OnNewMediaReady ()
{
IMediaInfo info = mediaPlayer.Info;
Debug.Log ("GetVideoWidth " + info.GetVideoWidth () + " GetVideoHeight() " + info.GetVideoHeight ());
// Create a texture the same resolution as our video
if (targetTexture != null) {
Texture2D.Destroy (targetTexture);
targetTexture = null;
}
if (texture != null) {
Texture2D.Destroy (texture);
texture = null;
}
targetTexture = new Texture2D (info.GetVideoWidth (), info.GetVideoHeight (), TextureFormat.ARGB32, false);
texture = new Texture2D (info.GetVideoWidth (), info.GetVideoHeight (), TextureFormat.RGBA32, false);
colors = new Color32[texture.width * texture.height];
rgbaMat = new Mat (texture.height, texture.width, CvType.CV_8UC4);
gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
}
/// <summary>
/// Raises the destroy event.
/// </summary>
void OnDestroy ()
{
if (targetTexture != null) {
Texture2D.Destroy (targetTexture);
targetTexture = null;
}
if (texture != null) {
Texture2D.Destroy (texture);
texture = null;
}
if (mSepiaKernel != null) {
mSepiaKernel.Dispose ();
mSepiaKernel = null;
}
if (mIntermediateMat != null) {
mIntermediateMat.Dispose ();
mIntermediateMat = null;
}
}
/// <summary>
/// Raises the original mode toggle event.
/// </summary>
public void OnOriginalModeToggle ()
{
if (originalModeToggle.isOn) {
mode = modeType.original;
}
}
/// <summary>
/// Raises the sepia mode toggle event.
/// </summary>
public void OnSepiaModeToggle ()
{
if (sepiaModeToggle.isOn) {
mode = modeType.sepia;
}
}
/// <summary>
/// Raises the pixelize mode toggle event.
/// </summary>
public void OnPixelizeModeToggle ()
{
if (pixelizeModeToggle.isOn) {
mode = modeType.pixelize;
}
}
}

Просмотреть файл

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 2272fedb06f4e884b8a09052768d0ab0
timeCreated: 1471094185
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: b54a791b6ec66024987c731b9ec67a3c
timeCreated: 1481556427
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

20
README.md Normal file
Просмотреть файл

@ -0,0 +1,20 @@
AVPro With OpenCV for Unity Sample
====================
Screen Shot
-----
![ScreenShot.PNG](ScreenShot.PNG)
Environment
-----
OpenCVForUnity 2.1.0
UnityPlugin-AVProVideo-Latest-Trial 1.5.12
Setup
-----
* Create New Project. (AVProWithOpenCVForUnitySample)
* Import OpenCVForUnity2.1.0 from AssetStore
* Import UnityPlugin-AVProVideo-Latest-Trial.unitypackage
* Import AVProWithOpenCVForUnitySample.unitypackage

Двоичные данные
ScreenShot.PNG Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 186 KiB