visageSDK
VisageFeaturesDetectorWrapper.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 "VisageRendering.h"
7 #include "VisageFeaturesDetector.h"
8 #include "Common.h"
9 #include "VisageFaceRecognition.h"
10 #include "LicenseString.h"
11 #include "Utils.h"
12 
13 #include <android/log.h>
14 #define LOG_TAG "DetectorWrapper"
15 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
16 #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
17 #define MAX_SIZE 768
18 
19 using namespace VisageSDK;
20 
33 extern "C" {
34 
35 VisageFeaturesDetector *visageFeaturesDetector = 0;
36 
37 extern VsImage *logo;
38 extern VsImage *font;
39 
40 VsImage* input = 0;
41 FDP* fdp = 0;
42 
43 int maxFaces = 100;
44 
45 int faces = -1;
46 FaceData* data;
47 
48 
49 static bool endsWith(const std::string& str, const std::string& suffix)
50 {
51  return str.size() >= suffix.size() && 0 == str.compare(str.size()-suffix.size(), suffix.size(), suffix);
52 }
53 
62 void Java_com_visagetechnologies_facedetectdemo_DetectorActivity_DetectorInit(JNIEnv *env, jobject obj, jstring appPath, jstring modelName)
63 {
64 
65  //initialize licensing
66  _env = env;
67  _obj = obj;
68  className = "com/visagetechnologies/facedetectdemo/DetectorActivity";
69  const char *_appPath;
70  _appPath = env->GetStringUTFChars(appPath, 0);
71 
72  //example how to initialize license key
73  std::string pathToLicense(_appPath + std::string("/") + licenseKey);
74  if (endsWith(licenseKey, ".vlc"))
75  initializeLicenseManager(env, obj, pathToLicense.c_str(), AlertCallback);
76  else
77  initializeLicenseManager(env, obj, licenseKey.c_str(), AlertCallback);
78 
79  if(visageFeaturesDetector == 0)
80  {
81  const char *_modelName = env->GetStringUTFChars(modelName, 0);
82  std::string pathToCfgFile(_appPath + std::string("/") + "Face Detector.cfg");
83  visageFeaturesDetector = new VisageFeaturesDetector(pathToCfgFile.c_str());
84  bool ret = visageFeaturesDetector->Initialize();
85  env->ReleaseStringUTFChars(modelName, _modelName);
86  env->ReleaseStringUTFChars(appPath, _appPath);
87 
88  data = 0;
89  }
90 }
91 
98 void Java_com_visagetechnologies_facedetectdemo_ImageDetectorView_DetectFeatures(JNIEnv *env, jobject obj, jbyteArray frame, jint width, jint height)
99 {
100  if(visageFeaturesDetector == 0)
101  {
102  return;
103  }
104 
105  jbyte *f = env->GetByteArrayElements(frame, 0);
106  input = vsCreateImage(vsSize(width, height), VS_DEPTH_8U, 3);
107  memcpy(input->imageData, (unsigned char*) f, input->imageSize);
108 
109  if(data != 0)
110  {
111  delete[] data;
112  }
113 
114  data = new FaceData[maxFaces];
115  faces = visageFeaturesDetector->detectFacialFeatures((VsImage*)input, data, maxFaces);
116 
117  env->ReleaseByteArrayElements(frame, f, 0);
118 }
119 
125 void Java_com_visagetechnologies_facedetectdemo_DetectorRenderer_displayResults(JNIEnv* env, jobject obj, jint width, jint height)
126 {
127  VisageRendering::Reset();
128  int glWidth = width;
129  int glHeight = height;
130 
131  float videoAspect = input->width / (float) input->height;
132 
133  float tmp;
134  if(input->width < input->height)
135  {
136  tmp = glHeight;
137  glHeight = glWidth / videoAspect;
138  if (glHeight > tmp)
139  {
140  glWidth = glWidth*tmp/glHeight;
141  glHeight = tmp;
142  }
143  }
144  else
145  {
146  tmp = glWidth;
147  glWidth = glHeight * videoAspect;
148  if (glWidth > tmp)
149  {
150  glHeight = glHeight*tmp/glWidth;
151  glWidth = tmp;
152  }
153  }
154  if (font)
156  else
157  LOGE("Could not load font");
158 
159  VisageRendering::DisplayResults(&data[0], 1, glWidth, glHeight, (VsImage*)input, DISPLAY_FRAME);
160  if (logo)
161  VisageRendering::DisplayLogo(logo, glWidth, glHeight);
162  else
163  LOGE("Could not load logo");
164 
165  for (int i = 0; i<faces; i++)
166  {
167  VisageRendering::DisplayResults(&data[i], 1, glWidth, glHeight, (VsImage*)input, DISPLAY_FEATURE_POINTS + DISPLAY_SPLINES + DISPLAY_GAZE + DISPLAY_AXES);
168  }
169 
170 }
171 
176 {
177  vsReleaseImage(&input);
178  vsReleaseImage(&logo);
179  vsReleaseImage(&font);
180  delete[] data;
181  data = 0;
182  faces = -1;
183 }
184 
185 }
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.
void Java_com_visagetechnologies_facedetectdemo_ImageDetectorView_DetectFeatures(JNIEnv *env, jobject obj, jbyteArray frame, jint width, jint height)
Method for passing raw image data to detector and initiating detection of features in it...
int glHeight
Size of the OpenGL view.
Faces and facial features detector implementation.
float videoAspect
Aspect of the video.
char * imageData
Pointer to aligned image data.
void AlertCallback(const char *warningMessage)
Callback method for license notification.
int width
Image width in pixels.
void Java_com_visagetechnologies_facedetectdemo_DetectorActivity_DetectorInit(JNIEnv *env, jobject obj, jstring appPath, jstring modelName)
Method for initializing the detector.
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) ...
int detectFacialFeatures(VsImage *frame, FaceData *output, int maxFaces=1, float minFaceScale=0.1f, float maxFaceScale=1.0f, bool outputOnly2DFeatures=false)
Performs faces and facial features detection in a still image.
bool Initialize()
Initializes the features detector.
Namespace.
Definition: android_header.h:1
int glWidth
Size of the OpenGL view.
Face data structure, used as container for all face tracking and detection results.
Definition: FaceData.h:259
void Java_com_visagetechnologies_facedetectdemo_DetectorActivity_CleanUp(JNIEnv *env, jobject obj)
Method for cleaning up resources used by application.
void Java_com_visagetechnologies_facedetectdemo_DetectorRenderer_displayResults(JNIEnv *env, jobject obj, jint width, jint height)
Method for displaying detection results.
Feature points of a face.
Definition: FDP.h:191
static void DisplayLogo(const VsImage *logo, int width, int height)
Method draws a logo in the upper right corner.
Image store container.