-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFfPlayStreamingFaceDetectionExample.cs
More file actions
448 lines (376 loc) · 15.6 KB
/
FfPlayStreamingFaceDetectionExample.cs
File metadata and controls
448 lines (376 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
#if !(PLATFORM_LUMIN && !UNITY_EDITOR)
using FfmpegWithOpenCVForUnity.UnityUtils.Helper;
using OpenCVForUnity;
using OpenCVForUnity.CoreModule;
using OpenCVForUnity.ImgprocModule;
using OpenCVForUnity.UnityUtils;
using OpenCVForUnityExample;
using UnityEngine;
using UnityEngine.SceneManagement;
using DlibFaceLandmarkDetector;
using DlibFaceLandmarkDetectorExample;
using DlibFaceLandmarkDetector.UnityUtils;
using OpenCVForUnity.UnityUtils.Helper;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
namespace FfmpegWithOpenCVForUnityExample
{
/// <summary>
/// FfmpegToMatHelper Example
/// </summary>
[RequireComponent(typeof(FfplayToMatHelper))]
public class FfplayToMatHelperExample : MonoBehaviour
{
///// <summary>
///// The requested resolution dropdown.
///// </summary>
//public Dropdown requestedResolutionDropdown;
///// <summary>
///// The requested resolution.
///// </summary>
//public ResolutionPreset requestedResolution = ResolutionPreset._640x480;
///// <summary>
///// The requestedFPS dropdown.
///// </summary>
//public Dropdown requestedFPSDropdown;
///// <summary>
///// The requestedFPS.
///// </summary>
//public FPSPreset requestedFPS = FPSPreset._30;
///// <summary>
///// The rotate 90 degree toggle.
///// </summary>
//public Toggle rotate90DegreeToggle;
///// <summary>
///// The flip vertical toggle.
///// </summary>
//public Toggle flipVerticalToggle;
///// <summary>
///// The flip horizontal toggle.
///// </summary>
//public Toggle flipHorizontalToggle;
/// <summary>
/// The texture.
/// </summary>
Texture2D texture;
/// <summary>
/// The webcam texture to mat helper.
/// </summary>
FfplayToMatHelper ffplayToMatHelper;
/// <summary>
/// The FPS monitor.
/// </summary>
FpsMonitor fpsMonitor;
/// <summary>
/// The face landmark detector.
/// </summary>
FaceLandmarkDetector faceLandmarkDetector;
/// <summary>
/// The dlib shape predictor file name.
/// </summary>
string dlibShapePredictorFileName = "DlibFaceLandmarkDetector/sp_human_face_68.dat";
/// <summary>
/// The dlib shape predictor file path.
/// </summary>
string dlibShapePredictorFilePath;
/// <summary>
/// VIDEO_FILENAME
/// </summary>
/// protected static readonly string VIDEO_FILENAME = "DlibFaceLandmarkDetector/couple_mjpeg.mjpeg";
// Use this for initialization
void Start()
{
fpsMonitor = GetComponent<FpsMonitor>();
//dlibShapePredictorFileName = DlibFaceLandmarkDetectorExample.dlibShapePredictorFileName;
dlibShapePredictorFilePath = DlibFaceLandmarkDetector.UnityUtils.Utils.getFilePath(dlibShapePredictorFileName);
ffplayToMatHelper = gameObject.GetComponent<FfplayToMatHelper>();
//int width, height;
//int width, height;
//Dimensions(requestedResolution, out width, out height);
//webCamTextureToMatHelper.requestedWidth = width;
//webCamTextureToMatHelper.requestedHeight = height;
//webCamTextureToMatHelper.requestedFPS = (int)requestedFPS;
//ffmpegToMatHelper.Initialize();
// Update GUI state
//requestedResolutionDropdown.value = (int)requestedResolution;
//string[] enumNames = System.Enum.GetNames(typeof(FPSPreset));
//int index = Array.IndexOf(enumNames, requestedFPS.ToString());
//requestedFPSDropdown.value = index;
//rotate90DegreeToggle.isOn = webCamTextureToMatHelper.rotate90Degree;
//flipVerticalToggle.isOn = webCamTextureToMatHelper.flipVertical;
//flipHorizontalToggle.isOn = webCamTextureToMatHelper.flipHorizontal;
Run();
}
private void Run()
{
if (string.IsNullOrEmpty(dlibShapePredictorFilePath))
{
Debug.LogError("shape predictor file does not exist. Please copy from “DlibFaceLandmarkDetector/StreamingAssets/DlibFaceLandmarkDetector/” to “Assets/StreamingAssets/DlibFaceLandmarkDetector/” folder. ");
}
faceLandmarkDetector = new FaceLandmarkDetector(dlibShapePredictorFilePath);
ffplayToMatHelper.Initialize();
}
/// <summary>
/// Raises the webcam texture to mat helper initialized event.
/// </summary>
public void OnFfplayToMatHelperInitialized()
{
Debug.Log("OnFfplayToMatHelperInitialized");
Mat ffmpegMat = ffplayToMatHelper.GetMat();
texture = new Texture2D(ffmpegMat.cols(), ffmpegMat.rows(), TextureFormat.RGBA32, false);
OpenCVForUnity.UnityUtils.Utils.matToTexture2D(ffmpegMat, texture);
gameObject.GetComponent<Renderer>().material.mainTexture = texture;
gameObject.transform.localScale = new Vector3(ffmpegMat.cols(), ffmpegMat.rows(), 1);
Debug.Log("Screen.width " + Screen.width + " Screen.height " + Screen.height + " Screen.orientation " + Screen.orientation);
if (fpsMonitor != null)
{
fpsMonitor.Add("dlib shape predictor", dlibShapePredictorFileName);
//fpsMonitor.Add("deviceName", webCamTextureToMatHelper.GetDeviceName().ToString());
fpsMonitor.Add("width", ffplayToMatHelper.GetWidth().ToString());
fpsMonitor.Add("height", ffplayToMatHelper.GetHeight().ToString());
//fpsMonitor.Add("videoRotationAngle", webCamTextureToMatHelper.GetWebCamTexture().videoRotationAngle.ToString());
//fpsMonitor.Add("videoVerticallyMirrored", webCamTextureToMatHelper.GetWebCamTexture().videoVerticallyMirrored.ToString());
//fpsMonitor.Add("camera fps", webCamTextureToMatHelper.GetFPS().ToString());
//fpsMonitor.Add("isFrontFacing", webCamTextureToMatHelper.IsFrontFacing().ToString());
//fpsMonitor.Add("rotate90Degree", webCamTextureToMatHelper.rotate90Degree.ToString());
//fpsMonitor.Add("flipVertical", webCamTextureToMatHelper.flipVertical.ToString());
//fpsMonitor.Add("flipHorizontal", webCamTextureToMatHelper.flipHorizontal.ToString());
fpsMonitor.Add("orientation", Screen.orientation.ToString());
}
float width = ffmpegMat.width();
float height = ffmpegMat.height();
float widthScale = (float)Screen.width / width;
float heightScale = (float)Screen.height / height;
if (widthScale < heightScale)
{
Camera.main.orthographicSize = (width * (float)Screen.height / (float)Screen.width) / 2;
}
else
{
Camera.main.orthographicSize = height / 2;
}
}
/// <summary>
/// Raises the webcam texture to mat helper disposed event.
/// </summary>
public void OnFfplayToMatHelperDisposed()
{
Debug.Log("OnFfplayToMatHelperDisposed");
if (texture != null)
{
Texture2D.Destroy(texture);
texture = null;
}
}
/// <summary>
/// Raises the webcam texture to mat helper error occurred event.
/// </summary>
/// <param name="errorCode">Error code.</param>
public void OnFfplayToMatHelperErrorOccurred(FfmpegGetTexturePerFrameToMatHelper.ErrorCode errorCode)
{
Debug.Log("OnFfplayToMatHelperErrorOccurred " + errorCode);
if (fpsMonitor != null)
{
fpsMonitor.consoleText = "ErrorCode: " + errorCode;
}
}
/// <summary>
/// Raises the webcam texture to mat helper error occurred event.
/// </summary>
/// <param name="errorCode">Error code.</param>
public void OnFfplayToMatHelperErrorOccurred(FfplayToMatHelper.ErrorCode errorCode)
{
Debug.Log("OnFfplayToMatHelperErrorOccurred " + errorCode);
if (fpsMonitor != null)
{
fpsMonitor.consoleText = "ErrorCode: " + errorCode;
}
}
// Update is called once per frame
void Update()
{
if (ffplayToMatHelper.IsPlaying() && ffplayToMatHelper.DidUpdateThisFrame())
{
Mat rgbaMat = ffplayToMatHelper.GetMat();
OpenCVForUnityUtils.SetImage(faceLandmarkDetector, rgbaMat);
//detect face rects
List<UnityEngine.Rect> detectResult = faceLandmarkDetector.Detect();
foreach (var rect in detectResult)
{
//detect landmark points
List<Vector2> points = faceLandmarkDetector.DetectLandmark(rect);
//draw landmark points
OpenCVForUnityUtils.DrawFaceLandmark(rgbaMat, points, new Scalar(0, 255, 0), 2);
//draw face rect
OpenCVForUnityUtils.DrawFaceRect(rgbaMat, rect, new Scalar(255, 0, 0), 2);
}
//Imgproc.putText (rgbMat, "W:" + rgbMat.width () + " H:" + rgbMat.height () + " SO:" + Screen.orientation, new Point (5, rgbMat.rows () - 10), Imgproc.FONT_HERSHEY_SIMPLEX, 0.5, new Scalar (255, 255, 255), 1, Imgproc.LINE_AA, false);
//Imgproc.putText(rgbaMat, "W:" + rgbaMat.width() + " H:" + rgbaMat.height() + " SO:" + Screen.orientation, new Point(5, rgbaMat.rows() - 10), Imgproc.FONT_HERSHEY_SIMPLEX, 1.0, new Scalar(255, 255, 255, 255), 2, Imgproc.LINE_AA, false);
OpenCVForUnity.UnityUtils.Utils.matToTexture2D(rgbaMat, texture);
}
}
/// <summary>
/// Raises the destroy event.
/// </summary>
void OnDestroy()
{
ffplayToMatHelper.Dispose();
if (faceLandmarkDetector != null)
faceLandmarkDetector.Dispose();
}
/// <summary>
/// Raises the back button click event.
/// </summary>
public void OnBackButtonClick()
{
SceneManager.LoadScene("FfmpegWithOpenCVForUnityExample");
}
/// <summary>
/// Raises the initialize button click event.
/// </summary>
public void OnInitializeButtonClick()
{
ffplayToMatHelper.Initialize();
}
/// <summary>
/// Raises the play button click event.
/// </summary>
public void OnPlayButtonClick()
{
ffplayToMatHelper.Play();
}
/// <summary>
/// Raises the pause button click event.
/// </summary>
public void OnPauseButtonClick()
{
ffplayToMatHelper.Pause();
}
/// <summary>
/// Raises the stop button click event.
/// </summary>
public void OnStopButtonClick()
{
ffplayToMatHelper.Stop();
}
/// <summary>
/// Raises the change camera button click event.
/// </summary>
public void OnChangeCameraButtonClick()
{
//webCamTextureToMatHelper.requestedIsFrontFacing = !webCamTextureToMatHelper.requestedIsFrontFacing;
}
/// <summary>
/// Raises the requested resolution dropdown value changed event.
/// </summary>
public void OnRequestedResolutionDropdownValueChanged(int result)
{
//if ((int)requestedResolution != result)
//{
// requestedResolution = (ResolutionPreset)result;
// int width, height;
// Dimensions(requestedResolution, out width, out height);
// webCamTextureToMatHelper.Initialize(width, height);
//}
}
/// <summary>
/// Raises the requestedFPS dropdown value changed event.
/// </summary>
public void OnRequestedFPSDropdownValueChanged(int result)
{
//string[] enumNames = Enum.GetNames(typeof(FPSPreset));
//int value = (int)System.Enum.Parse(typeof(FPSPreset), enumNames[result], true);
//if ((int)requestedFPS != value)
//{
// requestedFPS = (FPSPreset)value;
// webCamTextureToMatHelper.requestedFPS = (int)requestedFPS;
//}
}
/// <summary>
/// Raises the rotate 90 degree toggle value changed event.
/// </summary>
public void OnRotate90DegreeToggleValueChanged()
{
//if (rotate90DegreeToggle.isOn != webCamTextureToMatHelper.rotate90Degree)
//{
// webCamTextureToMatHelper.rotate90Degree = rotate90DegreeToggle.isOn;
// if (fpsMonitor != null)
// fpsMonitor.Add("rotate90Degree", webCamTextureToMatHelper.rotate90Degree.ToString());
//}
}
/// <summary>
/// Raises the flip vertical toggle value changed event.
/// </summary>
public void OnFlipVerticalToggleValueChanged()
{
//if (flipVerticalToggle.isOn != webCamTextureToMatHelper.flipVertical)
//{
// webCamTextureToMatHelper.flipVertical = flipVerticalToggle.isOn;
// if (fpsMonitor != null)
// fpsMonitor.Add("flipVertical", webCamTextureToMatHelper.flipVertical.ToString());
//}
}
/// <summary>
/// Raises the flip horizontal toggle value changed event.
/// </summary>
public void OnFlipHorizontalToggleValueChanged()
{
//if (flipHorizontalToggle.isOn != webCamTextureToMatHelper.flipHorizontal)
//{
// webCamTextureToMatHelper.flipHorizontal = flipHorizontalToggle.isOn;
// if (fpsMonitor != null)
// fpsMonitor.Add("flipHorizontal", webCamTextureToMatHelper.flipHorizontal.ToString());
//}
}
//public enum FPSPreset : int
//{
// _0 = 0,
// _1 = 1,
// _5 = 5,
// _10 = 10,
// _15 = 15,
// _30 = 30,
// _60 = 60,
//}
//public enum ResolutionPreset : byte
//{
// _50x50 = 0,
// _640x480,
// _1280x720,
// _1920x1080,
// _9999x9999,
//}
//private void Dimensions(ResolutionPreset preset, out int width, out int height)
//{
// switch (preset)
// {
// case ResolutionPreset._50x50:
// width = 50;
// height = 50;
// break;
// case ResolutionPreset._640x480:
// width = 640;
// height = 480;
// break;
// case ResolutionPreset._1280x720:
// width = 1280;
// height = 720;
// break;
// case ResolutionPreset._1920x1080:
// width = 1920;
// height = 1080;
// break;
// case ResolutionPreset._9999x9999:
// width = 9999;
// height = 9999;
// break;
// default:
// width = height = 0;
// break;
// }
//}
}
}
#endif