visageSDK
VisageFaceRecognitionWrapper.mm
1 #import "VisageFaceRecognitionWrapper.h"
2 #import "Utilities.h"
3 #import "VisageDrawing.h"
4 #import "ConversionFunctions.h"
5 #import <UIKit/UIKit.h>
6 
7 #include "sys/timeb.h"
8 
9 #define MAX_FACES 100
10 
11 using namespace VisageSDK;
12 
13 @implementation VisageFaceRecognitionWrapper
14 
15 - (bool) initRecognition : (VisageFeaturesDetector*) detector
16 {
17  if (vfr)
18  return true;
19 
20  else
21  {
22  NSString* resourcePath = [[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/vfr/fr.bin"];
23 
24  vfr = new VisageFaceRecognition([resourcePath UTF8String]);
25  vfd = detector;
26 
27  int success = [self createGallery];
28 
29  if (!success)
30  {
31  return false;
32  }
33 
34  if (!(vfr->is_initialized))
35  {
36  return false;
37  }
38 
39  return true;
40  }
41 }
42 
44 {
45  if (!vfd)
46  return 0;
47 
48  NSString* path = [[[NSBundle mainBundle] resourcePath] stringByAppendingString:@"/Famous Actors/gallery"];
49  NSArray* dirFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
50  NSArray* jpgFiles = [dirFiles filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"self ENDSWITH '.jpg'"]];
51 
52  if ([jpgFiles count] == 0)
53  return 0;
54 
55  for (int i = 0; i < [jpgFiles count]; i++)
56  {
57  NSString* filePath = [NSString stringWithFormat:@"%@/%@", path, jpgFiles[i]];
58  UIImage* nsImage = [[UIImage alloc] initWithContentsOfFile: filePath];
59 
60  IplImage* iplImage = IplImageFromUIImage(nsImage, 3);
61 
62  FaceData data;
63 
64  int n_faces = vfd->detectFacialFeatures((VsImage*) iplImage, &data);
65 
66  NSString* name = [jpgFiles[i] stringByReplacingOccurrencesOfString:@".jpg" withString:@""];
67 
68  if(n_faces > 0)
69  vfr->addDescriptor((VsImage*) iplImage, &data, [name UTF8String]);
70 
71  cvReleaseImage(&iplImage);
72  }
73 
74  return 1;
75 }
76 
77 - (UIImage*) recognizeFace:(UIImage*) uiImage
78 {
79  // if detector not initialized return the input image
80  if (!vfd)
81  return uiImage;
82 
83  IplImage* iplImage = IplImageFromUIImage(uiImage, 3);
84 
85  FaceData* data = new FaceData[MAX_FACES];
86  int n_faces = vfd->detectFacialFeatures((VsImage*) iplImage, data, MAX_FACES
87  );
88 
89  if(n_faces > 0)
90  {
91  for (int i = 0; i < n_faces; i++)
92  {
93  const int DESCRIPTOR_SIZE = vfr->getDescriptorSize();
94  short* descriptor = new short[DESCRIPTOR_SIZE];
95  const int TOP_N_FACES = 3;
96  const char* names[TOP_N_FACES];
97  float sim[TOP_N_FACES];
98  const float SIMILARITY_THRESHOLD = 0.65f;
99 
100  vfr->extractDescriptor(&data[i], (VsImage*)iplImage, descriptor);
101  vfr->recognize(descriptor, TOP_N_FACES, names, sim);
102 
103  printf("name: %s, similarity: %f\n", names[0], sim[0]);
104 
105  delete[] descriptor;
106 
107  char result[1024];
108  sprintf(result, "%s %f", names[0], sim[0]);
109 
110  if (sim[0] > SIMILARITY_THRESHOLD)
111  VisageDrawing::drawMatchingIdentity(iplImage, data[i], result);
112  else
113  VisageDrawing::drawMatchingIdentity(iplImage, data[i], "?");
114  }
115  }
116 
117  // convert iplImage to 4-channels image in order to draw logo
118  IplImage* iplImageBGRA = cvCreateImage(cvSize(iplImage->width,iplImage->height),IPL_DEPTH_8U,4);
119  cvCvtColor(iplImage, iplImageBGRA, CV_BGR2BGRA);
120 
121  //draw logo
122  if (logo)
123  VisageDrawing::drawLogo((VsImage*)iplImageBGRA, (VsImage*)logo);
124 
125  // convert the output IplImage to UIImage
126  UIImage* returnImage = UIImageFromIplImage(iplImageBGRA);
127 
128  cvReleaseImage(&iplImage);
129  cvReleaseImage(&iplImageBGRA);
130  delete[] data;
131 
132  return returnImage;
133 }
134 
135 
136 @end
137 
int height
Image height in pixels.
Faces and facial features detector implementation.
int createGallery()
Method which adds descriptors to VisageFaceRecognition gallery.
VisageFaceRecognition class contains a face recognition algorithm capable of measuring similarity bet...
Implements simple high-level Objective-C interface around VisageSDK::VisageFaceRecognition functional...
int width
Image width in pixels.
Namespace.
Definition: ios_header.h:1
Face data structure, used as container for all face tracking and detection results.
Definition: FaceData.h:259
Image store container.