-
Notifications
You must be signed in to change notification settings - Fork 1
/
Camera.cs
613 lines (577 loc) · 22.4 KB
/
Camera.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
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Net.Mime;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using Twilio;
using Twilio.Rest.Api.V2010.Account;
//using VisioForge.Controls.UI.WinForms;
using VisioForge.Controls.UI.WPF;
using VisioForge.Types;
using VisioForge.Types.OutputFormat;
// https://help.visioforge.com/sdks_net/html/T_VisioForge_Controls_UI_WPF_VideoCapture.htm
using VisioForge.Types.VideoEffects;
namespace IPCamera
{
public class Camera
{
public String url = "";
public string name = "";
public string id = "";
public string username = "";
public string password = "";
public bool isEsp32 = false;
WindowControll win_controll;
public int row = 0;
public int coll = 0;
public bool detection = false;
public bool recognition = false;
public bool on_move_sms = false;
public bool on_move_email = false;
public bool on_move_pic = false;
public bool on_move_rec = false;
public int on_move_sensitivity = 2;
public int brightness = 0;
public int contrast = 0;
public int darkness = 0;
public bool recording = false;
public VideoCapture video;
public static int count = 0;
public static String DB_connection_string = "Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\Alexp\\source\\repos\\ESP32-Cam-Dashboard\\Database1.mdf;Integrated Security=True";
public static String pictures_dir;
public static String videos_dir;
public static bool avi_format = false;
public static bool mp4_format = false;
public static bool webm_format = false;
public String up_req = "";
public String down_req = "";
public String right_req = "";
public String left_req = "";
public String net_stream_ip = "localhost";
public String net_stream_port = "";
public String net_stream_prefix = "";
public bool net_stream = false;
HttpServer server = new HttpServer();
public bool camera_oppened = false;
public Camera(String url, String name, String id, bool rec)
{
this.url = url;
this.name = name;
this.id = id;
// Create an VideoCapture
if (!this.isEsp32)
{
this.video = new VideoCapture
{
IP_Camera_Source = new VisioForge.Types.Sources.IPCameraSourceSettings()
{
URL = this.url,
Login = this.Username,
Password = this.Password
}
};
} else
{
this.video = new VideoCapture
{
IP_Camera_Source = new VisioForge.Types.Sources.IPCameraSourceSettings()
{
URL = this.url/*,
Type = VisioForge.Types.VFIPSource.Auto_LAV*/
}
};
}
this.video.OnError += OnError;
this.video.MouseUp += CamerasFocused;
this.video.Audio_PlayAudio = this.video.Audio_RecordAudio = false;
this.video.Video_Effects_Enabled = true;
this.video.IP_Camera_Source.Type = VisioForge.Types.VFIPSource.HTTP_MJPEG_LowLatency;
this.Recording = rec;
// Motion Detection Setup
this.video.Motion_Detection = new MotionDetectionSettings
{
Enabled = true,
Highlight_Enabled = false
};
this.video.OnMotion += this.OnMotion;
//this.video.Video_Still_Frames_Grabber_Enabled = true;
count++;
}
~Camera()
{
count--;
}
// Return this video capture
public VideoCapture Get()
{
return this.video;
}
// Setup Username
public String Username
{
get { return this.username; }
set { this.username = value; }
}
// Setup Password
public String Password
{
get { return this.password; }
set { this.password = value; }
}
// Setup Brightness Effext
public int Brightness
{
get { return this.brightness; }
set
{
this.brightness = value;
// Add the efect
IVFVideoEffectLightness lightness;
var effect_l = this.video.Video_Effects_Get("Lightness");
if (effect_l == null)
{
lightness = new VFVideoEffectLightness(true, this.brightness, 0, "Lightness");
this.video.Video_Effects_Add(lightness);
}
else
{
lightness = effect_l as IVFVideoEffectLightness;
if (lightness != null)
{
lightness.Value = this.brightness;
}
}
}
}
public bool Net_stream
{
get { return this.net_stream; }
set {
this.net_stream = value;
if (this.net_stream_ip.Length > 0 && this.net_stream_port.Length > 0)
{
this.server.run = this.net_stream;
Console.WriteLine("Server,Run: " + Convert.ToString(this.net_stream));
if (this.net_stream)
{
this.server.port = this.net_stream_port;
this.server.ip = this.net_stream_ip;
this.server.prefix = this.net_stream_prefix;
this.server.cam = this;
// Start http this.server
this.server.setup();
var result = this.server.ListenAsync();
} else
{
if(this.server.run)
{
this.server.close();
}
}
}
}
}
public String Net_stream_port
{
get { return this.net_stream_port; }
set { this.net_stream_port = value; }
}
// Setup Contrast Effext
public int Contrast
{
get { return this.contrast; }
set
{
this.contrast = value;
// Add the efect
IVFVideoEffectContrast contrast;
var effect_c = this.video.Video_Effects_Get("Contrast");
if (effect_c == null)
{
contrast = new VFVideoEffectContrast(true, this.contrast, 0, "Contrast");
this.video.Video_Effects_Add(contrast);
}
else
{
contrast = effect_c as IVFVideoEffectContrast;
if (contrast != null)
{
contrast.Value = this.contrast;
}
}
}
}
// Setup Drkness Effext
public int Darkness
{
get { return this.darkness; }
set
{
this.darkness = value;
// Add the efect
IVFVideoEffectDarkness darkness;
var effect_d = this.video.Video_Effects_Get("Darkness");
if (effect_d == null)
{
darkness = new VFVideoEffectDarkness(true, this.darkness, 0, "Darkness");
this.video.Video_Effects_Add(darkness);
}
else
{
darkness = effect_d as IVFVideoEffectDarkness;
if (darkness != null)
{
darkness.Value = this.darkness;
}
}
}
}
// When Change Detection
public bool Detection
{
get { return this.detection; }
set
{
this.detection = value;
if (this.detection)
{
/*
this.video.Face_Tracking = new FaceTrackingSettings
{
ColorMode = CamshiftMode.RGB,
Highlight = true,
MinimumWindowSize = 25,
ScalingMode = ObjectDetectorScalingMode.GreaterToSmaller,
ScaleFactor = (float)1.7,
SearchMode = ObjectDetectorSearchMode.Single
};
*/
this.video.Face_Tracking = new FaceTrackingSettings()
{
Highlight = true
};
this.video.OnFaceDetected += (object sender, AFFaceDetectionEventArgs e) =>
{
foreach (Rectangle faceRectangle in e.FaceRectangles)
{
// If Recognition is enable
if (this.recognition)
{
}
else
{
Console.WriteLine($"Face Detection: left-right({faceRectangle.Left}, {faceRectangle.Right}), " +
$"top-bottom({faceRectangle.Top}, {faceRectangle.Bottom}), width-height({faceRectangle.Width}, " +
$"{faceRectangle.Height}) {Environment.NewLine}");
}
}
};
}
else
{
this.video.Face_Tracking = new FaceTrackingSettings();
this.video.OnFaceDetected += (object sender, AFFaceDetectionEventArgs e) => { };
}
}
}
// When Change recognition
public bool Recognition
{
get { return this.recognition; }
set { this.recognition = value; }
}
// Start / Stop Recording
public bool Recording
{
get { return this.recording; }
set {
this.recording = value;
}
}
// On Move SmS
public bool On_move_sms
{
get { return this.on_move_sms; }
set { this.on_move_sms = value; }
}
// On Move Email
public bool On_move_email
{
get { return this.on_move_email; }
set { this.on_move_email = value; }
}
// On Move Pic
public bool On_move_pic
{
get { return this.on_move_pic; }
set { this.on_move_pic = value; }
}
// On Move Rec
public bool On_move_rec
{
get { return this.on_move_rec; }
set { this.on_move_rec = value; }
}
// Setup On Move Sensitivity
public int On_move_sensitivity
{
get { return this.on_move_sensitivity; }
set { this.on_move_sensitivity = value; }
}
// Start the Camera
public void Start()
{
if (this.video.Status != VisioForge.Types.VFVideoCaptureStatus.Work)
{
try
{
// If Rcording is enable setup recording mode
Setup_recording_mode();
// Start Cameres
this.video.Start();
//this.video.StartAsync();
}
catch (System.AccessViolationException ex)
{
Console.WriteLine($"Source:{ex.Source}\nStackTrace:{ex.StackTrace}\n{ex.Message}");
}
}
}
// Stop The Camera
public void Stop()
{
if (this.video.Status == VisioForge.Types.VFVideoCaptureStatus.Work)
{
try
{
this.video.Stop();
//this.video.StopAsync();
}
catch (System.AccessViolationException ex)
{
Console.WriteLine($"Source:{ex.Source}\nStackTrace:{ex.StackTrace}\n{ex.Message}");
}
}
}
// Take Picture
public void Take_pic()
{
DateTime now = DateTime.Now;
String date = now.ToString("F");
date = date.Replace(":", ".");
String dir_path = Camera.pictures_dir + "\\" + this.name;
if (! Directory.Exists(dir_path))
{
Directory.CreateDirectory(dir_path);
}
String file = dir_path + "\\" + date + ".jpg";
this.video.Frame_Save(file, VisioForge.Types.VFImageFormat.JPEG, 85);
}
// Setup Recording Mode
private void Setup_recording_mode()
{
try
{
if (this.Recording)
{
// Video mode == capture
this.video.Mode = VFVideoCaptureMode.IPCapture;
// Setup the right file name
DateTime now = DateTime.Now;
String date = now.ToString("F");
date = date.Replace(":", ".");
String dir_path = Camera.videos_dir + "\\" + this.name;
if (!Directory.Exists(dir_path)) // Directory with the name of the camera
{
Directory.CreateDirectory(dir_path);
}
// Start Recording
// AVI
if (avi_format)
{
String file = dir_path + "\\" + date + ".avi";
this.video.Output_Filename = file;
this.video.Output_Format = new VFAVIOutput();
}
// MP4
if (mp4_format)
{
String file = dir_path + "\\" + date + ".mp4";
this.video.Output_Filename = file;
this.video.Output_Format = new VFMP4v8v10Output();
}
// WEBM
if (webm_format)
{
String file = dir_path + "\\" + date + ".webm";
this.video.Output_Filename = file;
this.video.Output_Format = new VFWebMOutput();
}
}
else
{
// Setup video mode to preview
this.video.Mode = VisioForge.Types.VFVideoCaptureMode.IPPreview;
}
}
catch( Exception ex)
{
Console.WriteLine($"Source:{ex.Source}\nStackTrace:{ex.StackTrace}\n{ex.Message}");
}
}
// On Error EVnt
private void OnError(object sender, VisioForge.Types.ErrorsEventArgs ex)
{
Console.WriteLine($"Level:{ex.Level}\nStackTrace:{ex.StackTrace}\nMessage:{ex.Message}");
//throw new NotImplementedException();
}
// When click on camera
public void CamerasFocused(object sender, MouseButtonEventArgs e)
{
if (MainWindow.Logged && MainWindow.myUsers.Contains(MainWindow.user)
&& (MainWindow.user.Licences.Equals("Admin")))
{
if (this.camera_oppened == false)
{
this.camera_oppened = true;
this.win_controll = new WindowControll(this);
win_controll.Show();
}
else
{
this.win_controll.Activate();
}
}
}
// This Happends when camera detectets a motion
DateTime last_email_date_onmove = DateTime.Now.AddMinutes(-1);
public void OnMotion(object sender, MotionDetectionEventArgs e)
{
if (e.Level > this.On_move_sensitivity)
{
//Console.WriteLine($"Motion Detection!!! Matrix: {e.Matrix.Length.ToString()} Level: {e.Level}");
if (this.On_move_email)
{
try
{
/*
Console.WriteLine($"Motion Detected Send Email Message. [Before] " +
$"Time.now: {DateTime.Now} Time.before: {last_email_date_onmove.AddMinutes(1)}");
*/
// When Send Get the DateTime
if (DateTime.Now > last_email_date_onmove.AddMinutes(1))
{
last_email_date_onmove = DateTime.Now;
/*
// Grab a Pic
BitmapSource bmpS = this.video.Frame_GetCurrent();
// BitmapSource to Stream
MemoryStream stream = new MemoryStream();
GetBitmap(bmpS).Save(stream, ImageFormat.Jpeg);
*/
// Return to Sending Email
String host = "";
int port = 587;
String fromEmail = MainWindow.email_send;
if (fromEmail.Contains("gmail"))
{
host = "smtp.gmail.com";
}
else if (fromEmail.Contains("yahoo"))
{
host = "smtp.mail.yahoo.com";
}
else if (fromEmail.Contains("live"))
{
host = " smtp.live.com";
}
String fromPassword = MainWindow.pass_send;
String subject = this.name;
String body = $"[{this.name}] Detect Motion at [{DateTime.Now}]";
// Create a Message
MailMessage msg = new MailMessage();
msg.From = new MailAddress(fromEmail);
/*
// Add Image to message OK
System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Image.Jpeg);
System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(stream, ct);
attach.ContentDisposition.FileName = "img.jpeg";
msg.Attachments.Add(attach);
*/
// Add All Recievers
foreach (Users u in MainWindow.myUsers)
{
msg.To.Add(u.Email);
Console.WriteLine($"Send Email From: {fromEmail} Pass: {fromPassword} To: {u.Email}");
}
msg.Subject = subject;
msg.Body = body;
// Create Email Connection Object
SmtpClient smtp = new SmtpClient(host)
{
Port = port,
EnableSsl = true,
UseDefaultCredentials = true,
Credentials = new NetworkCredential(fromEmail, fromPassword),
DeliveryMethod = SmtpDeliveryMethod.Network
};
// Send Email
smtp.Send(msg); // Doesn't Works
}
}
catch (SmtpException ex)
{
Console.WriteLine($"Source:{ex.Source}\nStackTrace:{ex.StackTrace}\n{ex.Message}");
}
catch (Exception ex)
{
Console.WriteLine($"Source:{ex.Source}\nStackTrace:{ex.StackTrace}\n{ex.Message}");
}
}
if (this.On_move_pic)
{
Console.WriteLine("Take Picture.");
this.Take_pic();
}
if (this.On_move_rec)
{
Console.WriteLine("Start Recording.");
// Recording for some time
this.Recording = true;
Thread.Sleep(1000 * 10 * 60);
this.Recording = false;
}
if (this.On_move_sms)
{
// Send SMS
if (DateTime.Now > last_email_date_onmove.AddMinutes(1))
{
last_email_date_onmove = DateTime.Now;
// Find your Account Sid and Token at https://account.apifonica.com/
Console.WriteLine($"Before Send SMS. ssid: {MainWindow.twilioAccountSID} token: {MainWindow.twilioAccountToken}");
TwilioClient.Init(MainWindow.twilioAccountSID, MainWindow.twilioAccountToken);
foreach (Users u in MainWindow.myUsers)
{
var message = MessageResource.Create(
body: $"[{this.name}] Detect Motion at [{DateTime.Now}]",
from: new Twilio.Types.PhoneNumber(MainWindow.twilioNumber),
to: new Twilio.Types.PhoneNumber(u.Phone)
);
Console.WriteLine($"Send SMS To: {message.Sid}.");
}
}
}
}
}
}
}