visageSDK
VisageFaceRecognitionWrapper.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 "VisageFeaturesDetector.h"
7 #include "Common.h"
8 #include "VisageFaceRecognition.h"
9 #include <android/log.h>
10 #include <android/asset_manager.h>
11 #include <android/asset_manager_jni.h>
12 #include "LicenseString.h"
13 #include "VisageRendering.h"
14 #include "Utils.h"
15 
16 #define LOG_TAG "DetectorWrapper"
17 #define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
18 #define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
19 #define MAX_SIZE 768
20 
21 #define PATH_TO_GALLERY "/Famous Actors/gallery"
22 #define MAX_FACES 3
23 #define TOP_N_FACES 3
24 #define SIMILARITY_THRESHOLD 0.65f
25 
26 using namespace VisageSDK;
27 
40 extern "C" {
41 
42 
43 std::string pathToData;
44 
45 VsImage* inputImage = 0;
46 
47 int facesRec = -1;
48 
49 FaceData *dataRec = 0;
50 
51 float sim[TOP_N_FACES];
52 
53 char result[1024];
54 
55 VisageFeaturesDetector *m_visageFeaturesDetector = 0;
56 VisageFaceRecognition *m_visageFaceRecognition = 0;
57 
58 extern VsImage *logo;
59 extern VsImage *font;
60 
61 
62 void Java_com_visagetechnologies_facedetectdemo_ImageDetectorView_AddToGallery(JNIEnv *env, jobject obj, jbyteArray image, jstring filename, jint width, jint height)
63 {
64  FaceData recData;
65  const char *imgName = env->GetStringUTFChars(filename, 0);
66 
67  jbyte *f = env->GetByteArrayElements(image, 0);
68  VsImage *frame = vsCreateImage(vsSize(width, height), VS_DEPTH_8U, 3);
69  memcpy(frame->imageData, (unsigned char*) f, frame->imageSize);
70 
71  int numFaces = m_visageFeaturesDetector->detectFacialFeatures(frame, &recData, 1);
72 
73  char name[256] = "";
74  const char *pos = strchr(imgName, '.');
75  int offset = (int)(pos - imgName);
76  strncat(name, imgName, offset);
77  if(numFaces > 0)
78  {
79  int ret = m_visageFaceRecognition->addDescriptor(frame, &recData, (const char*)name);
80  }
81 
82  vsReleaseImage(&frame);
83 }
84 
85 
86 static bool endsWith(const std::string& str, const std::string& suffix)
87 {
88  return str.size() >= suffix.size() && 0 == str.compare(str.size()-suffix.size(), suffix.size(), suffix);
89 }
90 
97 {
98  const char *_appPath = env->GetStringUTFChars(appPath, 0);
99  className = "com/visagetechnologies/facedetectdemo/RecognitionActivity";
100  _env = env;
101  _obj = obj;
102  std::string pathToLicense(_appPath + std::string("/") + licenseKey);
103  if (endsWith(licenseKey, ".vlc"))
104  initializeLicenseManager(env, obj, pathToLicense.c_str(), AlertCallback);
105  else
106  initializeLicenseManager(env, obj, licenseKey.c_str(), AlertCallback);
107 
108  std::string path(_appPath);
109  pathToData = path;
110  std::string pathToCfgFile(_appPath + std::string("/") + "Face Detector.cfg");
111  m_visageFeaturesDetector = new VisageFeaturesDetector(pathToCfgFile.c_str());
112  bool ret = m_visageFeaturesDetector->Initialize();
113 
114  path.append("/vfr/fr.tflite");
115 
116  m_visageFaceRecognition = new VisageFaceRecognition(path.c_str());
117 
118  if (!(m_visageFaceRecognition->is_initialized))
119  {
120  LOGI("Could not load required face recognition data files!");
121  return;
122  }
123 }
124 
130 void Java_com_visagetechnologies_facedetectdemo_ImageDetectorView_recognizeFace(JNIEnv *env, jobject obj, jbyteArray frame, jint width, jint height)
131 {
132  if(m_visageFeaturesDetector == 0)
133  return;
134 
135  jbyte *f = env->GetByteArrayElements(frame, 0);
136  inputImage = vsCreateImage(vsSize(width, height), VS_DEPTH_8U, 3);
137  memcpy(inputImage->imageData, (unsigned char*) f, inputImage->imageSize);
138  dataRec = new FaceData[MAX_FACES];
139  facesRec = m_visageFeaturesDetector->detectFacialFeatures((VsImage*)inputImage, dataRec, MAX_FACES);
140  if (facesRec > 0)
141  {
142  for (int i = 0; i < facesRec; i++)
143  {
144  if (m_visageFaceRecognition->is_initialized)
145  {
146  const int DESCRIPTOR_SIZE = m_visageFaceRecognition->getDescriptorSize();
147  short *descriptor = new short[DESCRIPTOR_SIZE];
148  const char* names[TOP_N_FACES];
149  int success = m_visageFaceRecognition->extractDescriptor(dataRec, (VsImage*)inputImage, descriptor);
150  if (!success)
151  {
152  LOGI("Descriptor was not successfully extracted");
153  return;
154  }
155  int rec = m_visageFaceRecognition->recognize(descriptor, TOP_N_FACES, names, sim);
156  if (!rec)
157  LOGI("Number of faces for comparison needs to be larger than 0");
158  sprintf(result, "%s %f", names[0], sim[0]);
159  }
160  }
161  }
162  else
163  LOGI("Face was not detected");
164 }
165 
166 
172 void Java_com_visagetechnologies_facedetectdemo_DetectorRenderer_displayIdentity(JNIEnv* env, jobject obj, jint width, jint height)
173 {
174  VisageRendering::Reset();
175  int glWidth = width;
176  int glHeight = height;
177 
178  float videoAspect = inputImage->width / (float) inputImage->height;
179 
180  float tmp;
181  if(inputImage->width < inputImage->height)
182  {
183  tmp = glHeight;
184  glHeight = glWidth / videoAspect;
185  if (glHeight > tmp)
186  {
187  glWidth = glWidth*tmp/glHeight;
188  glHeight = tmp;
189  }
190  }
191  else
192  {
193  tmp = glWidth;
194  glWidth = glHeight * videoAspect;
195  if (glWidth > tmp)
196  {
197  glHeight = glHeight*tmp/glWidth;
198  glWidth = tmp;
199  }
200  }
201 
202  VisageRendering::DisplayResults(&dataRec[0], 1, glWidth, glHeight, (VsImage*)inputImage, DISPLAY_FRAME);
203 
204  if (font)
206  else
207  LOGE("Could not load font");
208  if (logo)
209  VisageRendering::DisplayLogo(logo, glWidth, glHeight);
210  else
211  LOGE("Could not load logo");
212 
213  if (facesRec > 0)
214  {
215  for (int i = 0; i < facesRec; i++)
216  {
217  if (sim[0] > SIMILARITY_THRESHOLD)
218  VisageRendering::DisplayText(result, 1, 5.0f);
219  else
220  VisageRendering::DisplayText("?", 1, 7.0f);
221  }
222  }
223 }
224 
229 {
230  vsReleaseImage(&inputImage);
231  vsReleaseImage(&logo);
232  vsReleaseImage(&font);
233  delete[] dataRec;
234  delete m_visageFaceRecognition;
235  m_visageFaceRecognition = 0;
236  delete m_visageFeaturesDetector;
237  m_visageFeaturesDetector = 0;
238  facesRec = -1;
239  dataRec = 0;
240 
241 
242 }
243 
244 }
245 
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_recognizeFace(JNIEnv *env, jobject obj, jbyteArray frame, jint width, jint height)
Method which performs face recognition on a given image.
int glHeight
Size of the OpenGL view.
Faces and facial features detector implementation.
float videoAspect
Aspect of the video.
void Java_com_visagetechnologies_facedetectdemo_RecognitionActivity_CleanUp(JNIEnv *env, jobject obj)
Method for cleaning up resources used by application.
VisageFaceRecognition class contains a face recognition algorithm capable of measuring similarity bet...
char * imageData
Pointer to aligned image data.
void Java_com_visagetechnologies_facedetectdemo_RecognitionActivity_InitRecognition(JNIEnv *env, jobject obj, jstring appPath)
Method for initializing the recognizer.
int addDescriptor(VsImage *image, FaceData *facedata, const char *name)
Extracts a face descriptor from the input RGB, RGBA or grayscale image and adds it to the gallery...
void AlertCallback(const char *warningMessage)
Callback method for license notification.
int width
Image width in pixels.
int extractDescriptor(FaceData *facedata, VsImage *image, short *descriptor)
Extracts the face descriptor for face recognition from a facial image.
bool is_initialized
If the model used in the VisageFaceRecognition is loaded and initialized successfully value of the is...
static void DisplayText(const char *displayText, float effectValue, float scale=1.0f)
Method used to display text with animation (fade in or fade out effect)
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.
int recognize(short *descriptor, int n, const char **names, float *similarities)
Compare a face to all faces in the current gallery and return n names of the most similar faces...
Face data structure, used as container for all face tracking and detection results.
Definition: FaceData.h:259
int getDescriptorSize()
Gets the descriptor&#39;s size.
static void DisplayLogo(const VsImage *logo, int width, int height)
Method draws a logo in the upper right corner.
Image store container.
void Java_com_visagetechnologies_facedetectdemo_DetectorRenderer_displayIdentity(JNIEnv *env, jobject obj, jint width, jint height)
Method for displaying recognition results.