文 || 張賀
Core ML是iOS11新推出的機器學(xué)習(xí)使用框架浦旱。在此框架之上還有兩個集成度較高的框架:Vision 和 NLP(分別是圖像處理和文字處理領(lǐng)域的機器學(xué)習(xí)框架)。
圖片來自蘋果官方文檔
CoreML也可以看做一個模型的轉(zhuǎn)換器,可以將一個MLModel格式的模型文件自動生成一些類和方法,可以直接使用這些類去做分析,讓你更簡單是在app使用訓(xùn)練好的模型。蘋果提供了一些已經(jīng)訓(xùn)練好的模型供使用,選擇一個合適自己需求的下載 房官。 戳我去下載。
下載模型文件
點擊箭頭會進入MobileNet的頭文件
第一行Mchaine Learning Model 是對模型的一些介紹。
第二行Model Class是自動生成的類和方法疲酌,點擊箭頭可以查看頭文件蜡峰。我使用的模型是MobileNet生成的類有3個
MobileNetInput MobileNetOutput MobileNet
//
// MobileNet.h
//
// This file was automatically generated and should not be edited.
//
#import <Foundation/Foundation.h>
#import <CoreML/CoreML.h>
#include <stdint.h>
NS_ASSUME_NONNULL_BEGIN
/// Model Prediction Input Type
/// 輸入模型
API_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0))
@interface MobileNetInput : NSObject<MLFeatureProvider>
/// Input image to be classified as color (kCVPixelFormatType_32BGRA) image buffer, 224 pixels wide by 224 pixels high
/// 輸入的是一個CVPixelBufferRef類型的圖片 尺寸是224 * 224 尺寸不對會報錯
@property (readwrite, nonatomic) CVPixelBufferRef image;
- (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithImage:(CVPixelBufferRef)image;
@end
/// Model Prediction Output Type
/// 輸出模型
API_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0))
@interface MobileNetOutput : NSObject<MLFeatureProvider>
/// Probability of each category as dictionary of strings to doubles
/// 所有可能的結(jié)果以及每種結(jié)果的可能百分比
@property (readwrite, nonatomic) NSDictionary<NSString *, NSNumber *> * classLabelProbs;
/// Most likely image category as string value
/// 最有可能的結(jié)果 也就是上面百分比最大的那個
@property (readwrite, nonatomic) NSString * classLabel;
- (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithClassLabelProbs:(NSDictionary<NSString *, NSNumber *> *)classLabelProbs classLabel:(NSString *)classLabel;
@end
/// Class for model loading and prediction
/// 通過這個類使輸入模型 ---> 輸出模型
API_AVAILABLE(macos(10.13), ios(11.0), watchos(4.0), tvos(11.0))
@interface MobileNet : NSObject
@property (readonly, nonatomic, nullable) MLModel * model;
- (nullable instancetype)initWithContentsOfURL:(NSURL *)url error:(NSError * _Nullable * _Nullable)error;
/**
Make a prediction using the standard interface
@param input an instance of MobileNetInput to predict from
@param error If an error occurs, upon return contains an NSError object that describes the problem. If you are not interested in possible errors, pass in NULL.
@return the prediction as MobileNetOutput
*/
- (nullable MobileNetOutput *)predictionFromFeatures:(MobileNetInput *)input error:(NSError * _Nullable * _Nullable)error;
/**
Make a prediction using the convenience interface
@param image Input image to be classified as color (kCVPixelFormatType_32BGRA) image buffer, 224 pixels wide by 224 pixels high:
@param error If an error occurs, upon return contains an NSError object that describes the problem. If you are not interested in possible errors, pass in NULL.
@return the prediction as MobileNetOutput
*/
- (nullable MobileNetOutput *)predictionFromImage:(CVPixelBufferRef)image error:(NSError * _Nullable * _Nullable)error;
@end
NS_ASSUME_NONNULL_END
MobileNet.h
的所有代碼都在上面,可以看見使用起來非常簡單朗恳。
你可能需要用到的方法:
將UIImage
轉(zhuǎn)換成CVPixelBufferRef
/// 下面這個屬性是UIImage的湿颅,你只要UIImage的對象 `image.CGImage`傳進來就行了
/// @property(nullable, nonatomic,readonly) CGImageRef CGImage;
- (CVPixelBufferRef)GetpixelBufferWithCGImage:(CGImageRef)cgimage
{
NSDictionary *options = @{
(NSString*)kCVPixelBufferCGImageCompatibilityKey : @YES,
(NSString*)kCVPixelBufferCGBitmapContextCompatibilityKey : @YES,
(NSString*)kCVPixelBufferIOSurfacePropertiesKey: [NSDictionary dictionary]
};
CVPixelBufferRef pxbuffer = NULL;
CGFloat frameWidth = CGImageGetWidth(cgimage);
CGFloat frameHeight = CGImageGetHeight(cgimage);
CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault,
frameWidth,
frameHeight,
kCVPixelFormatType_32BGRA,
(__bridge CFDictionaryRef) options,
&pxbuffer);
NSParameterAssert(status == kCVReturnSuccess && pxbuffer != NULL);
CVPixelBufferLockBaseAddress(pxbuffer, 0);
void *pxdata = CVPixelBufferGetBaseAddress(pxbuffer);
NSParameterAssert(pxdata != NULL);
CGColorSpaceRef rgbColorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(pxdata,
frameWidth,
frameHeight,
8,
CVPixelBufferGetBytesPerRow(pxbuffer),
rgbColorSpace,
(CGBitmapInfo)kCGImageAlphaNoneSkipFirst);
NSParameterAssert(context);
CGContextConcatCTM(context, CGAffineTransformIdentity);
CGContextDrawImage(context, CGRectMake(0,
0,
frameWidth,
frameHeight),
cgimage);
CGColorSpaceRelease(rgbColorSpace);
CGContextRelease(context);
CVPixelBufferUnlockBaseAddress(pxbuffer, 0);
return pxbuffer;
}
你可能用到的方法,裁剪圖片粥诫。將圖片裁剪成224 * 224 的油航。
#pragma mark -裁剪圖片
//需要傳過來的參數(shù)有 : 圖片 image 和 自定的尺寸
- (UIImage *)image:(UIImage*)image byScalingToSize:(CGSize)targetSize {
//原始 iamge
UIImage *sourceImage = image;
//新的image 用來接收裁剪后的image 開始時為 nil
UIImage *newImage = nil;
UIGraphicsBeginImageContext(targetSize);
CGRect thumbnailRect = CGRectZero;
//裁剪后的image 的原點和 裁剪前 的 image 的 原點相同
thumbnailRect.origin = CGPointZero;
//裁剪后的image 的寬和 指定的寬 相同
thumbnailRect.size.width = targetSize.width;
//裁剪后的image 的長和 指定的長 相同
thumbnailRect.size.height = targetSize.height;
//將原始image 在設(shè)定的 位置上繪制(裁剪)
[sourceImage drawInRect:thumbnailRect];
//把裁剪好的 image 放在 新的image 上
newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage ;
}
調(diào)用系統(tǒng)相機或從相冊中選擇照片,選擇完成之后
#pragma mark - 圖片識別
/// 我里面的方法就是上面的2個方法做了一點封裝
- (void)RecognizeWithImage:(UIImage *)pImage
{
MobileNet *mobileNet = [[MobileNet alloc]init];
//只需要調(diào)用predictionFromImage:error:這一個方法就可以得到結(jié)果了
MobileNetOutput *output = [mobileNet predictionFromImage:[[pImage ScalingToSize:CGSizeMake(224, 224)] GetpixelBuffer] error:nil];
_photoName.text = output.classLabel;
//所有可能的結(jié)果 以及每種結(jié)果的可能性百分比
NSLog(@"classLabelProbs----------------%@",output.classLabelProbs);
//可能性最大的那個結(jié)果
NSLog(@"classLabel----------------%@",output.classLabel);
}
最后附一張識別截圖
最后附一張識別截圖
此文章待更....一些細節(jié)和理論部分有待深入學(xué)習(xí)和理解怀浆。