Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Hack face detection

  • Loading branch information...
commit 9cabf741bf14d228557746671a3bea341288977a 1 parent 11ac41f
Martin Kvlr authored July 26, 2012
5  framework/Source/GPUImageVideoCamera.h
@@ -6,7 +6,9 @@
6 6
 
7 7
 /**
8 8
  A GPUImageOutput that provides frames from either camera
9  
-*/
  9
+ */
  10
+typedef void (^ArrayBlock)(NSArray*);
  11
+
10 12
 @interface GPUImageVideoCamera : GPUImageOutput <AVCaptureVideoDataOutputSampleBufferDelegate, AVCaptureAudioDataOutputSampleBufferDelegate>
11 13
 {
12 14
     CVOpenGLESTextureCacheRef coreVideoTextureCache;    
@@ -25,6 +27,7 @@
25 27
 
26 28
 /// The AVCaptureSession used to capture from the camera
27 29
 @property(readonly, retain) AVCaptureSession *captureSession;
  30
+@property(nonatomic, copy) ArrayBlock featuresDetected;
28 31
 
29 32
 /// This enables the benchmarking mode, which logs out instantaneous and average frame times to the console
30 33
 @property(readwrite, nonatomic) BOOL runBenchmark;
22  framework/Source/GPUImageVideoCamera.m
@@ -13,6 +13,7 @@ @interface GPUImageVideoCamera ()
13 13
     NSDate *startingCaptureTime;
14 14
     
15 15
     dispatch_queue_t audioProcessingQueue;
  16
+  CIDetector *_faceDetector;
16 17
 }
17 18
 
18 19
 @end
@@ -44,7 +45,8 @@ - (id)initWithSessionPreset:(NSString *)sessionPreset cameraPosition:(AVCaptureD
44 45
 		return nil;
45 46
     }
46 47
     
47  
-	audioProcessingQueue = dispatch_queue_create("com.sunsetlakesoftware.GPUImage.processingQueue", NULL);
  48
+  _faceDetector = [CIDetector detectorOfType:CIDetectorTypeFace context:nil options:@{CIDetectorAccuracy: CIDetectorAccuracyLow}];
  49
+	audioProcessingQueue = dispatch_queue_create("com.sunsetlakesoftware.GPUImage.processingQueue", DISPATCH_QUEUE_SERIAL);
48 50
     
49 51
     _runBenchmark = NO;
50 52
     capturePaused = NO;
@@ -257,6 +259,7 @@ - (void)processVideoSampleBuffer:(CMSampleBufferRef)sampleBuffer;
257 259
     {
258 260
         return;
259 261
     }
  262
+  
260 263
     
261 264
     CFAbsoluteTime startTime = CFAbsoluteTimeGetCurrent();
262 265
     CVImageBufferRef cameraFrame = CMSampleBufferGetImageBuffer(sampleBuffer);
@@ -265,6 +268,15 @@ - (void)processVideoSampleBuffer:(CMSampleBufferRef)sampleBuffer;
265 268
     
266 269
 	CMTime currentTime = CMSampleBufferGetPresentationTimeStamp(sampleBuffer);
267 270
     
  271
+  
  272
+  
  273
+  CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
  274
+  CIImage *image = [CIImage imageWithCVPixelBuffer:imageBuffer];
  275
+  NSArray *features = [_faceDetector featuresInImage:image options:@{CIDetectorImageOrientation: @(4)}]; //should be 7
  276
+  if(_featuresDetected) {
  277
+    _featuresDetected(features);
  278
+  }
  279
+  
268 280
     if ([GPUImageOpenGLESContext supportsFastTextureUpload])
269 281
     {
270 282
         CVPixelBufferLockBaseAddress(cameraFrame, 0);
@@ -367,12 +379,13 @@ - (void)processVideoSampleBuffer:(CMSampleBufferRef)sampleBuffer;
367 379
                 totalFrameTimeDuringCapture += currentFrameTime;
368 380
             }
369 381
         }
370  
-    }  
  382
+    }
  383
+
371 384
 }
372 385
 
373 386
 - (void)processAudioSampleBuffer:(CMSampleBufferRef)sampleBuffer;
374 387
 {
375  
-    [self.audioEncodingTarget processAudioBuffer:sampleBuffer]; 
  388
+    [self.audioEncodingTarget processAudioBuffer:sampleBuffer];
376 389
 }
377 390
 
378 391
 #pragma mark -
@@ -481,7 +494,7 @@ - (void)setOutputImageOrientation:(UIInterfaceOrientation)newValue;
481 494
             case UIInterfaceOrientationPortrait:outputRotation = kGPUImageRotateRight; break;
482 495
             case UIInterfaceOrientationPortraitUpsideDown:outputRotation = kGPUImageRotateLeft; break;
483 496
             case UIInterfaceOrientationLandscapeLeft:outputRotation = kGPUImageRotate180; break;
484  
-            case UIInterfaceOrientationLandscapeRight:outputRotation = kGPUImageNoRotation; break;
  497
+            case UIInterfaceOrientationLandscapeRight:outputRotation = kGPUImageRotate180; break;
485 498
         }
486 499
     }
487 500
     
@@ -490,6 +503,7 @@ - (void)setOutputImageOrientation:(UIInterfaceOrientation)newValue;
490 503
         NSInteger indexOfObject = [targets indexOfObject:currentTarget];
491 504
         [currentTarget setInputRotation:outputRotation atIndex:[[targetTextureIndices objectAtIndex:indexOfObject] integerValue]];
492 505
     }
  506
+  
493 507
 }
494 508
 
495 509
 @end

0 notes on commit 9cabf74

Please sign in to comment.
Something went wrong with that request. Please try again.