MPPImageClassifier
@interface MPPImageClassifier : NSObject@brief Performs classification on images.
The API expects a TFLite model with optional, but strongly recommended, TFLite Model Metadata..
The API supports models with one image input tensor and one or more output tensors. To be more specific, here are the requirements.
Input tensor (kTfLiteUInt8/kTfLiteFloat32)
- image input of size
[batch x height x width x channels]. - batch inference is not supported (
batchis required to be 1). - only RGB inputs are supported (
channelsis required to be 3). - if type is kTfLiteFloat32, NormalizationOptions are required to be attached to the metadata for input normalization.
At least one output tensor with: (kTfLiteUInt8/kTfLiteFloat32)
Nclasses and either 2 or 4 dimensions, i.e.[1 x N]or[1 x 1 x 1 x N]- optional (but recommended) label map(s) as AssociatedFiles with type TENSOR_AXIS_LABELS,
containing one label per line. The first such AssociatedFile (if any) is used to fill the
class_namefield of the results. Thedisplay_namefield is filled from the AssociatedFile (if any) whose locale matches thedisplay_names_localefield of theImageClassifierOptionsused at creation time (“en” by default, i.e. English). If none of these are available, only theindexfield of the results will be filled. - optional score calibration can be attached using ScoreCalibrationOptions and an AssociatedFile with type TENSOR_AXIS_SCORE_CALIBRATION. See metadata_schema.fbs [1] for more details.
-
Creates a new instance of
ImageClassifierfrom an absolute path to a TensorFlow Lite model file stored locally on the device and the defaultImageClassifierOptions.Declaration
Objective-C
- (nullable instancetype)initWithModelPath:(nonnull NSString *)modelPath error:(NSError *_Nullable *_Nullable)error;Parameters
modelPathAn absolute path to a TensorFlow Lite model file stored locally on the device.
Return Value
A new instance of
ImageClassifierwith the given model path.nilif there is an error in initializing the image classifier. -
Creates a new instance of
ImageClassifierfrom the givenImageClassifierOptions.Declaration
Objective-C
- (nullable instancetype)initWithOptions: (nonnull MPPImageClassifierOptions *)options error:(NSError *_Nullable *_Nullable)error;Parameters
optionsThe options of type
ImageClassifierOptionsto use for configuring theImageClassifier.Return Value
A new instance of
ImageClassifierwith the given options.nilif there is an error in initializing the image classifier. -
Performs image classification on the provided
MPImageusing the whole image as region of interest. Rotation will be applied according to theorientationproperty of the providedMPImage. Only use this method when theImageClassifieris created with running mode,.image.This method supports classification of RGBA images. If your
MPImagehas a source type of.pixelBufferor.sampleBuffer, the underlying pixel buffer must usekCVPixelFormatType_32BGRAas its pixel format.If your
MPImagehas a source type of.imageensure that the color space is RGB with an Alpha channel.Declaration
Objective-C
- (nullable MPPImageClassifierResult *) classifyImage:(nonnull MPPImage *)image error:(NSError *_Nullable *_Nullable)error;Parameters
imageThe
MPImageon which image classification is to be performed.Return Value
An
ImageClassifierResultobject that contains a list of image classifications. -
Performs image classification on the provided
MPImagecropped to the specified region of interest. Rotation will be applied on the cropped image according to theorientationproperty of the providedMPImage. Only use this method when theImageClassifieris created with running mode,.image.This method supports classification of RGBA images. If your
MPImagehas a source type of.pixelBufferor.sampleBuffer, the underlying pixel buffer must usekCVPixelFormatType_32BGRAas its pixel format.If your
MPImagehas a source type of.imageensure that the color space is RGB with an Alpha channel.Declaration
Objective-C
- (nullable MPPImageClassifierResult *) classifyImage:(nonnull MPPImage *)image regionOfInterest:(CGRect)roi error:(NSError *_Nullable *_Nullable)error;Parameters
imageThe
MPImageon which image classification is to be performed.roiA
CGRectspecifying the region of interest within the givenMPImage, on which image classification should be performed.Return Value
An
ImageClassifierResultobject that contains a list of image classifications. -
Performs image classification on the provided video frame of type
MPImageusing the whole image as region of interest. Rotation will be applied according to theorientationproperty of the providedMPImage. Only use this method when theImageClassifieris created with running mode.video.It’s required to provide the video frame’s timestamp (in milliseconds). The input timestamps must be monotonically increasing.
This method supports classification of RGBA images. If your
MPImagehas a source type of.pixelBufferor.sampleBuffer, the underlying pixel buffer must usekCVPixelFormatType_32BGRAas its pixel format.If your
MPImagehas a source type of.imageensure that the color space is RGB with an Alpha channel.Declaration
Objective-C
- (nullable MPPImageClassifierResult *) classifyVideoFrame:(nonnull MPPImage *)image timestampInMilliseconds:(NSInteger)timestampInMilliseconds error:(NSError *_Nullable *_Nullable)error;Parameters
imageThe
MPImageon which image classification is to be performed.timestampInMillisecondsThe video frame’s timestamp (in milliseconds). The input timestamps must be monotonically increasing.
Return Value
An
ImageClassifierResultobject that contains a list of image classifications. -
Performs image classification on the provided video frame of type
MPImagecropped to the specified region of interest. Rotation will be applied according to theorientationproperty of the providedMPImage. Only use this method when theImageClassifieris created with.video.It’s required to provide the video frame’s timestamp (in milliseconds). The input timestamps must be monotonically increasing.
This method supports classification of RGBA images. If your
MPImagehas a source type of.pixelBufferor.sampleBuffer, the underlying pixel buffer must usekCVPixelFormatType_32BGRAas its pixel format.If your
MPImagehas a source type of.imageensure that the color space is RGB with an Alpha channel.Declaration
Objective-C
- (nullable MPPImageClassifierResult *) classifyVideoFrame:(nonnull MPPImage *)image timestampInMilliseconds:(NSInteger)timestampInMilliseconds regionOfInterest:(CGRect)roi error:(NSError *_Nullable *_Nullable)error;Parameters
imageA live stream image data of type
MPImageon which image classification is to be performed.timestampInMillisecondsThe video frame’s timestamp (in milliseconds). The input timestamps must be monotonically increasing.
roiA
CGRectspecifying the region of interest within the video frame of typeMPImage, on which image classification should be performed.Return Value
An
ImageClassifierResultobject that contains a list of image classifications. -
Sends live stream image data of type
MPImageto perform image classification using the whole image as region of interest. Rotation will be applied according to theorientationproperty of the providedMPImage. Only use this method when theImageClassifieris created with running mode.liveStream.The object which needs to be continuously notified of the available results of image classification must confirm to
ImageClassifierLiveStreamDelegateprotocol and implement theimageClassifier(_:didFinishClassificationWithResult:timestampInMilliseconds:error:)delegate method.It’s required to provide a timestamp (in milliseconds) to indicate when the input image is sent to the image classifier. The input timestamps must be monotonically increasing.
This method supports classification of RGBA images. If your
MPImagehas a source type of.pixelBufferor.sampleBuffer, the underlying pixel buffer must usekCVPixelFormatType_32BGRAas its pixel format.If the input
MPImagehas a source type of.imageensure that the color space is RGB with an Alpha channel.If this method is used for classifying live camera frames using
AVFoundation, ensure that you requestAVCaptureVideoDataOutputto output frames inkCMPixelFormat_32BGRAusing itsvideoSettingsproperty.Declaration
Objective-C
- (BOOL)classifyAsyncImage:(nonnull MPPImage *)image timestampInMilliseconds:(NSInteger)timestampInMilliseconds error:(NSError *_Nullable *_Nullable)error;Parameters
imageA live stream image data of type
MPImageon which image classification is to be performed.timestampInMillisecondsThe timestamp (in milliseconds) which indicates when the input image is sent to the image classifier. The input timestamps must be monotonically increasing.
Return Value
trueif the image was sent to the task successfully, otherwisefalse. -
Sends live stream image data of type
MPImageto perform image classification, cropped to the specified region of interest.. Rotation will be applied according to theorientationproperty of the providedMPImage. Only use this method when theImageClassifieris created with.liveStream.The object which needs to be continuously notified of the available results of image classification must confirm to
ImageClassifierLiveStreamDelegateprotocol and implement theimageClassifier(_:didFinishClassificationWithResult:timestampInMilliseconds:error:)delegate method.It’s required to provide a timestamp (in milliseconds) to indicate when the input image is sent to the image classifier. The input timestamps must be monotonically increasing.
This method supports classification of RGBA images. If your
MPImagehas a source type of.pixelBufferor.sampleBuffer, the underlying pixel buffer must usekCVPixelFormatType_32BGRAas its pixel format.If the input
MPImagehas a source type of.imageensure that the color space is RGB with an Alpha channel.If this method is used for classifying live camera frames using
AVFoundation, ensure that you requestAVCaptureVideoDataOutputto output frames inkCMPixelFormat_32BGRAusing itsvideoSettingsproperty.Declaration
Objective-C
- (BOOL)classifyAsyncImage:(nonnull MPPImage *)image timestampInMilliseconds:(NSInteger)timestampInMilliseconds regionOfInterest:(CGRect)roi error:(NSError *_Nullable *_Nullable)error;Parameters
imageA live stream image data of type
MPImageon which image classification is to be performed.timestampInMillisecondsThe timestamp (in milliseconds) which indicates when the input image is sent to the image classifier. The input timestamps must be monotonically increasing.
roiA
CGRectspecifying the region of interest within the given live stream image data of typeMPImage, on which image classification should be performed.Return Value
trueif the image was sent to the task successfully, otherwisefalse. -
Undocumented
Declaration
Objective-C
- (instancetype)init NS_UNAVAILABLE; -
Undocumented
Declaration
Objective-C
+ (instancetype)new NS_UNAVAILABLE;