-
Notifications
You must be signed in to change notification settings - Fork 28
/
dr-roboto.cpp.in
365 lines (298 loc) · 9.12 KB
/
dr-roboto.cpp.in
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
/*
File: dr-roboto.cpp
Author: Dagim Sisay <[email protected]>
License: AGPL
Date: May, 2018
*/
#define YES_RPI 1
#define NO_RPI 0
#define _DR_ROBOTO_RPI_ @ON_RPI_3@_RPI
#include <stdio.h>
#include <vector>
#include <thread>
#include <unistd.h>
#define BUILD_Debug 1
#define BUILD_Release 0
#define DEBUG_MODE BUILD_@CMAKE_BUILD_TYPE@
#if DEBUG_MODE
#define _DEBUG_
#ifdef _DEBUG_
#include "sense/vision/FileCapture.hpp"
#endif
#endif
#if _DR_ROBOTO_RPI_
#include "sense/vision/RaspiVision.hpp"
#else //_DR_ROBOTO_RPI
#include "sense/vision/Vision.hpp"
#endif
#include "act/audio/FestivalTTS.hpp"
#include "comm/rpc/RPC_Client_Face_Emotion.hpp"
//debug consts
#define _NEED_GUI_
#define _NEED_TIME_INFO_
// opencog headers
#define HAVE_GUILE
#include <opencog/atomspace/AtomSpace.h>
#include <opencog/atoms/truthvalue/TruthValue.h>
#include <opencog/atoms/truthvalue/AttentionValue.h>
#include <opencog/atoms/value/FloatValue.h>
#include <opencog/atoms/value/LinkValue.h>
#include <opencog/atoms/value/StringValue.h>
#include <opencog/guile/SchemePrimitive.h>
#define SCALE 0.1
using namespace std;
using namespace opencog;
class DrRoboto
{
private:
AtomSpace *_as;
bool ok;
thread *run;
static void sensor_loop(DrRoboto *);
ImageSource *cap;
ITColor2Gray c2g;
ITEqualizeHist eh;
ITDetectFace fcd;
ITDetectSmile smd;
FacialLandMark flm;
ITDetectHand dh;
FingersCount fc;
DSaliency sal_d;
FestivalTTS tts;
RPC_Client_Face_Emotion ferpc;
public:
/*
XXX don't know if this is the right way to do it.
The AS is shared b/n this program and the guile instance once this module
is loaded and init_as is called from guile with the AS address as arg.
(init_as (cog-atomspace)) ; this should be called first
Also the AS is just copied.
*/
DrRoboto();
~DrRoboto();
void init_as(AtomSpace *as) { _as = as; ok = true;}
#ifdef _DEBUG_ //XXX REMOVE WHEN DONE
std::string _dbg_fname;
bool _dbg_from_file;
void cap_from_file(std::string);
#endif
//sensor functions
void start_sense();
void stop_sense();
//act functions
void say(std::string);
//TODO add body movement functions.
};
extern "C" {
void init_dr_roboto();
};
DrRoboto::DrRoboto() : c2g("c2g") , eh("eh") , fcd("fcd") , smd("smd") ,
dh("dh") , fc("fc") , sal_d(SAL_FINE_GRAINED)
{
ok = false;
#ifdef _DEBUG
#endif
#if _DR_ROBOTO_RPI_
cap = RaspiCamCapture::init("cap", 320, 240, 20);
#else
cap = CamCapture::init("cap", 320, 240, 0, 20);
#endif //_DR_ROBOTO_RPI_
}
DrRoboto::~DrRoboto() {}
#ifdef _DEBUG_
void DrRoboto::cap_from_file(std::string file_name)
{
_dbg_from_file = true;
_dbg_fname = file_name;
cap = FileCapture::init("cap", _dbg_fname, 30);
}
#endif
void DrRoboto::sensor_loop(DrRoboto *dr)
{
cv::Mat frame, img, gr, he;
std::vector<cv::Rect> faces, smile, hands, emo_boxes;
facial_lms f_lms;
cv::Point2d cent;
std::string str;
std::vector<std::string> emo_preds;
size_t idx = 0, didx = 0;
//XXX clean this up! lash new! maybe have all required nodes created from
// within the scheme code
Handle h;
Handle hl;
Handle fs = dr->_as->add_node(CONCEPT_NODE, "face");
Handle sm = dr->_as->add_node(PREDICATE_NODE, "smile");
Handle lk = dr->_as->add_node(PREDICATE_NODE, "look");
Handle em = dr->_as->add_node(PREDICATE_NODE, "emotion");
Handle nof = dr->_as->add_node(PREDICATE_NODE, "number_of");
Handle ey = dr->_as->add_node(CONCEPT_NODE, "eyes");
Handle hh = dr->_as->add_node(CONCEPT_NODE, "hand");
Handle fi = dr->_as->add_node(CONCEPT_NODE, "fingers");
Handle pos_h = dr->_as->add_node(CONCEPT_NODE, "position");
Handle sen_h = dr->_as->add_node(CONCEPT_NODE, "sense");
Handle rate_h = dr->_as->add_node(CONCEPT_NODE, "rate");
//sen_h->setValue(rate_h, createFloatValue(1000)); //u-secs to delay sensor
ValuePtr pap;
while(dr->ok) {
frame = dr->cap->getCurrentFrame();
#ifdef _DEBUG_ //XXX REMOVE WHEN DONE_
Mat _dbg_img;
_dbg_img = frame.clone();
#endif
gr = dr->c2g.Transform(frame);
he = dr->eh.Transform(gr);
/*
Face Detected
Value
ConceptNode "position"
ConceptNode "face_x"
FloatValue X Y
*/
faces = dr->fcd.Transform(he);
for(idx = 0; idx < faces.size(); idx++) {
str = "face_" + std::to_string(idx+1);
h = dr->_as->add_node(CONCEPT_NODE, str.c_str());
pap = createFloatValue(std::vector<double>({(double) faces[idx].x + faces[idx].width/2,
(double) faces[idx].y + faces[idx].height/2}));
h->setValue(pos_h, pap);
img = he(faces[idx]);
smile = dr->smd.Transform(img); //only check on the smaller face region
if(!smile.empty())
pap = createFloatValue(1.0);
else
pap = createFloatValue(0.0);
h->setValue(sm, pap);
/*
^ this is
Valuation
PredicateNode "smile"
ConceptNode "face_x"
FloatValue x.x
*/
#ifdef _DEBUG_ //XXX REMOVE WHEN DONE
cv::rectangle(_dbg_img, faces[idx], cv::Scalar(0, 255, 0), 2, 1);
cv::putText(_dbg_img, (smile.empty() ? "not smiling" : "smiling"),
cv::Point(faces[idx].x + faces[idx].x*0.05,
faces[idx].y + faces[idx].y*0.05),
FONT_HERSHEY_COMPLEX_SMALL, 0.4, cv::Scalar(0, 0, 255),
0.8, CV_AA);
#endif
// facial lms
dr->flm.get_lm_points(frame, faces[idx], f_lms);
/*
TODO emo detector here... should just be a function that returns
the detected emo along with confidence (if it's possible)
once the emo is detected, make a link to the current face
with the conceptnode for the emo type and set stv based on
confidence (if possible)
current implementation
Value
PredicateNode "emotion"
ConceptNode "face_x"
StringValue "emotion_x"
*/
if(dr->ferpc.detect_emotion(img, emo_preds, emo_boxes)) {
if(emo_preds.size() == 1) {
pap = createStringValue(emo_preds[0]);
h->setValue(em, pap);
#ifdef _DEBUG_ //XXX REMOVE WHEN DONE
cv::putText(_dbg_img, emo_preds[0],
cv::Point(faces[idx].x + faces[idx].x*0.15,
faces[idx].y + faces[idx].y*0.05),
FONT_HERSHEY_COMPLEX_SMALL, 0.4, cv::Scalar(0, 255, 0),
0.8, CV_AA);
#endif
}
}
// TODO do eye state here
} // end of for faces
/*
set number of faces
Value
ConceptNode "face"
PredicateNode "number_of"
FloatValue X
*/
fs->setValue(nof, createFloatValue((double)faces.size()));
/*
salient point
Valuation
PredicateNode "look"
ConceptNode "eyes"
FloatValue X Y
*/
cent = dr->sal_d.sal_point(gr, gr);
pap = createFloatValue(std::vector<double>({(double)cent.x, (double)cent.y}));
ey->setValue(lk, pap);
#ifdef _DEBUG_ //XXX REMOVE WHEN DONE
cv::circle(_dbg_img, cent, 2.0, cv::Scalar(255, 0, 0), 3);
#endif
/*
hand detection and finger counting
** hand detection **
Valuation
PredicateNode "position"
ConceptNode "hand"
FloatValue X Y
** finger count ** XXX not sure if this is the right way
Valuation
ConceptNode "fingers"
ConceptNode "hand_x"
FloatValue X
*/
cv::threshold(he, he, 70, 255, CV_THRESH_BINARY_INV | CV_THRESH_OTSU);
hands = dr->dh.Transform(he);
for(idx = 0; idx < hands.size(); idx++) {
img = cv::Mat(he, cv::Rect(hands[idx].x - hands[idx].x*SCALE,
hands[idx].y - hands[idx].y*SCALE,
hands[idx].width + hands[idx].width*SCALE,
hands[idx].height + hands[idx].height*SCALE));
str = "hand_" + std::to_string(idx);
h = dr->_as->add_node(CONCEPT_NODE, str.c_str());
pap = createFloatValue(std::vector<double>({
(double)hands[idx].x+(hands[idx].width/2),
(double)hands[idx].y+(hands[idx].height/2)}));
h->setValue(pos_h, pap);
pap = createFloatValue((float)dr->fc.num_fingers(img));
h->setValue(fi, pap);
#ifdef _DEBUG_ //XXX REMOVE WHEN DONE
cv::rectangle(_dbg_img, hands[idx], cv::Scalar(255, 0, 0), 2, 1);
for(didx = 0; didx < dr->fc.f_tips.size(); didx++)
cv::circle(_dbg_img, cv::Point(dr->fc.f_tips[didx].x + hands[idx].x,
dr->fc.f_tips[didx].y + hands[idx].y),
2, cv::Scalar(0, 0, 255), 2);
#endif
}
#ifdef _DEBUG_
cv::imshow("debug", _dbg_img);
cv::waitKey(10);
#endif
usleep(10000);
} //while ok
}
void DrRoboto::start_sense()
{
run = new thread(DrRoboto::sensor_loop, this);
}
void DrRoboto::stop_sense()
{
ok = false;
run->join();
delete run;
}
void DrRoboto::say(std::string data)
{
tts.setSpeaker(FestivalTTS::speaker::DON);
tts.speak(data);
}
void init_dr_roboto()
{
DrRoboto *dr = new DrRoboto();
define_scheme_primitive("c-init-as", &DrRoboto::init_as, dr);
define_scheme_primitive("c-start-sensors", &DrRoboto::start_sense, dr);
define_scheme_primitive("c-stop-sensors", &DrRoboto::stop_sense, dr);
define_scheme_primitive("c-say", &DrRoboto::say, dr);
#ifdef _DEBUG_
define_scheme_primitive("c-dbg-cap-from-file", &DrRoboto::cap_from_file, dr);
#endif
}