-
Notifications
You must be signed in to change notification settings - Fork 0
/
CameraModule.cs
138 lines (126 loc) · 4.79 KB
/
CameraModule.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
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.Util;
using Emgu.CV.Cuda;
using System;
using System.Drawing;
using System.Threading.Tasks;
using Telegram.Bot.Types;
using Emgu.CV.Reg;
namespace CameraMicTelegram
{
public class CameraModule
{
private Mat Image { get; set; }
private Image<Bgr, byte> BgrImage { get; set; }
private string FileName { get; set; }
private DateTime TakenDate { get; set; }
private string ErrorMessage { get; set; }
private CameraModule()
{
TakenDate = DateTime.Now;
}
public static CameraModule TakePic()
{
CameraModule Camera = new CameraModule();
try
{
VideoCapture capture = new VideoCapture(0);
capture.Set(CapProp.FrameWidth, 1280);
capture.Set(CapProp.FrameHeight, 720);
Camera.Image = capture.QueryFrame();
Camera.BgrImage = Camera.Image.ToImage<Bgr, byte>();
capture.Dispose();
}
catch (Exception)
{
Camera.Image = new Mat();
}
return Camera;
}
public CameraModule SavePic(string fileName)
{
if (fileName.Length > 0)
FileName = fileName;
else
FileName = DateTime.Now.ToString();
try
{
BgrImage.Save(Environment.CurrentDirectory + "/assets/" + FileName + ".jpg");
Image.Dispose();
}
catch (Exception)
{
ErrorMessage = "Cannot save picture, might be no BgrImage?";
}
return this;
}
public CameraModule CheckFace()
{
CascadeClassifier haar = new CascadeClassifier(Environment.CurrentDirectory + "/models/haarcascade_frontalface_default.xml");
Image<Gray, byte> grayframe = Image.ToImage<Gray, byte>();
var faces = haar.DetectMultiScale(
grayframe,
1.4,
4,
new Size(grayframe.Width / 8, grayframe.Height / 8),
Size.Empty);
foreach (var face in faces)
{
this.BgrImage.Draw(face, new Bgr(0, double.MaxValue, 100), 3);
}
return this;
}
public static CameraModule WatchFaces()
{
CameraModule Camera = new CameraModule();
VideoCapture capture = new VideoCapture(0);
capture.Set(CapProp.FrameWidth, 1280);
capture.Set(CapProp.FrameHeight, 720);
CascadeClassifier haar = new CascadeClassifier(Environment.CurrentDirectory + "/models/haarcascade_frontalface_default.xml");
while ("1" == "1")
using (Camera.Image = capture.QueryFrame())
{
if (Camera.Image != null)
{
Camera.BgrImage = Camera.Image.ToImage<Bgr, byte>();
Image<Gray, byte> grayframe = Camera.Image.ToImage<Gray, byte>();
var faces = haar.DetectMultiScale(
grayframe,
1.4,
4,
new Size(grayframe.Width / 8, grayframe.Height / 8),
Size.Empty);
foreach (var face in faces)
{
Camera.BgrImage.Draw(face, new Bgr(0, double.MaxValue, 100), 3);
}
if (faces.Length != 0)
{
return Camera;
}
}
else
{
Camera.ErrorMessage = "Camera image is not available.";
return Camera;
}
}
//Camera.ErrorMessage = "Cannot reach frames. Could be camera used by other software? or software problems.";
//return Camera;
}
public static async Task<Boolean> TakeSavePic(string fileName = "pict")
{
return await Task.Run(() => TakePic().SavePic(fileName).FileName != null);
}
public static async Task<Boolean> FaceAndSave(string fileName = "pict")
{
return await Task.Run(() => TakePic().CheckFace().SavePic(fileName).FileName != null);
}
public static async Task<Boolean> WatchAndSee(string fileName = "catch")
{
return await Task.Run(() => WatchFaces().SavePic(fileName).FileName != null);
}
}
}