引言:作為一個開發小白,也注冊簡書也有一段時間了,但是一直沒太瀏覽過,更別說給大家分享一些經驗了,只能沒事借鑒別人的經驗。突然想到識別圖片中的二維碼的功能,所以查了一些資料,發現CIDetector這個類可以實現,在這里簡單和大家分享一下。
CIDetector 概述
A CIDetector object uses image processing to search for and identify notable features (faces, rectangles, and barcodes) in a still image or video. Detected features are represented by CIFeature objects that provide more information about each feature.
This class can maintain many state variables that can impact performance. So for best performance, reuse CIDetector instances instead of creating new ones.
CIDetector這個類用于識別、檢測靜止圖片或者視頻中的顯著特征(面部,矩形和條形碼),識別的具體特征由CIFeature類去處理。(由于英文水平有限,所有官方文檔只做概述,英語好的朋友可以細細研究)
CIFeature 介紹
A CIFeature object represents a portion of an image that a detector believes matches its criteria. Subclasses of CIFeature typically hold additional information specific to the detector that discovered the feature.
CIFeature類只保存基本信息, 所有的附加信息由子類保存。
- CIFaceFeature (人臉識別)
以下是基本屬性,相信大家也可以看明白(識別結果中使用的是坐標系和我們常用的有所區別,這個會在后面說明)
@property (readonly, assign) BOOL hasLeftEyePosition;
@property (readonly, assign) CGRect bounds;
@property (readonly, assign) CGPoint leftEyePosition;
@property (readonly, assign) BOOL hasRightEyePosition;
@property (readonly, assign) CGPoint rightEyePosition;
@property (readonly, assign) BOOL hasMouthPosition;
@property (readonly, assign) CGPoint mouthPosition;
@property (readonly, assign) BOOL hasTrackingID;
@property (readonly, assign) int trackingID;
@property (readonly, assign) BOOL hasSmile;
@property (readonly, assign) BOOL leftEyeClosed;
@property (readonly, assign) BOOL rightEyeClosed;
這四個屬性和視頻檢測相關,我目前也不知道作何使用。
@property (readonly, assign) BOOL hasTrackingID;
@property (readonly, assign) int trackingID;
@property (readonly, assign) BOOL hasTrackingFrameCount;
@property (readonly, assign) int trackingFrameCount;
- CIQRCodeFeature (二維碼識別)
除基本的信息位置之外,只有一個重要信息,messageString。
@property (nullable, readonly) NSString* messageString;
- CIRectangleFeature (矩形識別)
只有基本的位置信息。
- CITextFeature (文本識別)
除基本的信息位置之外,只有一個額外信息,subFeatures。數組中是CITextFeature對象(見后門解釋)
@property (nullable, readonly) NSArray *subFeatures;
開始擼代碼 (以人臉識別為例)
初始化CIDetector
```
CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeFace context:nil options:@{CIDetectorAccuracy : CIDetectorAccuracyHigh}];
```
context:用于檢測的圖形上下文,可不傳
options: CIDetectorAccuracy
A key used to specify the desired accuracy for the detector.
指定檢測精度 (取值CIDetectorAccuracyHigh :CIDetectorAccuracyLow)
CIDetectorTracking
A key used to enable or disable face tracking for the detector. Use this option when you want to track faces across frames in a video.
是否開啟面部追蹤(視頻中使用)
CIDetectorMinFeatureSize
A key used to specify the minimum size that the detector will recognize as a feature.
The value for this key is an NSNumber object ranging from 0.0 through 1.0 that represents a fraction of the minor dimension of the image.
指定識別要素最大或最小(值0~1,表示次要維度,我也不懂這是什么鬼)
CIDetectorNumberOfAngles
The number of perspectives to use for detecting a face in video input.
The value for this key is an NSNumber object containing the number 1, 3, 5, 7, 9, or 11. At higher numbers of angles, face detection in video becomes more accurate, but at a higher computational cost.
臉部透視數(值為包含1、3、5、7、9、11的NSNumber對象)
開始檢測
```
NSArray *features = [detector featuresInImage:(CIImage *)image options:(NSDictionary*)options];
```
image: 一個CIImage對象
options: CIDetectorImageOrientation
An option for the display orientation of the image whose features you want to detect.
The value of this key is an NSNumber object whose value is an integer between 1 and 8. The TIFF and EXIF specifications define these values to indicate where the pixel coordinate origin (0,0) of the image should appear when it is displayed. The default value is 1, indicating that the origin is in the top left corner of the image. For further details, see kCGImagePropertyOrientation.
Core Image detects only faces whose orientation matches that of the image. You should provide a value for this key if you want to detect faces in a different orientation.
用于要檢測其要素的圖像的顯示方向的選項。默認為1,具體參考kCGImagePropertyOrientation
CIDetectorEyeBlink
An option for whether Core Image will perform additional processing to recognize closed eyes in detected faces.
是否執行額外選項處理以識別面部眼部閉合
CIDetectorSmile
An option for whether Core Image will perform additional processing to recognize smiles in detected faces.
是否執行額外選項處理以識別面部微笑
CIDetectorFocalLength
An option identifying the focal length used in capturing images to be processed by the detector.
The value of this key is an NSNumber object whose value is a floating-point number between -1.0 and 1.0. Use this option with the CIDetectorTypeRectangle detector type to fine-tune the accuracy of the detector.
檢測中使用的焦距選項值為-1~1,配合CIDetectorTypeRectangle使用
CIDetectorAspectRatio
An option specifying the aspect ratio (width divided by height) of rectangles to search for.
The value of this key is an NSNumber object whose value is a positive floating-point number. Use this option with the CIDetectorTypeRectangle detector type to fine-tune the accuracy of the detector. For example, to more accurately find a business card (3.5 x 2 inches) in an image, specify an aspect ratio of 1.75 (3.5 / 2).
矩形檢查的寬高比選項,值為正浮點型,配合CIDetectorTypeRectangle使用
CIDetectorReturnSubFeatures
An option specifying whether to return feature information for components of detected features..
The value of this key is an NSNumber object with a Boolean value. Use this option with the CIDetectorTypeText detector type to choose whether to detect only regions likely to contain text (NO, the default) or to also identify sub-regions likely to contain individual characters of text (YES).
是否返回檢測結果組件,與CIDetectorTypeText配合使用,默認為NO,如果設置為YES,則CITextFeature的subFeatures中是單個字符的信息
features:一個包含所有檢測結果的數組,數組中為CIFeature子類
坐標系轉換:
看代碼 ? :
```
CGAffineTransform transform = CGAffineTransformMakeScale(1, -1);
transform = CGAffineTransformTranslate(transform, 0, -image.size.height);
CGPoint point = CGPointApplyAffineTransform(point, transform);
```
這里需要一些基本的數學嘗試,通過蘋果提供的API進行旋轉偏移進行抓換,然后將CIDetector坐標系中的點轉為我們常用的坐標系。
提示:人臉檢測大家可以使用Face++,或者自己研究OpenCV,我現在正在利用空余時間研究利用OpenCV與OCR技術識別文字,若有進展,再來分享給大家。
感謝您的閱讀,如有不足之處還望您能指正。