iOS 5 SDKの顔検出

iOS SDKは長い間利用可能ですが、クライアントはこのOSの古いバージョンとのプログラムの互換性に関心があるため、すべてのiOS開発者は、アプリケーションで新しいAPIを使用するのがまだ早いことを知っています。



しかし、すべてが新しいSDKでいくつかの利点が見つかりました。 最初に注目したのは、数か月前に必要だったUIViewController viewWillUnloadのメソッドでした。

iOS 5の技術革新の完全なリストは、 こちらをご覧ください 。

アドオンフレームワークのリストでは、CoreImageおよび特にCIDetector.hが対象です。



CIDetectorクラスは 、画像内の顔を見つけて識別するのに役立つように作成されました。これを簡単に説明します。





iOS 5 SDKでXcode 4.2を使用します。



プロジェクトを作成する


「自動参照カウントを使用する」を無効にすることを強くお勧めします。



プロジェクトフレームワークCoreImageに接続します


画像



UIViewControllerを作成する


#import <UIKit / UIKit.h>



@interface RootViewController : UIViewController <UIImagePickerControllerDelegate、UINavigationControllerDelegate>

{

IBOutlet UIImageView * imageView;

IBOutlet UILabel *ラベル;

CIDetector *検出器。

}



@end




UIImagePickerControllerを使用して画像をアップロードします


- ( IBAction ) onImport : ( id )送信者

{

UIImagePickerController * vc = [ [ UIImagePickerController alloc ] init ] ;

vc.delegate = self;

vc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

[ self presentModalViewController : vc animated : YES ] ;

[ vcリリース] ;

}




デバイスをiOS 5にアップグレードしませんでした。すべてのアクションは、カメラではなくギャラリーからのインポートを使用してシミュレーターで実行されるためです。



画像内の顔を定義する


- ( IBAction ) onRecognize : ( id )送信者

{

detector = [ CIDetector detectorOfType : CIDetectorTypeFace context : nil options : [ NSDictionary dictionaryWithObject : CIDetectorAccuracyHigh forKey : CIDetectorAccuracy ] ] ;



NSDate * date = [ NSDate date ] ;



NSArray * features = [ディテクターfeaturesInImage :

[ [ [ CIImage alloc ] initWithCGImage : imageView.image.CGImage ] autorelease ]

] ;



NSTimeInterval ti = fabs ( [ date timeIntervalSinceNow ] ) ;



label.text = [ NSString stringWithFormat : @ "Time:%0.3f \ n Faces:%i" 、ti、 [機能カウント] ] ;



UIGraphicsBeginImageContext ( imageView.image.size ) ;



CGContextRef ctx = UIGraphicsGetCurrentContext ( ) ;



CGContextDrawImage ( ctx、CGRectMake ( 0、0 、imageView.image.size.width、imageView.image.size.height ) 、imageView.image.CGImage ) ;





for ( CIFeature *機能の機能)

{

CGRect r = feature.bounds;



CGContextSetStrokeColor ( ctx、CGColorGetComponents ( [ UIColor yellowColor ] .CGColor ) ) ;

CGContextSetLineWidth ( ctx、1.0f ) ;



CGContextBeginPath ( ctx ) ;

CGContextAddRect ( ctx、r ) ;

CGContextClosePath ( ctx ) ;

CGContextStrokePath ( ctx ) ;



}

imageView.image = [ UIImage imageWithCGImage : UIGraphicsGetImageFromCurrentImageContext ( ). CGImage scale : 1.0f orientation : UIImageOrientationDownMirrored ] ;

UIGraphicsEndImageContext ( ) ;



}





結果


ドキュメントでは、 CIFeatureクラスのインスタンスはフェイスフレームとそのタイプに関する情報のみを提供しますが、AppleがOpenCVを完全にリダイレクトしたら、クラスの更新(IMHO)を待つことができます。











編集:onImportメソッドの顔を修正:



All Articles