visageSDK
AndroidWrapper.cpp
Go to the documentation of this file.
1 #include <jni.h>
2 #include <EGL/egl.h>
3 #include <GLES/gl.h>
4 #include <vector>
5 #include <stdio.h>
6 #include <unistd.h>
7 #include "VisageTracker.h"
8 #include "VisageRendering.h"
9 #include "AndroidImageCapture.h"
10 #include "AndroidCameraCapture.h"
11 #include "VisageLivenessAction.h"
12 #include "VisageLivenessBrowRaise.h"
13 #include "VisageLivenessSmile.h"
14 #include "VisageLivenessBlink.h"
15 #include "LivenessGUI.h"
16 #include <random>
17 #include "LicenseString.h"
18 
19 #include <android/log.h>
20 #define LOG_TAG "TrackerWrapper"
21 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
22 #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
23 
24 using namespace VisageSDK;
25 
26 // neccessary prototype declaration for licensing
27 namespace VisageSDK
28 {
29  int initializeLicenseManager(JNIEnv* env, jobject obj, const char *licenseKeyFileName, void (*alertFunction)(const char*) = 0);
30 }
31 
32 static AndroidImageCapture *a_cap_image = 0;
33 static AndroidCameraCapture *a_cap_camera = 0;
34 static VsImage *drawImageBuffer = 0;
35 static VsImage *renderImage = 0;
36 
37 static float m_fps = 0;
38 
39 void Sleep(int ms) {usleep(ms*1000);}
40 
54 extern "C" {
55 
56 // ********************************
57 // Variables used in tracking thread
58 // ********************************
59 const int MAX_FACES = 4;
60 static VisageTracker *m_Tracker = 0;
61 static FaceData trackingData[MAX_FACES];
62 int *trackingStatus = 0;
63 float displayFramerate = 0;
64 int timestamp = 0;
65 int trackingTime;
66 
67 // ********************************
68 // Buffers for track->render communication
69 // ********************************
70 static FaceData trackingDataBuffer[MAX_FACES];
71 int trackingStatusBuffer[MAX_FACES];
72 
73 // ********************************
74 // Variables used for liveness
75 // ********************************
76 
81 static const int MAX_ACTION_STATES = 5;
85 static const int NUM_ACTIONS = 3;
86 
90 struct Action
91 {
92  VisageLivenessAction *action;
93  std::string images[MAX_ACTION_STATES];
94  std::string messages[MAX_ACTION_STATES];
95 };
96 
101 
107 
113 
119 
120 bool liveness = false;
121 
122 int currAction = 0;
123 int actionState = -1;
124 int timePassed = 0;
125 long startTime_liveness;
126 long currentTime_liveness;
127 
128 LivenessGUI *GUI;
129 
130 
131 
132 // ********************************
133 // Variables used in rendering thread
134 // ********************************
135 static FaceData trackingDataRender[MAX_FACES];
136 int trackingStatusRender[MAX_FACES];
137 // Logo image
138 VsImage* logo = 0;
139 VsImage* faceContourImage = 0;
140 VsImage* fontImage = 0;
141 const char* faceContourImagePath = 0;
142 
143 // ********************************
144 // Control flow variables
145 // ********************************
146 bool trackingOk = false;
147 bool isTracking = false;
148 bool orientationChanged = false;
149 bool trackerPaused = false;
150 bool trackerStopped;
151 //
152 int camOrientation;
153 int camHeight;
154 int camWidth;
155 int camFlip;
156 //
157 pthread_mutex_t displayRes_mutex;
158 pthread_mutex_t guardFrame_mutex;
159 //
160 
161 GLuint texIds[3];
162 
166 GLuint frameTexId = 0;
167 
168 GLuint instructionsTexId = 0;
169 
173 float xTexCoord;
174 
178 float yTexCoord;
179 
184 
189 
194 
199 
204 
205 // ********************************
206 // JNI variables
207 // ********************************
208 JNIEnv* _env;
209 jobject _obj;
210 
211 
212 // ********************************
213 // Helper functions
214 // ********************************
215 
221 void AlertCallback(const char* warningMessage)
222 {
223  jclass dataClass = _env->FindClass("com/visagetechnologies/visagetrackerdemo/TrackerActivity");
224  if (_env->ExceptionCheck())
225  _env->ExceptionClear();
226  if (dataClass != NULL)
227  {
228  jclass javaClassRef = (jclass) _env->NewGlobalRef(dataClass);
229  jmethodID javaMethodRef = _env->GetMethodID(javaClassRef, "AlertDialogFunction", "(Ljava/lang/String;)V");
230  if (_env->ExceptionCheck())
231  _env->ExceptionClear();
232  jstring message = _env->NewStringUTF(warningMessage);
233  if (javaMethodRef != 0)
234  _env->CallVoidMethod(_obj, javaMethodRef, message);
235 
236  _env->DeleteGlobalRef(javaClassRef);
237  _env->DeleteLocalRef(message);
238  }
239 }
240 
244 long getTimeNsec() {
245  struct timespec now;
246  clock_gettime(CLOCK_REALTIME, &now);
247  return (long) ((now.tv_sec*1000000000LL + now.tv_nsec)/1000000LL);
248 }
249 
250 void SetLivenessActions()
251 {
252  BrowRaiseAction = new VisageLivenessBrowRaise();
253  SmileAction = new VisageLivenessSmile();
254  BlinkAction = new VisageLivenessBlink();
255 
256  actions[0].action = BrowRaiseAction;
258  actions[0].messages[VisageLivenessBrowRaise::STATE_ACTION_INITIALIZING] = "";
259  actions[0].images[VisageLivenessBrowRaise::STATE_WAIT_FOR_FRONTAL] = "";
260  actions[0].messages[VisageLivenessBrowRaise::STATE_WAIT_FOR_FRONTAL] = "Keep your head frontal!";
261  actions[0].images[VisageLivenessBrowRaise::STATE_WAIT_FOR_STABLE] = "";
262  actions[0].messages[VisageLivenessBrowRaise::STATE_WAIT_FOR_STABLE] = "Keep your head stable!";
263  actions[0].images[VisageLivenessBrowRaise::STATE_WAIT_FOR_ACTION] = "";
264  actions[0].messages[VisageLivenessBrowRaise::STATE_WAIT_FOR_ACTION] = "Raise your eyebrows!";
265  actions[0].images[VisageLivenessBrowRaise::STATE_ACTION_VERIFIED] = "";
266  actions[0].messages[VisageLivenessBrowRaise::STATE_ACTION_VERIFIED] = "";
267 
268  actions[1].action = SmileAction;
269  actions[1].images[VisageLivenessSmile::STATE_ACTION_INITIALIZING] = "";
270  actions[1].messages[VisageLivenessSmile::STATE_ACTION_INITIALIZING] = "";
271  actions[1].images[VisageLivenessSmile::STATE_WAIT_FOR_FRONTAL] = "";
272  actions[1].messages[VisageLivenessSmile::STATE_WAIT_FOR_FRONTAL] = "Keep your head frontal!";
273  actions[1].images[VisageLivenessSmile::STATE_WAIT_FOR_STABLE] = "";
274  actions[1].messages[VisageLivenessSmile::STATE_WAIT_FOR_STABLE] = "Keep your head stable!";
275  actions[1].images[VisageLivenessSmile::STATE_WAIT_FOR_ACTION] = "";
276  actions[1].messages[VisageLivenessSmile::STATE_WAIT_FOR_ACTION] = "Smile!";
277  actions[1].images[VisageLivenessSmile::STATE_ACTION_VERIFIED] = "";
278  actions[1].messages[VisageLivenessSmile::STATE_ACTION_VERIFIED] = "";
279 
280  actions[2].action = BlinkAction;
281  actions[2].images[VisageLivenessBlink::STATE_ACTION_INITIALIZING] = "";
282  actions[2].messages[VisageLivenessBlink::STATE_ACTION_INITIALIZING] = "";
283  actions[2].images[VisageLivenessBlink::STATE_WAIT_FOR_FRONTAL] = "";
284  actions[2].messages[VisageLivenessBlink::STATE_WAIT_FOR_FRONTAL] = "Keep your head frontal!";
285  actions[2].images[VisageLivenessBlink::STATE_WAIT_FOR_STABLE] = "";
286  actions[2].messages[VisageLivenessBlink::STATE_WAIT_FOR_STABLE] = "Keep your head stable!";
287  actions[2].images[VisageLivenessBlink::STATE_WAIT_FOR_ACTION] = "";
288  actions[2].messages[VisageLivenessBlink::STATE_WAIT_FOR_ACTION] = "Blink!";
289  actions[2].images[VisageLivenessBlink::STATE_ACTION_VERIFIED] = "";
290  actions[2].messages[VisageLivenessBlink::STATE_ACTION_VERIFIED] = "";
291 }
292 
293 void RandomizeActions()
294 {
295  std::random_device rd;
296  std::mt19937 g(rd());
297 
298  std::shuffle(&actions[0], &actions[3], g);
299 }
300 
301 // ********************************
302 
303 // ********************************
304 // Wrapper function
305 // ********************************
306 
307 
308 static bool endsWith(const std::string& str, const std::string& suffix)
309 {
310  return str.size() >= suffix.size() && 0 == str.compare(str.size()-suffix.size(), suffix.size(), suffix);
311 }
312 
321 void
323  jbyteArray logo_, jint width,
324  jint height) {
325 
326  jbyte *logoBytes = env->GetByteArrayElements(logo_, 0);
327 
328  pthread_mutex_lock(&displayRes_mutex);
329 
330  if (logo) {
331 
332  pthread_mutex_unlock(&displayRes_mutex);
333  env->ReleaseByteArrayElements(logo_, logoBytes, 0);
334  return;
335  }
336 
337  logo = vsCreateImage(vsSize(width, height), VS_DEPTH_8U, 4);
338  memcpy(logo->imageData, logoBytes, logo->imageSize);
339 
340  pthread_mutex_unlock(&displayRes_mutex);
341 
342  env->ReleaseByteArrayElements(logo_, logoBytes, 0);
343 }
344 
353  jstring path, jbyteArray faceContourImage_, jint width,
354  jint height) {
355 
356  jbyte *faceContourImageBytes = env->GetByteArrayElements(faceContourImage_, 0);
357 
358  pthread_mutex_lock(&displayRes_mutex);
359 
360  if (faceContourImage) {
361 
362  pthread_mutex_unlock(&displayRes_mutex);
363  env->ReleaseByteArrayElements(faceContourImage_, faceContourImageBytes, 0);
364  return;
365  }
366 
367  faceContourImage = vsCreateImage(vsSize(width, height), VS_DEPTH_8U, 4);
368  memcpy(faceContourImage->imageData, faceContourImageBytes, faceContourImage->imageSize);
369  faceContourImagePath = env->GetStringUTFChars(path, 0);
370 
371  GUI->setImage(faceContourImagePath, faceContourImage);
372 
373  pthread_mutex_unlock(&displayRes_mutex);
374 
375  env->ReleaseByteArrayElements(faceContourImage_, faceContourImageBytes, 0);
376 }
377 
386  jbyteArray fontImage_, jint width,
387  jint height) {
388  jbyte *fontImageBytes = env->GetByteArrayElements(fontImage_, 0);
389 
390  pthread_mutex_lock(&displayRes_mutex);
391 
392  if (fontImage) {
393 
394  pthread_mutex_unlock(&displayRes_mutex);
395  env->ReleaseByteArrayElements(fontImage_, fontImageBytes, 0);
396  return;
397  }
398 
399  fontImage = vsCreateImage(vsSize(width, height), VS_DEPTH_8U, 4);
400  memcpy(fontImage->imageData, fontImageBytes, fontImage->imageSize);
401  pthread_mutex_unlock(&displayRes_mutex);
402 
403  env->ReleaseByteArrayElements(fontImage_, fontImageBytes, 0);
404 }
405 
412 void Java_com_visagetechnologies_visagetrackerdemo_TrackerActivity_TrackerInit(JNIEnv *env, jobject obj, jstring pathToAssets, jstring configFilename)
413 {
414  liveness = false;
415  _env = env;
416  _obj = obj;
417  const char *_pathToAssets = env->GetStringUTFChars(pathToAssets, 0);
418  trackerStopped = false;
419 
420  //example how to initialize license key
421  std::string pathToLicense(_pathToAssets + std::string("/") + licenseKey);
422  if (endsWith(licenseKey, ".vlc"))
423  initializeLicenseManager(env, obj, pathToLicense.c_str(), AlertCallback);
424  else
425  initializeLicenseManager(env, obj, licenseKey.c_str(), AlertCallback); //for embedded license
426 
427  if (fontImage)
429 
430  //Set up mutex for track->render thread synchronization
431  pthread_mutex_destroy(&displayRes_mutex);
432  pthread_mutex_init(&displayRes_mutex, NULL);
433  //Set up mutex for tracking->stopping tracking synchronization
434  pthread_mutex_destroy(&guardFrame_mutex);
435  pthread_mutex_init(&guardFrame_mutex, NULL);
436 
437  //Delete previously allocated objects
438  delete a_cap_camera;
439  a_cap_camera = 0;
440  a_cap_image = 0;
441 
442  const char *_configFilename = env->GetStringUTFChars(configFilename, 0);
443 
444  std::string pathToConfig = _pathToAssets + std::string("/") + _configFilename;
445  //Initialize tracker
446  m_Tracker = new VisageTracker((_pathToAssets + std::string("/Facial Features Tracker.cfg")).c_str());
447 
448 // LOGI("%s", _configFilename);
449  env->ReleaseStringUTFChars(pathToAssets, _pathToAssets);
450 }
451 
452 void Java_com_visagetechnologies_visagetrackerdemo_TrackerActivity_LivenessInit(JNIEnv *env, jobject obj)
453 {
454  GUI = new LivenessGUI();
455  liveness = true;
456  SetLivenessActions();
457  RandomizeActions();
458 
459 }
460 
461 void Java_com_visagetechnologies_visagetrackerdemo_TrackerActivity_RestartLiveness(JNIEnv *env, jobject obj)
462 {
463  for (int i = 0; i < NUM_ACTIONS; i++)
464  actions[i].action->reset();
465 
466  currAction = 0;
467  RandomizeActions();
468 }
469 
476 {
477  trackerPaused = true;
478 
479 if (liveness)
480  {
481  for (int i = 0; i < NUM_ACTIONS; i++)
482  actions[i].action->reset();
483 
484  currAction = 0;
485 
486  RandomizeActions();
487  }
488 }
489 
496 void Java_com_visagetechnologies_visagetrackerdemo_TrackerActivity_TrackFromImage(JNIEnv *env, jobject obj, jint width, jint height )
497 {
498  while(!trackerStopped)
499  {
500  if (m_Tracker && a_cap_image && !trackerStopped)
501  {
502  pthread_mutex_lock(&guardFrame_mutex);
503  long ts;
504  drawImageBuffer = a_cap_image->GrabFrame(ts);
505  if (!renderImage)
506  renderImage = vsCloneImage(drawImageBuffer);
507  if (trackerStopped || drawImageBuffer == 0)
508  {
509  pthread_mutex_unlock(&guardFrame_mutex);
510  return;
511  }
512  long startTime = getTimeNsec();
513  trackingStatus = m_Tracker->track(width, height, (const char*)drawImageBuffer->imageData, trackingData, VISAGE_FRAMEGRABBER_FMT_RGB, VISAGE_FRAMEGRABBER_ORIGIN_TL, 0, -1, MAX_FACES);
514  long endTime = getTimeNsec();
515  trackingTime = (int)(endTime - startTime);
516  LOGE("Tracking time is %d", trackingTime);
517  pthread_mutex_unlock(&guardFrame_mutex);
518  pthread_mutex_lock(&displayRes_mutex);
519  for (int i=0; i<MAX_FACES; i++)
520  {
521  if (trackingStatus[i] == TRACK_STAT_OFF)
522  continue;
523  std::swap(trackingDataBuffer[i], trackingData[i]);
524  trackingStatusBuffer[i] = trackingStatus[i];
525  trackingOk = true;
526 
527  }
528  isTracking = true;
529  pthread_mutex_unlock(&displayRes_mutex);
530  }
531  else
532  Sleep(100);
533 
534  }
535  return;
536 }
537 
549 int Java_com_visagetechnologies_visagetrackerdemo_TrackerActivity_setParameters(JNIEnv *env, jobject obj, jint width, jint height, jint orientation, jint flip)
550 {
551  camOrientation = orientation;
552  camHeight = height;
553  camWidth = width;
554  camFlip = flip;
555 
556  //Dispose of the previous drawImageBuffer
557  if (!drawImageBuffer)
558  {
559  vsReleaseImage(&drawImageBuffer);
560  drawImageBuffer = 0;
561  }
562 
563  //Depending on the camera orientation (landscape or portrait), create a drawImageBuffer buffer for storing pixels that will be used in the tracking thread
564  if (camOrientation == 90 || camOrientation == 270)
565  drawImageBuffer = vsCreateImage(vsSize(height, width), VS_DEPTH_8U, 3);
566  else
567  drawImageBuffer = vsCreateImage(vsSize(width, height), VS_DEPTH_8U, 3);
568 
569  //Dispose of the previous drawImage
570  vsReleaseImage(&renderImage);
571  renderImage = 0;
572 
573  //Create a renderImage buffer based on the drawImageBuffer which will be used in the rendering thread
574  //NOTE: Copying imageData between track and draw buffers is protected with mutexes
575  renderImage = vsCloneImage(drawImageBuffer);
576 
577  orientationChanged = true;
578 
579  return 0;
580 }
581 
589 {
590  while (!trackerStopped)
591  {
592  if (m_Tracker && a_cap_camera && !trackerStopped && !trackerPaused)
593  {
594  pthread_mutex_lock(&guardFrame_mutex);
595  long ts;
596  VsImage *trackImage = a_cap_camera->GrabFrame(ts);
597  if (trackerStopped || trackImage == 0)
598  {
599  pthread_mutex_unlock(&guardFrame_mutex);
600  return;
601  }
602  long startTime = getTimeNsec();
603  if (camOrientation == 90 || camOrientation == 270)
604  trackingStatus = m_Tracker->track(camHeight, camWidth, trackImage->imageData, trackingData, VISAGE_FRAMEGRABBER_FMT_RGB, VISAGE_FRAMEGRABBER_ORIGIN_TL, 0, -1, MAX_FACES);
605  else
606  trackingStatus = m_Tracker->track(camWidth, camHeight, trackImage->imageData, trackingData, VISAGE_FRAMEGRABBER_FMT_RGB, VISAGE_FRAMEGRABBER_ORIGIN_TL, 0, -1, MAX_FACES);
607  long endTime = getTimeNsec();
608  trackingTime = (int)endTime - startTime;
609 
610  pthread_mutex_unlock(&guardFrame_mutex);
611 
612  //***
613  //*** LOCK render thread while copying data for rendering ***
614  //***
615  pthread_mutex_lock(&displayRes_mutex);
616  for (int i=0; i<MAX_FACES; i++)
617  {
618  if (trackingStatus[i] == TRACK_STAT_OFF)
619  continue;
620  std::swap(trackingDataBuffer[i], trackingData[i]);
621  trackingStatusBuffer[i] = trackingStatus[i];
622  //Signalize that at least one face was tracked
623  trackingOk = true;
624 
625  }
626  isTracking = true;
627  vsCopy(trackImage, drawImageBuffer);
628 
629  //***
630  //*** UNLOCK render thread ***
631  //***
632  pthread_mutex_unlock(&displayRes_mutex);
633  }
634  else
635  Sleep(1);
636  }
637  return;
638 }
639 
640 
641 void Java_com_visagetechnologies_visagetrackerdemo_TrackerActivity_PerformLiveness(JNIEnv *env, jobject obj)
642 {
643  for (int i = 0; i < NUM_ACTIONS; i++)
644  actions[i].action->reset();
645  timePassed = 0;
646  startTime_liveness = getTimeNsec();
647 
648  currAction = 0;
649  actionState = -1;
650 
651  while (!trackerStopped)
652  {
653  if (m_Tracker && a_cap_camera && !trackerStopped && !trackerPaused)
654  {
655  pthread_mutex_lock(&guardFrame_mutex);
656  long ts;
657  VsImage *trackImage = a_cap_camera->GrabFrame(ts);
658  if (trackerStopped || trackImage == 0)
659  {
660  pthread_mutex_unlock(&guardFrame_mutex);
661  return;
662  }
663  if (currAction >= NUM_ACTIONS)
664  {
665  vsCopy(trackImage, drawImageBuffer);
666  trackingStatusBuffer[0] = trackingStatus[0];
667  pthread_mutex_unlock(&guardFrame_mutex);
668  continue;
669  }
670  long startTime = getTimeNsec();
671  if (camOrientation == 90 || camOrientation == 270)
672  trackingStatus = m_Tracker->track(camHeight, camWidth, trackImage->imageData, trackingData, VISAGE_FRAMEGRABBER_FMT_RGB, VISAGE_FRAMEGRABBER_ORIGIN_TL, 0, -1);
673  else
674  trackingStatus = m_Tracker->track(camWidth, camHeight, trackImage->imageData, trackingData, VISAGE_FRAMEGRABBER_FMT_RGB, VISAGE_FRAMEGRABBER_ORIGIN_TL, 0, -1);
675  long endTime = getTimeNsec();
676 
677  pthread_mutex_unlock(&guardFrame_mutex);
678 
679  trackingTime = (int)endTime - startTime;
680 
681  if (trackingStatus[0] == TRACK_STAT_OK)
682  {
683  actionState = actions[currAction].action->update(trackingData, trackImage);
684  currentTime_liveness = getTimeNsec();
685  timePassed = (int)currentTime_liveness - startTime_liveness;
686  }
687  else
688  {
689  //reset liveness detection if face is lost
690  for (int i = 0; i < NUM_ACTIONS; i++)
691  actions[i].action->reset();
692  currAction = 0;
693  RandomizeActions();
694  }
695 
696  if (actionState == 0)
697  {
698  currAction++;
699  startTime_liveness = getTimeNsec();
700  }
701 
702  //***
703  //*** LOCK render thread while copying data for rendering ***
704  //***
705  pthread_mutex_lock(&displayRes_mutex);
706  if (trackingStatus[0] != TRACK_STAT_OFF)
707  {
708  vsCopy(trackImage, drawImageBuffer);
709  trackingStatusBuffer[0] = trackingStatus[0];
710  trackingOk = true;
711  }
712  isTracking = true;
713 
714  //***
715  //*** UNLOCK render thread ***
716  //***
717  pthread_mutex_unlock(&displayRes_mutex);
718  }
719  else
720  Sleep(1);
721 
722 
723  }
724  return;
725 }
726 
727 void ReleaseActions()
728 {
729  BrowRaiseAction = 0;
730  delete BrowRaiseAction;
731 
732  SmileAction = 0;
733  delete SmileAction;
734 
735  BlinkAction = 0;
736  delete BlinkAction;
737 }
738 
739 
744 {
745  if (m_Tracker)
746  {
747  trackerStopped = true;
748  trackingOk =false;
749  pthread_mutex_lock(&guardFrame_mutex);
750  for (int i=0; i<MAX_FACES; i++)
751  {
752  trackingStatusRender[i] = TRACK_STAT_OFF;
753  trackingStatusBuffer[i] = TRACK_STAT_OFF;
754  trackingStatus[i] = TRACK_STAT_OFF;
755  }
756  m_Tracker->stop();
757  delete m_Tracker;
758  m_Tracker = 0;
759  vsReleaseImage(&drawImageBuffer);
760  drawImageBuffer = 0;
761  vsReleaseImage(&renderImage);
762  renderImage = 0;
763  VisageRendering::Reset();
764  pthread_mutex_unlock(&guardFrame_mutex);
765 
766  }
767 
768  if (liveness)
769  ReleaseActions();
770 }
771 
772 
779 void Java_com_visagetechnologies_visagetrackerdemo_ImageTrackerView_WriteFrameImage(JNIEnv *env, jobject obj, jbyteArray frame, jint width, jint height)
780 {
781  if (!a_cap_image)
782  a_cap_image = new AndroidImageCapture(width, height, VISAGE_FRAMEGRABBER_FMT_RGB);
783  jbyte *f = env->GetByteArrayElements(frame, 0);
784  a_cap_image->WriteFrame((unsigned char *)f, (int)width, (int)height);
785  env->ReleaseByteArrayElements(frame, f, 0);
786 }
787 
788 
799 {
800  if (trackerStopped)
801  return;
802 
803  //Reinitialize if the parameters changed or initialize if it is the first time
804  if (!a_cap_camera || orientationChanged)
805  {
806  delete a_cap_camera;
807  a_cap_camera = new AndroidCameraCapture(camWidth, camHeight, camOrientation, camFlip);
808  orientationChanged = false;
809  trackerPaused = false;
810  }
811  //
812  jbyte *f = env->GetByteArrayElements(frame, 0);
813  //Write frames from Java to native
814  a_cap_camera->WriteFrameYUV((unsigned char *)f);
815  env->ReleaseByteArrayElements(frame, f, 0);
816 }
817 
818 
829 bool Java_com_visagetechnologies_visagetrackerdemo_TrackerRenderer_displayTrackingStatus(JNIEnv* env, jobject obj, jint width, jint height)
830 {
831  glWidth = width;
832  glHeight = height;
833 
834  if (!m_Tracker || trackerStopped || !isTracking || !drawImageBuffer || trackerPaused)
835  return false;
836  //***
837  //*** LOCK track thread to copy data for rendering ***
838  //***
839  pthread_mutex_lock(&displayRes_mutex);
840  //copy image for rendering
841  vsCopy(drawImageBuffer, renderImage);
842  //copy faceData and statuses
843  if (!liveness)
844  {
845  for (int i=0; i<MAX_FACES; i++)
846  {
847  if (trackingStatusBuffer[i] == TRACK_STAT_OFF)
848  continue;
849  trackingDataRender[i] = trackingDataBuffer[i];
850  trackingStatusRender[i] = trackingStatusBuffer[i];
851  }
852  }
853  else
854  trackingStatusRender[0] = trackingStatusBuffer[0];
855  //***
856  //*** UNLOCK track thread ***
857  //***
858  pthread_mutex_unlock(&displayRes_mutex);
859 
860  //calculate aspect corrected width and height
861  videoAspect = renderImage->width / (float) renderImage->height;
862 
863  float tmp;
864  if(renderImage->width < renderImage->height)
865  {
866  tmp = glHeight;
867  glHeight = glWidth / videoAspect;
868  if (glHeight > tmp)
869  {
870  glWidth = glWidth*tmp/glHeight;
871  glHeight = tmp;
872  }
873  }
874  else
875  {
876  tmp = glWidth;
877  glWidth = glHeight * videoAspect;
878  if (glWidth > tmp)
879  {
880  glHeight = glHeight*tmp/glWidth;
881  glWidth = tmp;
882  }
883  }
884 
885  //Render tracking results for the first face and display frame
886  VisageRendering::DisplayResults(&trackingDataRender[0], trackingStatusRender[0], glWidth, glHeight, renderImage, DISPLAY_FRAME);
887  if (logo)
888  VisageRendering::DisplayLogo(logo, glWidth, glHeight);
889  //Render tracking results for rest of the faces without rendering the frame
890  if (!liveness)
891  {
892  for (int i=0; i<MAX_FACES; i++)
893  {
894  if(trackingStatusRender[i] == TRACK_STAT_OK)
895 
896  VisageRendering::DisplayResults(&trackingDataRender[i], trackingStatusRender[i], glWidth, glHeight, renderImage, DISPLAY_DEFAULT - DISPLAY_FRAME);
897  }
898  }
899  else
900  {
901  if (currAction < NUM_ACTIONS)
902  {
903  if (trackingStatusRender[0] == TRACK_STAT_OK)
904  GUI->promptUser(actions[currAction].messages[actionState].c_str(), actions[currAction].images[actionState].c_str(), true);
905  else
906  {
907  // if tracker was unable to find face, display bounding box as instruction on where face needs to be positioned
908  GUI->promptUser("", faceContourImagePath, true);
909  }
910  }
911  else
912  {
913  //keep displaying frame after liveness is confirmed
914  GUI->promptUser("Liveness confirmed!", "", true);
915  }
916  }
917 
918  return true;
919 }
920 
921 
922 
929 {
930  return trackingData[0].frameRate;
931 }
932 
939 {
940  return trackingTime;
941 }
942 
943 
949 JNIEXPORT jstring JNICALL Java_com_visagetechnologies_visagetrackerdemo_TrackerActivity_GetStatus(JNIEnv* env, jobject obj)
950 {
951  char* msg;
952 
953  for (int i=0; i<MAX_FACES; i++)
954  {
955  if (trackingStatusBuffer[i] == TRACK_STAT_OK)
956  return env->NewStringUTF("OK");
957  }
958 
959  for (int i=0; i<MAX_FACES; i++)
960  {
961  if (trackingStatusBuffer[i] == TRACK_STAT_RECOVERING)
962  return env->NewStringUTF("RECOVERING");
963  }
964 
965  for (int i=0; i<MAX_FACES; i++)
966  {
967  if (trackingStatusBuffer[i] == TRACK_STAT_INIT)
968  return env->NewStringUTF("INITIALIZING");
969  }
970 
971  return env->NewStringUTF("OFF");
972 }
973 
974 float Java_com_visagetechnologies_visagetrackerdemo_TrackerRenderer_getTrackerFps( JNIEnv* env )
975 {
976  return m_fps;
977 }
978 
979 
980 float Java_com_visagetechnologies_visagetrackerdemo_TrackerActivity_GetDisplayFps(JNIEnv* env)
981 {
982  return displayFramerate;
983 }
984 
985 
986 bool Java_com_visagetechnologies_visagetrackerdemo_TrackerView_IsAutoStopped(JNIEnv* env)
987 {
988  //RESET VARIABLES IN WRAPPER
989  if (m_Tracker)
990  {
991 
992  for (int i=0; i<MAX_FACES; i++)
993  {
994  if (trackingStatus[i] != TRACK_STAT_OK)
995  return true;
996  }
997  return false;
998 
999  }
1000  else
1001  return false;
1002 }
1003 
1004 }
int height
Image height in pixels.
static void DisplayResults(FaceData *trackingData, int trackStat, int width, int height, VsImage *frame, int drawingOptions=DISPLAY_DEFAULT)
Method calls other methods for drawing the frame and tracking results.
VisageTracker is a face tracker capable of tracking the head pose, facial features and gaze for multi...
JNIEXPORT jstring JNICALL Java_com_visagetechnologies_visagetrackerdemo_TrackerActivity_GetStatus(JNIEnv *env, jobject obj)
Method for getting the tracking status information from tracker.
void Java_com_visagetechnologies_visagetrackerdemo_TrackerActivity_TrackFromImage(JNIEnv *env, jobject obj, jint width, jint height)
Method for starting tracking in image.
VisageLivenessAction is an abstract class that can be used to implement algorithm which searches for ...
AndroidImageCapture is class that implements a simple frame grabber that serves a single image to the...
long getTimeNsec()
Simple timer function.
VisageLivenessAction * SmileAction
VisageLivenessSmile object.
GLuint frameTexId
Texture ID for displaying frames from the tracker.
int glHeight
Size of the OpenGL view.
void Java_com_visagetechnologies_visagetrackerdemo_JavaCamTrackerView_WriteFrameCamera(JNIEnv *env, jobject obj, jbyteArray frame)
Writes raw image data into VisageSDK::AndroidCameraCapture object.
float videoAspect
Aspect of the video.
int yTexSize
Size of the texture for displaying frames from the tracker.
float frameRate
The frame rate of the tracker, in frames per second, measured over last 10 frames.
Definition: FaceData.h:282
static const int STATE_ACTION_VERIFIED
Describes action&#39;s state where the action was performed and verified.
VisageLivenessAction * BrowRaiseAction
VisageLivenessBrowRaise object.
char * imageData
Pointer to aligned image data.
static const int STATE_WAIT_FOR_ACTION
Describes action&#39;s state when eyebrows raise is trying to be verified.
void Java_com_visagetechnologies_visagetrackerdemo_TrackerActivity_TrackerStop(JNIEnv *env, jobject obj)
Stops the tracker and cleans memory.
VisageLivenessSmile is a class that detects whether the person on the live video stream has smiled...
float Java_com_visagetechnologies_visagetrackerdemo_TrackerActivity_GetFps(JNIEnv *env, jobject obj)
Method for getting frame rate information from the tracker.
void Java_com_visagetechnologies_visagetrackerdemo_TrackerActivity_WriteFontImage(JNIEnv *env, jclass type, jbyteArray fontImage_, jint width, jint height)
Creates VsImage object for font image received from TrackerActivity.
static const int MAX_ACTION_STATES
Maximal number of states that the actions will have.
void Java_com_visagetechnologies_visagetrackerdemo_ImageTrackerView_WriteFrameImage(JNIEnv *env, jobject obj, jbyteArray frame, jint width, jint height)
Writes raw image data into VisageSDK::AndroidImageCapture object.
virtual int update(const FaceData *faceData, VsImage *frame=0)=0
Method used to process input data in order to confirm that the action has occured.
static const int STATE_ACTION_INITIALIZING
Describes action&#39;s state while the action is initializing, i.e.
void WriteFrame(unsigned char *imageData, int width, int height)
Method for writing imageData to buffer object.
void AlertCallback(const char *warningMessage)
Callback method for license notification.
void Java_com_visagetechnologies_visagetrackerdemo_TrackerActivity_TrackFromCam(JNIEnv *env, jobject obj)
Method for starting tracking from camera.
int xTexSize
Size of the texture for displaying frames from the tracker.
void Java_com_visagetechnologies_visagetrackerdemo_TrackerActivity_WriteFaceContourImage(JNIEnv *env, jclass type, jstring path, jbyteArray faceContourImage_, jint width, jint height)
Creates VsImage object for face countour image received from TrackerActivity.
static const int NUM_ACTIONS
Number of actions that will need to be verified in order to confirm liveness.
VisageLivenessBrowRaise is a class that detects whether the person on the live video stream has raise...
int width
Image width in pixels.
virtual int * track(int frameWidth, int frameHeight, const char *p_imageData, FaceData *facedata, int format=VISAGE_FRAMEGRABBER_FMT_RGB, int origin=VISAGE_FRAMEGRABBER_ORIGIN_TL, int widthStep=0, long timeStamp=-1, int maxFaces=1)
Performs face tracking (one or more faces) in the given image and returns tracking results and status...
VisageLivenessAction * BlinkAction
VisageLivenessBlink object.
bool Java_com_visagetechnologies_visagetrackerdemo_TrackerRenderer_displayTrackingStatus(JNIEnv *env, jobject obj, jint width, jint height)
Method for displaying tracking results.
int Java_com_visagetechnologies_visagetrackerdemo_TrackerActivity_setParameters(JNIEnv *env, jobject obj, jint width, jint height, jint orientation, jint flip)
Method that sets frame parameters.
VsImage * GrabFrame(long &timeStamp)
This function is called to get the frame to process.
void Java_com_visagetechnologies_visagetrackerdemo_TrackerActivity_TrackerInit(JNIEnv *env, jobject obj, jstring pathToAssets, jstring configFilename)
Method for initializing the tracker.
Structure that holds the action and its accompanying messages and images that will be displayed to us...
static const int STATE_ACTION_INITIALIZING
Describes action&#39;s state while the action is initializing, i.e.
float yTexCoord
Texture coordinates for displaying frames from the tracker.
static void SetFontTexture(const VsImage *font_tex)
Method sets font texture.
int imageSize
Image data size in bytes (==image->height*image->widthStep in case of interleaved data) ...
void Java_com_visagetechnologies_visagetrackerdemo_TrackerActivity_WriteLogoImage(JNIEnv *env, jclass type, jbyteArray logo_, jint width, jint height)
Creates VsImage object for logo received from TrackerActivity.
int Java_com_visagetechnologies_visagetrackerdemo_TrackerActivity_GetTrackTime(JNIEnv *env, jobject obj)
Method for getting frame rate information from the tracker.
static const int STATE_WAIT_FOR_FRONTAL
Describes action&#39;s state where face in the frame is not frontal (head rotations larger than 0...
static const int STATE_WAIT_FOR_ACTION
Describes action&#39;s state when smile is trying to be verified.
Namespace.
Definition: android_header.h:1
int glWidth
Size of the OpenGL view.
static const int STATE_WAIT_FOR_STABLE
Describes action&#39;s state when user is not keeping his head still.
VsImage * GrabFrame(long &timeStamp)
This function is called periodically to get the new video frame to process.
void Java_com_visagetechnologies_visagetrackerdemo_TrackerActivity_PauseTracker(JNIEnv *env, jobject obj)
Method for "pausing" tracking.
Face data structure, used as container for all face tracking and detection results.
Definition: FaceData.h:259
AndroidCameraCapture demonstrates use of raw camera image input to track from Android camera...
float xTexCoord
Texture coordinates for displaying frames from the tracker.
static const int STATE_WAIT_FOR_STABLE
Describes action&#39;s state when user is not keeping his head still.
static const int STATE_WAIT_FOR_FRONTAL
Describes action&#39;s state where face in the frame is not frontal (head rotations larger than 0...
static void DisplayLogo(const VsImage *logo, int width, int height)
Method draws a logo in the upper right corner.
Action actions[NUM_ACTIONS]
Array of actions that will need to be verified.
Image store container.