-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMicso2.cs
152 lines (127 loc) · 3.5 KB
/
Micso2.cs
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
using System;
using System.Threading;
using Emgu.CV;
using Emgu.CV.Linemod;
using Emgu.CV.Structure;
public class MicrophoneListener : IDisposable
{
private readonly Microphone _microphone;
private readonly Level _level;
private readonly VideoCapture _videoCapture;
public MicrophoneListener(MicrophoneListener microphone, Level level, VideoCapture videoCapture)
{
var _microphone = microphone;
var _level = level;
var _videoCapture = videoCapture;
var _microphone.VolumeChanged += OnVolumeChanged;
}
private void OnVolumeChanged(object sender, VolumeChangedEventArgs e)
{
if (e.NewValue > _level.Value)
{
// Start recording audio and video
_microphone.StartRecording();
_videoCapture.StartRecording();
}
}
public void Dispose()
{
_microphone.StopRecording();
_videoCapture.StopRecording();
}
}
/// <summary>
/// Define the `Level` class to represent the threshold for starting recording:
/// </summary>
public class Level
{
private int _value;
public Level(int value)
{
_value = value;
}
public int Value
{
get { return _value; }
}
}
using Emgu.CV.Structure;
/// <summary>
/// Create a `VideoCapture` instance to capture the video stream:
/// </summary>
public class VideoCapture
{
private readonly VideoCaptureDevice _device;
public VideoCapture(VideoCaptureDevice device)
{
_device = device;
}
public void StartRecording()
{
// Start recording the video stream
_device.StartRecording();
}
public void StopRecording()
{
// Stop recording the video stream
_device.StopRecording();
}
}
```
4.Define a `FaceDetector` class to detect faces in the video stream:
```
using Emgu.CV.Objects;
public class FaceDetector
{
private readonly CascadeClassifier _cascade;
public FaceDetector(CascadeClassifier cascade)
{
_cascade = cascade;
}
public void DetectFaces(Image<Gray, Byte> image)
{
// Detect faces in the image
_cascade.DetectMultiScale(image, new GrayRect(new Size(30, 30), new Point(10, 10)), 1.1, 2);
}
}
```
5.Create a `MicrophoneListener` instance to start recording audio and video when the level passes the
threshold:
```
using System;
using System.Threading;
using Emgu.CV;
using Emgu.CV.Structure;
public class MicrophoneListener : IDisposable
{
private readonly Microphone _microphone;
private readonly Level _level;
private readonly VideoCapture _videoCapture;
private readonly FaceDetector _faceDetector;
public MicrophoneListener(Microphone microphone, Level level, VideoCapture videoCapture,
FaceDetector faceDetector)
{
_microphone = microphone;
_level = level;
_videoCapture = videoCapture;
_faceDetector = faceDetector;
_microphone.VolumeChanged += OnVolumeChanged;
}
private void OnVolumeChanged(object sender, VolumeChangedEventArgs e)
{
if (e.NewValue > _level.Value)
{
// Start recording audio and video
_microphone.StartRecording();
_videoCapture.StartRecording();
// Detect faces in the video stream
Image<Gray, Byte> image = new Image<Gray, Byte>(_videoCapture.GetFrame());
_faceDetector.DetectFaces(image);
}
}
public void Dispose()
{
_microphone.StopRecording();
_videoCapture.StopRecording();
}
}