-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
367 lines (327 loc) · 13.4 KB
/
Program.cs
File metadata and controls
367 lines (327 loc) · 13.4 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
using RestSharp;
using RestSharp.Deserializers;
using Spectral;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Drawing.Imaging;
using System.IO;
using System.IO.Ports;
using System.Linq;
using System.Threading;
using WebEye.Controls.Wpf;
namespace Light_Emoting_Diode
{
class Program
{
static SerialPort port;
[STAThread]
static void Main(string[] args)
{
int[] red = new int[3] { 255, 0, 0 };
int[] orange = new int[3] { 255, 50, 0 };
int[] yellow = new int[3] { 255, 200, 0 };
int[] green = new int[3] { 0, 255, 0 };
int[] teal = new int[3] { 0, 127, 255 };
int[] blue = new int[3] { 0, 0, 255 };
int[] purple = new int[3] { 255, 0, 255 };
int[] currentColor = new int[3] { 0, 0, 0 };
int[] futureColor = null;
int baud = 9600;
string portName = ConfigurationManager.AppSettings["DefaultPort"];
Console.Write("Using ports {0}. Change ports? (y/n) ", portName);
string changePort = Console.ReadLine();
if (changePort == "y")
{
Console.WriteLine(" ");
Console.WriteLine("Available ports:");
if (SerialPort.GetPortNames().Count() >= 0)
{
foreach (string port in SerialPort.GetPortNames())
{
Console.WriteLine(port);
}
}
else
{
Console.WriteLine("No Ports available.");
Console.ReadLine();
return;
}
Console.WriteLine("Choose Port:");
portName = Console.ReadLine();
}
Console.WriteLine(" ");
Console.Write("Using baud rate {0}. Change rate? (y/n) ", baud);
string changeRate = Console.ReadLine();
if (changeRate == "y")
{
Console.WriteLine(" ");
Console.Write("Baud rate: ");
baud = GetBaudRate();
}
Console.WriteLine(" ");
Console.WriteLine("Beging Serial...");
BeginSerial(baud, portName);
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
port.Open();
Console.WriteLine("Serial Started.");
Console.WriteLine(" ");
Console.Write("Enable color choose mode? (y/n) ");
string colorChoose = Console.ReadLine();
if (colorChoose == "y")
{
while(true)
{
Console.WriteLine(" ");
Console.Write("Enter a number 1 through 7: ");
int choice = 1;
try
{
choice = int.Parse(Console.ReadLine());
}
catch
{
Console.WriteLine("Failed to parse number");
}
try
{
switch (choice)
{
case 1:
futureColor = red;
break;
case 2:
futureColor = orange;
break;
case 3:
futureColor = yellow;
break;
case 4:
futureColor = green;
break;
case 5:
futureColor = teal;
break;
case 6:
futureColor = blue;
break;
case 7:
futureColor = purple;
break;
}
port.WriteLine((choice).ToString());
for (int i = 0; i < 255; i++)
{
//change red
if (futureColor[0] != currentColor[0])
{
if (futureColor[0] > currentColor[0])
{
currentColor[0]++;
}
else
{
currentColor[0]--;
}
}
//change green
if (futureColor[1] != currentColor[1])
{
if (futureColor[1] > currentColor[1])
{
currentColor[1]++;
}
else
{
currentColor[1]--;
}
}
//change blue
if (futureColor[2] != currentColor[2])
{
if (futureColor[2] > currentColor[2])
{
currentColor[2]++;
}
else
{
currentColor[2]--;
}
}
Led.SetColor((byte)currentColor[0], (byte)currentColor[1], (byte)currentColor[2]);
Thread.Sleep(4);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
Console.WriteLine("Sending emotions");
Thread newWindowThread = new Thread(new ThreadStart(() =>
{
System.Windows.Threading.Dispatcher.Run();
}));
newWindowThread.SetApartmentState(ApartmentState.STA);
newWindowThread.IsBackground = true;
newWindowThread.Start();
System.Drawing.Bitmap image = null;
var webCameraControl1 = new WebCameraControl();
List<WebCameraId> cameras = new List<WebCameraId>(webCameraControl1.GetVideoCaptureDevices());
webCameraControl1.StartCapture(cameras[0]);
var client = new RestClient("https://api-us.faceplusplus.com/facepp/v3");
client.AddHandler("application/json", new JsonDeserializer());
string apiKey = ConfigurationManager.AppSettings["api_key"];
string apiSecret = ConfigurationManager.AppSettings["api_secret"];
DateTime compareTime = DateTime.Now;
while (true)
{
DateTime currentTime = DateTime.Now;
if (currentTime > compareTime.AddSeconds(5))
{
image = webCameraControl1.GetCurrentImage();
image.Save(ConfigurationManager.AppSettings["imagePath"]);
MemoryStream ms = new MemoryStream();
image.Save(ms, ImageFormat.Jpeg);
byte[] byteImage = ms.ToArray();
string imageString = Convert.ToBase64String(byteImage);
var request = new RestRequest("detect", Method.POST);
request.AddParameter("api_key", apiKey);
request.AddParameter("api_secret", apiSecret);
request.AddParameter("image_base64", imageString);
request.AddParameter("return_attributes", "emotion");
try
{
var response = client.Execute<FPPResponse>(request);
var fppResponse = response.Data;
var emotionValue = CalculateEmotion(fppResponse.faces[0].attributes.emotion);
switch((int)emotionValue)
{
case 1:
futureColor = red;
break;
case 2:
futureColor = orange;
break;
case 3:
futureColor = yellow;
break;
case 4:
futureColor = green;
break;
case 5:
futureColor = teal;
break;
case 6:
futureColor = blue;
break;
case 7:
futureColor = purple;
break;
}
port.WriteLine(((int)emotionValue).ToString());
for (int i = 0; i < 255; i++)
{
//change red
if (futureColor[0] != currentColor[0])
{
if (futureColor[0] > currentColor[0])
{
currentColor[0]++;
}
else
{
currentColor[0]--;
}
}
//change green
if (futureColor[1] != currentColor[1])
{
if (futureColor[1] > currentColor[1])
{
currentColor[1]++;
}
else
{
currentColor[1]--;
}
}
//change blue
if (futureColor[2] != currentColor[2])
{
if (futureColor[2] > currentColor[2])
{
currentColor[2]++;
}
else
{
currentColor[2]--;
}
}
Led.SetColor((byte)currentColor[0], (byte)currentColor[1], (byte)currentColor[2]);
Thread.Sleep(4);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
compareTime = DateTime.Now;
}
}
}
static void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
for (int i = 0; i < (10000 * port.BytesToRead) / port.BaudRate; i++);
Console.Write(port.ReadExisting());
Console.WriteLine("");
Console.Write("> ");
}
static void BeginSerial(int baud, string name)
{
port = new SerialPort(name, baud);
}
static int GetBaudRate()
{
try
{
return int.Parse(Console.ReadLine());
}
catch
{
Console.WriteLine("Invalid integer. Please try again:");
return GetBaudRate();
}
}
public static EmotionValue CalculateEmotion(Emotion emotion)
{
int emotionValue = 0;
Dictionary<EmotionValue, int> emotions = new Dictionary<EmotionValue, int>()
{
{ EmotionValue.anger, emotion.anger },
{ EmotionValue.fear, emotion.fear },
{ EmotionValue.happiness, emotion.happiness },
{ EmotionValue.disgust, emotion.disgust },
{ EmotionValue.neutral, emotion.neutral },
{ EmotionValue.sadness, emotion.sadness },
{ EmotionValue.surprise, emotion.surprise },
};
var firstEmotion = emotions.OrderByDescending(x => x.Value).First().Key;
var secondEmotion = emotions.OrderByDescending(x => x.Value).ElementAt(1).Key;
Console.WriteLine("{0} {1}, {2} {3}", firstEmotion, emotions[firstEmotion], secondEmotion, emotions[secondEmotion]);
return firstEmotion;
}
}
public enum EmotionValue
{
anger = 1,
fear = 2,
happiness = 3,
disgust = 4,
neutral = 5,
sadness = 6,
surprise = 7
}
}