Skip to content

Commit

Permalink
촬영화면에서 프로필 사진 제외하면 얼굴인식DB에서 삭제되는 기능 추가.
Browse files Browse the repository at this point in the history
(카메라 닫을 때까지 제외된 DB 유지하도록 수정하는 버전)
  • Loading branch information
jungwukim committed Feb 10, 2014
1 parent 3176399 commit 4fd3326
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 28 deletions.
169 changes: 141 additions & 28 deletions External/FaceRecognition/FaceDetectionViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ @interface FaceDetectionViewController ()
CIDetector *faceDetector;

NSMutableArray *selectedUsers;
NSMutableArray *unSelectedUSers;

cv::Mat old_prepreprocessedFace;
double old_time;
Expand Down Expand Up @@ -173,6 +174,7 @@ - (void)viewDidLoad
recognisedFaces = @{}.mutableCopy;
processing = @{}.mutableCopy;
selectedUsers = [NSMutableArray array];
unSelectedUSers = [NSMutableArray array];

guideImage = [UIImage imageNamed:@"hive_line"];

Expand Down Expand Up @@ -245,6 +247,8 @@ - (void)viewWillDisappear:(BOOL)animated
[super viewWillDisappear:animated];
isReadyToScanFace = NO;
[self teardownAVCapture];
[selectedUsers removeAllObjects];
[unSelectedUSers removeAllObjects];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MotionOrientationChangedNotification object:nil];
}

Expand Down Expand Up @@ -688,7 +692,8 @@ - (void) imageEnd:(id) sender withEvent:(UIEvent *) event
[button0 removeFromSuperview];

int userid = button0.UserID;
[selectedUsers removeObject:@(userid)];

[self exceptUSerFromTrainDB:userid];

NSLog(@"REMOVE || selectedUsers = %@", selectedUsers);

Expand Down Expand Up @@ -718,6 +723,38 @@ - (void) imageEnd:(id) sender withEvent:(UIEvent *) event
}
}

- (void)exceptUSerFromTrainDB:(int)userid
{
[selectedUsers removeObject:@(userid)];
[unSelectedUSers addObject:@(userid)];
//if(![unSelectedUSers containsObject:@(userid)])


NSArray *users = [SQLManager getAllUsers];
int userCount = (int)[users count];
int unSelectedUserCount = (int)[unSelectedUSers count];

[self clearGuide];

isFaceRecRedy = NO;

if(userCount > unSelectedUserCount) {
NSArray *trainModel = [SQLManager getTrainModels];
if(!IsEmpty(trainModel))
[FaceLib trainModel:trainModel withOut:unSelectedUSers];
}
else if(userCount <= unSelectedUserCount){
[unSelectedUSers removeAllObjects];
NSArray *trainModel = [SQLManager getTrainModels];
if(!IsEmpty(trainModel))
[FaceLib trainModel:trainModel];

}

isFaceRecRedy = YES;

}

#pragma mark - AV setup
- (void)setupAVCapture
{
Expand Down Expand Up @@ -1315,6 +1352,19 @@ - (void)drawFaceBoxesForFeatures:(NSArray *)features forVideoBox:(CGRect)clap or
name = nil;
}

if(!IsEmpty(name)){
if(!GlobalValue.testMode) {

if ([name rangeOfString:@"Unknown"].location != NSNotFound) {
name = @"Unknown";
}

else {
name = [self getPrefix:name divider:@":"];
}
}
}

[self showFaceRect:faceRect withName:name];

currentFeature++;
Expand Down Expand Up @@ -1582,6 +1632,28 @@ - (void)identifyFace:(CIFaceFeature *)feature inImage:(CIImage *)ciImage
}
}

- (NSString *)getPrefix:(NSString*)str divider:(NSString*)div
{
NSRange range = [str rangeOfString:div];
NSInteger start = 0;
NSInteger size = range.location ;
NSRange searchRange = NSMakeRange(start,size);
NSString *string = [str substringWithRange:searchRange];
return string;
}

- (NSString *)getSuffix:(NSString*)str divider:(NSString*)div
{
NSRange range = [str rangeOfString:div];
NSInteger start = range.location + 1;
NSInteger size = [str length] - start;
NSRange searchRange = NSMakeRange(start,size);
NSString *string = [str substringWithRange:searchRange];
return string;
}



- (void)parseFace:(cv::Mat &)image forId:(int)trackingID
{
NSDictionary *match = [FaceLib recognizeFace:image];
Expand Down Expand Up @@ -1612,63 +1684,104 @@ - (void)parseFace:(cv::Mat &)image forId:(int)trackingID
PBFaceRecognizer currentRecognizerType = (PBFaceRecognizer)[match[@"currentRecognizerType"] intValue];
double confidence = [match[@"confidence"] doubleValue];

NSString *dbUserName = [SQLManager getUserName:UserID];
//NSString *score;
NSString *name;

if(currentRecognizerType == LBPHFaceRecognizer)
{
NSString *name;

if(confidence < 50.f){ // For LBPH
if(GlobalValue.testMode)
name = [NSString stringWithFormat:@"%@:%.2f", [SQLManager getUserName:UserID], confidence];
else
name = [NSString stringWithFormat:@"%@ ", [SQLManager getUserName:UserID]];
// if(GlobalValue.testMode)
name = [NSString stringWithFormat:@"%@:%.2f", dbUserName, confidence];
// else
// name = [NSString stringWithFormat:@"%@ ", tmpUserName];
isFindFace = YES;
}
//else if(confidence > 50.f && confidence < 60.f){ // For LBPH
else if(confidence > 50.f && confidence < 80.f){ // For LBPH
if(GlobalValue.testMode)
name = [NSString stringWithFormat:@"? %@:%.2f", [SQLManager getUserName:UserID], confidence];
else
name = [NSString stringWithFormat:@"? %@ ", [SQLManager getUserName:UserID]];
// if(GlobalValue.testMode)
name = [NSString stringWithFormat:@"? %@:%.2f", dbUserName, confidence];
// else
// name = [NSString stringWithFormat:@"? %@ ", tmpUserName];
isFindFace = YES;
}
else {
if(GlobalValue.testMode)
// if(GlobalValue.testMode)
name = [NSString stringWithFormat:@"Unknown[%d:%.2f]", UserID, confidence];
else
name = [NSString stringWithFormat:@"Unknown"];
// else
// name = [NSString stringWithFormat:@"Unknown"];
isFindFace = NO;
}

recognisedFaces[@(trackingID)] = name;

}
else if(currentRecognizerType == EigenFaceRecognizer || currentRecognizerType == FisherFaceRecognizer)
{
NSString *name;
//NSString *name;
if(confidence >= 0.8f){ // For EigenFace
if(GlobalValue.testMode)
name = [NSString stringWithFormat:@"%@:%.2f", [SQLManager getUserName:UserID], confidence];
else
name = [NSString stringWithFormat:@"%@ ", [SQLManager getUserName:UserID]];
// if(GlobalValue.testMode)
name = [NSString stringWithFormat:@"%@:%.2f", dbUserName, confidence];
// else
// name = [NSString stringWithFormat:@"%@ ", tmpUserName];

isFindFace = YES;
}
else if(confidence > 0.7f && confidence < 0.8f){ // For EigenFace
if(GlobalValue.testMode)
name = [NSString stringWithFormat:@"? %@:%.2f", [SQLManager getUserName:UserID], confidence];
else
name = [NSString stringWithFormat:@"? %@ ", [SQLManager getUserName:UserID]];
// if(GlobalValue.testMode)
name = [NSString stringWithFormat:@"? %@:%.2f", dbUserName, confidence];
// else
// name = [NSString stringWithFormat:@"? %@ ", tmpUserName];

isFindFace = YES;
}
else {
if(GlobalValue.testMode)
// if(GlobalValue.testMode)
name = [NSString stringWithFormat:@"Unknown[%d:%.2f]", UserID, confidence];
else
name = [NSString stringWithFormat:@"Unknown"];
// else
// name = [NSString stringWithFormat:@"Unknown"];
isFindFace = NO;
}
recognisedFaces[@(trackingID)] = name;
//recognisedFaces[@(trackingID)] = name;
}

recognisedFaces[@(trackingID)] = name;
NSLog(@"BEFORE :: recognisedFaces = %@",recognisedFaces);

NSArray *allkeys = recognisedFaces.allKeys;
if(allkeys.count > 1){
for(NSNumber *key in allkeys)
{
//rint cTrackingID = [key intValue];
NSString *value = recognisedFaces[key];


//if(cTrackingID == trackingID) continue;


if(([value rangeOfString:dbUserName].location != NSNotFound) ||
([value rangeOfString:[NSString stringWithFormat:@"? %@",dbUserName]].location != NSNotFound))
//if([value hasPrefix:tmpUserName] || [value hasPrefix:[NSString stringWithFormat:@"? %@",tmpUserName]])
{
//NSString *vName = [self getPrefix:value divider:@":"];
double vConfidence = [[self getSuffix:value divider:@":"] doubleValue];
if(confidence > vConfidence){
recognisedFaces[@(trackingID)] = @"Unknown";
}
else {
recognisedFaces[key] = @"Unknown";
}

isFindFace = NO;
break;
}
}
}



NSLog(@"AFTER :: recognisedFaces = %@",recognisedFaces);

dispatch_async(dispatch_get_main_queue(), ^{
if(isFindFace)
[self addNewFaceIcon:UserID];
Expand Down
1 change: 1 addition & 0 deletions External/FaceRecognition/PBFaceLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ typedef enum{

#pragma mark trainModel Operations
- (BOOL)trainModel:(NSArray *)models;
- (BOOL)trainModel:(NSArray *)models withOut:(NSMutableArray*)exceptIDs;

#pragma mark Detect Operations
- (NSArray*)detectFace:(CIImage*)ciImage options:(NSDictionary *)options;
Expand Down
36 changes: 36 additions & 0 deletions External/FaceRecognition/PBFaceLib.mm
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,42 @@ - (BOOL)trainModel:(NSArray *)models
}
}

- (BOOL)trainModel:(NSArray *)models withOut:(NSMutableArray*)exceptIDs
{
if(IsEmpty(models) || IsEmpty(exceptIDs)) return NO;

std::vector<cv::Mat> images;
std::vector<int> labels;

for(NSDictionary *model in models){
int UserID = [model[@"UserID"] intValue];
if(![exceptIDs containsObject:@(UserID)]) {
//if(UserID != exceptID){
NSLog(@"UserID = %d model Added..", UserID);
NSData *imageData = model[@"imageData"];

// Then convert NSData to a cv::Mat. Images are standardized into 100x100
cv::Mat faceData = [self dataToMat:imageData
width:@100 //[NSNumber numberWithInt:100]
height:@100]; //[NSNumber numberWithInt:100]];
// Put this image into the model
images.push_back(faceData);
labels.push_back(UserID);
}


}

if (images.size() > 0 && labels.size() > 0) {
_model->train(images, labels);
return YES;
}
else {
return NO;
}

}

- (BOOL)updateModel:(NSArray *)models
{
std::vector<cv::Mat> images;
Expand Down
29 changes: 29 additions & 0 deletions Pixbee/Common/DB/PBSQLiteManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -837,6 +837,35 @@ - (NSArray*)getTrainModelsForID:(int)UserID
return (NSArray*)models;
}

- (NSArray*)getTrainModelsExceptID:(int)UserID
{
NSMutableArray *models = [NSMutableArray array];

const char* selectSQL = "SELECT UserID, image FROM FaceData where UserID IS NOT ?";
sqlite3_stmt *statement;

sqlite3 *sqlDB = [SQLManager getDBContext];

if (sqlite3_prepare_v2(sqlDB, selectSQL, -1, &statement, nil) == SQLITE_OK) {
sqlite3_bind_int(statement, 1, UserID);
while (sqlite3_step(statement) == SQLITE_ROW) {
int imageSize = sqlite3_column_bytes(statement, 1);
NSData *imageData = [NSData dataWithBytes:sqlite3_column_blob(statement, 1) length:imageSize];

cv::Mat data = [FaceLib dataToMat:imageData width:@(100) height:@(100)];
UIImage *image = [FaceLib MatToUIImage:data];
[models addObject:image];

}
}

sqlite3_finalize(statement);


return (NSArray*)models;
}


- (NSArray*)getUsersFaces
{
NSMutableArray *usersPhotos = [NSMutableArray array];
Expand Down
Binary file added Pixbee/Resources/Images/[email protected]
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4fd3326

Please sign in to comment.