Skip to content

Commit 272f458

Browse files
committed
Initial Commit
0 parents  commit 272f458

File tree

7 files changed

+1816
-0
lines changed

7 files changed

+1816
-0
lines changed
7.26 KB
Binary file not shown.
Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
using UnityEngine;
2+
using System.Collections;
3+
using UnityEngine.UI;
4+
5+
using RenderHeads.Media.AVProVideo;
6+
using OpenCVForUnity;
7+
8+
public class AVProWithOpenCVForUnitySample : MonoBehaviour
9+
{
10+
11+
/// <summary>
12+
/// The media player.
13+
/// </summary>
14+
public MediaPlayer mediaPlayer;
15+
16+
/// <summary>
17+
/// The target texture.
18+
/// </summary>
19+
Texture2D targetTexture;
20+
21+
/// <summary>
22+
/// The texture.
23+
/// </summary>
24+
Texture2D texture;
25+
26+
/// <summary>
27+
/// The colors.
28+
/// </summary>
29+
Color32[] colors;
30+
31+
/// <summary>
32+
/// The rgba mat.
33+
/// </summary>
34+
Mat rgbaMat;
35+
36+
/// <summary>
37+
/// The m sepia kernel.
38+
/// </summary>
39+
Mat mSepiaKernel;
40+
41+
/// <summary>
42+
/// The m size0.
43+
/// </summary>
44+
Size mSize0;
45+
46+
/// <summary>
47+
/// The m intermediate mat.
48+
/// </summary>
49+
Mat mIntermediateMat;
50+
51+
public enum modeType
52+
{
53+
original,
54+
sepia,
55+
pixelize,
56+
}
57+
58+
/// <summary>
59+
/// The mode.
60+
/// </summary>
61+
modeType mode;
62+
63+
64+
/// <summary>
65+
/// The original mode toggle.
66+
/// </summary>
67+
public Toggle originalModeToggle;
68+
69+
70+
/// <summary>
71+
/// The sepia mode toggle.
72+
/// </summary>
73+
public Toggle sepiaModeToggle;
74+
75+
76+
/// <summary>
77+
/// The pixelize mode toggle.
78+
/// </summary>
79+
public Toggle pixelizeModeToggle;
80+
81+
82+
// Use this for initialization
83+
void Start ()
84+
{
85+
if (originalModeToggle.isOn) {
86+
mode = modeType.original;
87+
}
88+
if (sepiaModeToggle.isOn) {
89+
mode = modeType.sepia;
90+
}
91+
if (pixelizeModeToggle.isOn) {
92+
mode = modeType.pixelize;
93+
}
94+
95+
// sepia
96+
mSepiaKernel = new Mat (4, 4, CvType.CV_32F);
97+
mSepiaKernel.put (0, 0, /* R */0.189f, 0.769f, 0.393f, 0f);
98+
mSepiaKernel.put (1, 0, /* G */0.168f, 0.686f, 0.349f, 0f);
99+
mSepiaKernel.put (2, 0, /* B */0.131f, 0.534f, 0.272f, 0f);
100+
mSepiaKernel.put (3, 0, /* A */0.000f, 0.000f, 0.000f, 1f);
101+
102+
103+
// pixelize
104+
mIntermediateMat = new Mat ();
105+
mSize0 = new Size ();
106+
107+
108+
mediaPlayer.Events.AddListener (OnVideoEvent);
109+
}
110+
111+
// Update is called once per frame
112+
void Update ()
113+
{
114+
115+
if (texture != null) {
116+
117+
//Convert AVPro's Texture to Texture2D
118+
#if UNITY_ANDROID
119+
Helper.GetReadableTexture (mediaPlayer.TextureProducer.GetTexture (), false, targetTexture);
120+
#else
121+
Helper.GetReadableTexture (mediaPlayer.TextureProducer.GetTexture (), true, targetTexture);
122+
#endif
123+
124+
//Convert Texture2D to Mat
125+
Utils.texture2DToMat (targetTexture, rgbaMat);
126+
127+
128+
if (mode == modeType.original) {
129+
130+
} else if (mode == modeType.sepia) {
131+
132+
Core.transform (rgbaMat, rgbaMat, mSepiaKernel);
133+
134+
} else if (mode == modeType.pixelize) {
135+
136+
Imgproc.resize (rgbaMat, mIntermediateMat, mSize0, 0.1, 0.1, Imgproc.INTER_NEAREST);
137+
Imgproc.resize (mIntermediateMat, rgbaMat, rgbaMat.size (), 0.0, 0.0, Imgproc.INTER_NEAREST);
138+
139+
}
140+
141+
142+
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);
143+
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);
144+
145+
//Convert Mat to Texture2D
146+
Utils.matToTexture2D (rgbaMat, texture, colors);
147+
148+
}
149+
150+
}
151+
152+
// Callback function to handle events
153+
public void OnVideoEvent (MediaPlayer mp, MediaPlayerEvent.EventType et,
154+
ErrorCode errorCode)
155+
{
156+
switch (et) {
157+
case MediaPlayerEvent.EventType.ReadyToPlay:
158+
mediaPlayer.Control.Play ();
159+
break;
160+
case MediaPlayerEvent.EventType.FirstFrameReady:
161+
Debug.Log ("First frame ready");
162+
OnNewMediaReady ();
163+
break;
164+
case MediaPlayerEvent.EventType.FinishedPlaying:
165+
mediaPlayer.Control.Rewind ();
166+
break;
167+
}
168+
Debug.Log ("Event: " + et.ToString ());
169+
}
170+
171+
/// <summary>
172+
/// Raises the new media ready event.
173+
/// </summary>
174+
private void OnNewMediaReady ()
175+
{
176+
IMediaInfo info = mediaPlayer.Info;
177+
178+
Debug.Log ("GetVideoWidth " + info.GetVideoWidth () + " GetVideoHeight() " + info.GetVideoHeight ());
179+
180+
// Create a texture the same resolution as our video
181+
if (targetTexture != null) {
182+
Texture2D.Destroy (targetTexture);
183+
targetTexture = null;
184+
}
185+
if (texture != null) {
186+
Texture2D.Destroy (texture);
187+
texture = null;
188+
}
189+
190+
targetTexture = new Texture2D (info.GetVideoWidth (), info.GetVideoHeight (), TextureFormat.ARGB32, false);
191+
192+
texture = new Texture2D (info.GetVideoWidth (), info.GetVideoHeight (), TextureFormat.RGBA32, false);
193+
194+
colors = new Color32[texture.width * texture.height];
195+
196+
rgbaMat = new Mat (texture.height, texture.width, CvType.CV_8UC4);
197+
198+
199+
gameObject.GetComponent<Renderer> ().material.mainTexture = texture;
200+
201+
}
202+
203+
/// <summary>
204+
/// Raises the destroy event.
205+
/// </summary>
206+
void OnDestroy ()
207+
{
208+
if (targetTexture != null) {
209+
Texture2D.Destroy (targetTexture);
210+
targetTexture = null;
211+
}
212+
213+
if (texture != null) {
214+
Texture2D.Destroy (texture);
215+
texture = null;
216+
}
217+
218+
if (mSepiaKernel != null) {
219+
mSepiaKernel.Dispose ();
220+
mSepiaKernel = null;
221+
}
222+
223+
if (mIntermediateMat != null) {
224+
mIntermediateMat.Dispose ();
225+
mIntermediateMat = null;
226+
}
227+
}
228+
229+
/// <summary>
230+
/// Raises the original mode toggle event.
231+
/// </summary>
232+
public void OnOriginalModeToggle ()
233+
{
234+
235+
if (originalModeToggle.isOn) {
236+
mode = modeType.original;
237+
}
238+
}
239+
240+
/// <summary>
241+
/// Raises the sepia mode toggle event.
242+
/// </summary>
243+
public void OnSepiaModeToggle ()
244+
{
245+
246+
if (sepiaModeToggle.isOn) {
247+
mode = modeType.sepia;
248+
}
249+
}
250+
251+
/// <summary>
252+
/// Raises the pixelize mode toggle event.
253+
/// </summary>
254+
public void OnPixelizeModeToggle ()
255+
{
256+
257+
if (pixelizeModeToggle.isOn) {
258+
mode = modeType.pixelize;
259+
}
260+
}
261+
}

AVProWithOpenCVForUnitySample/AVProWithOpenCVForUnitySample.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)