-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathWebCamOperation.cs
More file actions
479 lines (412 loc) · 13.6 KB
/
WebCamOperation.cs
File metadata and controls
479 lines (412 loc) · 13.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
using System;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
/// <summary>
/// WebCamTexture Example
/// An example of detecting face landmarks in WebCamTexture images.
/// </summary>
public class WebCamOperation : MonoBehaviour
{
//public RawImage rawImage;
[SerializeField, TooltipAttribute("Hide the image or not")]
public bool isHideCameraImage = true;
/// <summary>
/// Set the name of the device to use.
/// </summary>
//[SerializeField, TooltipAttribute("Set the name of the device to use.")]
private string requestedDeviceName = null;
/// <summary>
/// Set the width of WebCamTexture.
/// </summary>
[SerializeField, TooltipAttribute("Set the width of WebCamTexture.")]
public int requestedWidth = 1280;
/// <summary>
/// Set the height of WebCamTexture.
/// </summary>
[SerializeField, TooltipAttribute("Set the height of WebCamTexture.")]
public int requestedHeight = 760;
/// <summary>
/// Set FPS of WebCamTexture.
/// </summary>
[SerializeField, TooltipAttribute("Set FPS of WebCamTexture.")]
public int requestedFPS = 30;
/// <summary>
/// Set whether to use the front facing camera.
/// </summary>
private bool requestedIsFrontFacing = true;
///// <summary>
///// Determines if adjust pixels direction.
///// </summary>
//[SerializeField, TooltipAttribute("Determines if adjust pixels direction.")]
private bool adjustPixelsDirection = true;
/// <summary>
/// The webcam texture.
/// </summary>
WebCamTexture webCamTexture;
/// <summary>
/// The webcam device.
/// </summary>
WebCamDevice webCamDevice;
/// <summary>
/// The colors.
/// </summary>
Color32[] colors;
/// <summary>
/// Indicates whether this instance is waiting for initialization to complete.
/// </summary>
bool isInitWaiting = false;
/// <summary>
/// Indicates whether this instance has been initialized.
/// </summary>
bool hasInitDone = false;
/// <summary>
/// The width of the screen.
/// </summary>
int screenWidth;
/// <summary>
/// The height of the screen.
/// </summary>
int screenHeight;
/// <summary>
/// The texture.
/// </summary>
Texture2D texture;
OpenCVForUnity.CoreModule.Mat mat;
// Use this for initialization
void Awake()
{
Initialize();
}
void Initialize()
{
if (isInitWaiting)
return;
StartCoroutine(_Initialize());
}
public WebCamTexture GetWebCamTexture()
{
return webCamTexture;
}
public bool WebCamRunning()
{
return hasInitDone && webCamTexture.isPlaying;
}
/// <summary>
/// Initializes webcam texture by coroutine.
/// </summary>
private IEnumerator _Initialize()
{
if (hasInitDone)
Dispose();
isInitWaiting = true;
// Creates the camera
webCamDevice = WebCamTexture.devices[0];
webCamTexture = new WebCamTexture(webCamDevice.name, requestedWidth, requestedHeight, requestedFPS);
if (webCamTexture == null)
{
Debug.Log("Cannot find camera device " + requestedDeviceName + ".");
}
if (webCamTexture == null)
{
// Checks how many and which cameras are available on the device
for (int cameraIndex = 0; cameraIndex < WebCamTexture.devices.Length; cameraIndex++)
{
if (WebCamTexture.devices[cameraIndex].isFrontFacing == requestedIsFrontFacing)
{
webCamDevice = WebCamTexture.devices[cameraIndex];
webCamTexture = new WebCamTexture(webCamDevice.name, requestedWidth, requestedHeight, requestedFPS);
break;
}
}
}
if (webCamTexture == null)
{
if (WebCamTexture.devices.Length > 0)
{
webCamDevice = WebCamTexture.devices[0];
webCamTexture = new WebCamTexture(webCamDevice.name, requestedWidth, requestedHeight, requestedFPS);
}
else
{
Debug.LogError("Camera device does not exist.");
isInitWaiting = false;
yield break;
}
}
//rawImage.material.mainTexture = webCamTexture;
// Starts the camera
webCamTexture.Play();
while (true)
{
if (webCamTexture.didUpdateThisFrame)
{
Debug.Log("name:" + webCamTexture.deviceName + " width:" + webCamTexture.width + " height:" + webCamTexture.height + " fps:" + webCamTexture.requestedFPS);
Debug.Log("videoRotationAngle:" + webCamTexture.videoRotationAngle + " videoVerticallyMirrored:" + webCamTexture.videoVerticallyMirrored + " isFrongFacing:" + webCamDevice.isFrontFacing);
screenWidth = Screen.width;
screenHeight = Screen.height;
isInitWaiting = false;
hasInitDone = true;
OnInited();
break;
}
else
{
yield return 0;
}
}
}
/// <summary>
/// Releases all resource.
/// </summary>
private void Dispose()
{
isInitWaiting = false;
hasInitDone = false;
if (webCamTexture != null)
{
webCamTexture.Stop();
WebCamTexture.Destroy(webCamTexture);
webCamTexture = null;
}
if (texture != null)
{
Texture2D.Destroy(texture);
texture = null;
}
}
/// <summary>
/// Raises the webcam texture initialized event.
/// </summary>
private void OnInited()
{
if (colors == null || colors.Length != webCamTexture.width * webCamTexture.height)
{
colors = new Color32[webCamTexture.width * webCamTexture.height];
}
texture = new Texture2D(webCamTexture.width, webCamTexture.height, TextureFormat.RGBA32, false);
mat = new OpenCVForUnity.CoreModule.Mat(webCamTexture.height, webCamTexture.width, OpenCVForUnity.CoreModule.CvType.CV_8UC3);
//rawImage.texture = texture;
gameObject.transform.localScale = new Vector3(texture.width, texture.height, 1);
Debug.Log("Screen.width " + Screen.width + " Screen.height " + Screen.height + " Screen.orientation " + Screen.orientation);
float width = texture.width;
float height = texture.height;
float widthScale = (float)Screen.width / width;
float heightScale = (float)Screen.height / height;
}
// Update is called once per frame
void Update()
{
if (adjustPixelsDirection)
{
// Catch the orientation change of the screen.
if (screenWidth != Screen.width || screenHeight != Screen.height)
{
Initialize();
}
else
{
screenWidth = Screen.width;
screenHeight = Screen.height;
}
}
if (hasInitDone && webCamTexture.isPlaying && webCamTexture.didUpdateThisFrame)
{
int webCamTexture_width = webCamTexture.width;
int webCamTexture_height = webCamTexture.height;
// Color32 format
Color32[] colors = GetColors();
if (colors != null && colors.Length != 0)
{
texture.SetPixels32(colors);
texture.Apply(false);
// Convert the texture2d to mat format
OpenCVForUnity.UnityUtils.Utils.texture2DToMat(texture, mat);
return;
}
}
}
/// <summary>
/// Gets the current WebCameraTexture frame that converted to the correct direction.
/// </summary>
public Color32[] GetColors()
{
webCamTexture.GetPixels32(colors);
if (adjustPixelsDirection)
{
//Adjust an array of color pixels according to screen orientation and WebCamDevice parameter.
FlipColors<Color32>(colors, webCamTexture.width, webCamTexture.height);
return colors;
}
return colors;
}
/// <summary>
/// Gets the current WebCameraTexture frame that converted to the correct direction.
/// </summary>
public Color[] GetColors(int x, int y, int width, int height)
{
// The (0,0) coordinate of unity is right-bottom corner
int x_RightBottom = webCamTexture.width - width - x;
int y_RightBottom = webCamTexture.height - height - y;
Color[] tempColors = webCamTexture.GetPixels(x_RightBottom, y_RightBottom, width, height);
if (adjustPixelsDirection)
{
//Adjust an array of color pixels according to screen orientation and WebCamDevice parameter.
FlipColors<Color>(tempColors, width, height);
return tempColors;
}
return tempColors;
}
public OpenCVForUnity.CoreModule.Mat GetMat()
{
// Get the full image
var colors = GetColors();
var texture = new Texture2D(webCamTexture.width, webCamTexture.height);
texture.SetPixels32(colors);
// Convert the texture2d to mat format
var dst = new OpenCVForUnity.CoreModule.Mat(webCamTexture.height, webCamTexture.width, OpenCVForUnity.CoreModule.CvType.CV_8UC3);
OpenCVForUnity.UnityUtils.Utils.texture2DToMat(texture, dst);
// Validate the mat object
if (dst == null || dst.getNativeObjAddr() == System.IntPtr.Zero)
return null;
return dst;
}
public OpenCVForUnity.CoreModule.Mat GetMat(int x, int y, int width, int height)
{
if (mat == null || mat.getNativeObjAddr() == System.IntPtr.Zero)
return null;
// Crop the image by using opencv
var rect = new OpenCVForUnity.CoreModule.Rect(x, y, width, height);
var dst = new OpenCVForUnity.CoreModule.Mat(mat, rect);
// Validate the mat object
if (dst == null || dst.getNativeObjAddr() == System.IntPtr.Zero)
return null;
return dst;
}
/// <summary>
/// Raises the destroy event.
/// </summary>
void OnDestroy()
{
Dispose();
}
/// <summary>
/// Flips the colors.
/// </summary>
/// <param name="colors">Colors.</param>
/// <param name="width">Width.</param>
/// <param name="height">Height.</param>
void FlipColors<T>(IList<T> colors, int width, int height)
{
int flipCode = int.MinValue;
if (webCamDevice.isFrontFacing)
{
if (webCamTexture.videoRotationAngle == 0)
{
flipCode = 1;
}
else if (webCamTexture.videoRotationAngle == 90)
{
flipCode = 1;
}
if (webCamTexture.videoRotationAngle == 180)
{
flipCode = 0;
}
else if (webCamTexture.videoRotationAngle == 270)
{
flipCode = 0;
}
}
else
{
if (webCamTexture.videoRotationAngle == 180)
{
flipCode = -1;
}
else if (webCamTexture.videoRotationAngle == 270)
{
flipCode = -1;
}
}
if (flipCode > int.MinValue)
{
if (flipCode == 0)
{
FlipVertical(colors, colors, width, height);
}
else if (flipCode == 1)
{
FlipHorizontal(colors, colors, width, height);
}
else if (flipCode < 0)
{
Rotate180(colors, colors, height, width);
}
}
}
/// <summary>
/// Flips vertical.
/// </summary>
/// <param name="src">Src colors.</param>
/// <param name="dst">Dst colors.</param>
/// <param name="width">Width.</param>
/// <param name="height">Height.</param>
void FlipVertical<T>(IList<T> src, IList<T> dst, int width, int height)
{
for (var i = 0; i < height / 2; i++)
{
var y = i * width;
var x = (height - i - 1) * width;
for (var j = 0; j < width; j++)
{
int s = y + j;
int t = x + j;
var c = src[s];
dst[s] = src[t];
dst[t] = c;
}
}
}
/// <summary>
/// Flips horizontal.
/// </summary>
/// <param name="src">Src colors.</param>
/// <param name="dst">Dst colors.</param>
/// <param name="width">Width.</param>
/// <param name="height">Height.</param>
void FlipHorizontal<T>(IList<T> src, IList<T> dst, int width, int height)
{
for (int i = 0; i < height; i++)
{
int y = i * width;
int x = y + width - 1;
for (var j = 0; j < width / 2; j++)
{
int s = y + j;
int t = x - j;
var c = src[s];
dst[s] = src[t];
dst[t] = c;
}
}
}
/// <summary>
/// Rotates 180 degrees.
/// </summary>
/// <param name="src">Src colors.</param>
/// <param name="dst">Dst colors.</param>
/// <param name="width">Width.</param>
/// <param name="height">Height.</param>
void Rotate180<T>(IList<T> src, IList<T> dst, int height, int width)
{
int i = src.Count;
for (int x = 0; x < i / 2; x++)
{
var t = src[x];
dst[x] = src[i - x - 1];
dst[i - x - 1] = t;
}
}
}