-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathCameraViewController.m
631 lines (570 loc) · 25.8 KB
/
CameraViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
//
// Copyright (c) 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#import "CameraViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <CoreImage/CoreImage.h>
#import <CoreVideo/CoreVideo.h>
#import "UIUtilities.h"
@import MLKit;
NS_ASSUME_NONNULL_BEGIN
static NSString *const alertControllerTitle = @"Vision Detectors";
static NSString *const alertControllerMessage = @"Select a detector";
static NSString *const cancelActionTitleText = @"Cancel";
static NSString *const videoDataOutputQueueLabel = @"com.google.mlkit.automl.VideoDataOutputQueue";
static NSString *const sessionQueueLabel = @"com.google.mlkit.automl.SessionQueue";
static NSString *const noResultsMessage = @"No Results";
/** Name of the remote AutoML model. */
static NSString *const MLKRemoteAutoMLModelName = @"remote_automl_model";
/** Filename of AutoML local model manifest in the main resource bundle. */
static NSString *const MLKAutoMLLocalModelManifestFilename = @"automl_labeler_manifest";
/** File type of AutoML local model manifest in the main resource bundle. */
static NSString *const MLKAutoMLManifestFileType = @"json";
static const float kLabelConfidenceThreshold = 0.75f;
static const CGFloat kImageScale = 1.0;
static const CGFloat kLayoutPadding = 10.0;
static const CGFloat kResultsLabelHeight = 200.0;
static const int kResultsLabelLines = 5;
@interface CameraViewController () <AVCaptureVideoDataOutputSampleBufferDelegate>
typedef NS_ENUM(NSInteger, Detector) {
/** AutoML image label detector. */
DetectorImageLabelsAutoML,
/** AutoML object detector, single, only tracking. */
DetectorObjectsAutoMLSingleNoClassifier,
/** AutoML object detector, single, with classification. */
DetectorObjectsAutoMLSingleWithClassifier,
/** AutoML object detector, multiple, only tracking. */
DetectorObjectsAutoMLMultipleNoClassifier,
/** AutoML object detector, multiple, with classification. */
DetectorObjectsAutoMLMultipleWithClassifier,
};
@property(nonatomic) NSArray *detectors;
@property(nonatomic) Detector currentDetector;
@property(nonatomic) bool isUsingFrontCamera;
@property(nonatomic, nonnull) AVCaptureVideoPreviewLayer *previewLayer;
@property(nonatomic) AVCaptureSession *captureSession;
@property(nonatomic) dispatch_queue_t sessionQueue;
@property(nonatomic) UIView *annotationOverlayView;
@property(nonatomic) UIImageView *previewOverlayView;
@property(weak, nonatomic) IBOutlet UIView *cameraView;
@property(nonatomic) CMSampleBufferRef lastFrame;
@property(nonatomic) MLKModelManager *modelManager;
@property(strong, nonatomic) IBOutlet UIProgressView *downloadProgressView;
@end
@implementation CameraViewController
- (NSString *)stringForDetector:(Detector)detector {
switch (detector) {
case DetectorImageLabelsAutoML:
return @"AutoML Image Labeling";
case DetectorObjectsAutoMLSingleNoClassifier:
return @"AutoML ODT, single, no labeling";
case DetectorObjectsAutoMLSingleWithClassifier:
return @"AutoML ODT, single, labeling";
case DetectorObjectsAutoMLMultipleNoClassifier:
return @"AutoML ODT, multiple, no labeling";
case DetectorObjectsAutoMLMultipleWithClassifier:
return @"AutoML ODT, multiple, labeling";
}
}
- (void)viewDidLoad {
[super viewDidLoad];
self.detectors = @[
@(DetectorImageLabelsAutoML),
@(DetectorObjectsAutoMLSingleNoClassifier),
@(DetectorObjectsAutoMLSingleWithClassifier),
@(DetectorObjectsAutoMLMultipleNoClassifier),
@(DetectorObjectsAutoMLMultipleWithClassifier),
];
self.currentDetector = DetectorImageLabelsAutoML;
self.isUsingFrontCamera = YES;
self.captureSession = [[AVCaptureSession alloc] init];
self.sessionQueue = dispatch_queue_create(sessionQueueLabel.UTF8String, nil);
self.modelManager = [MLKModelManager modelManager];
self.previewOverlayView = [[UIImageView alloc] initWithFrame:CGRectZero];
self.previewOverlayView.contentMode = UIViewContentModeScaleAspectFill;
self.previewOverlayView.translatesAutoresizingMaskIntoConstraints = NO;
self.annotationOverlayView = [[UIView alloc] initWithFrame:CGRectZero];
self.annotationOverlayView.translatesAutoresizingMaskIntoConstraints = NO;
self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];
[self setUpPreviewOverlayView];
[self setUpAnnotationOverlayView];
[self setUpCaptureSessionOutput];
[self setUpCaptureSessionInput];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[self startSession];
[NSNotificationCenter.defaultCenter addObserver:self
selector:@selector(remoteModelDownloadDidSucceed:)
name:MLKModelDownloadDidSucceedNotification
object:nil];
[NSNotificationCenter.defaultCenter addObserver:self
selector:@selector(remoteModelDownloadDidFail:)
name:MLKModelDownloadDidFailNotification
object:nil];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[self stopSession];
// We wouldn't have needed to remove the observers if iOS 9.0+ had cleaned up the observer "the
// next time it would have posted to it" as documented here:
// https://developer.apple.com/documentation/foundation/nsnotificationcenter/1413994-removeobserver
[NSNotificationCenter.defaultCenter removeObserver:self
name:MLKModelDownloadDidSucceedNotification
object:nil];
[NSNotificationCenter.defaultCenter removeObserver:self
name:MLKModelDownloadDidFailNotification
object:nil];
}
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
self.previewLayer.frame = self.cameraView.frame;
}
- (IBAction)selectDetector:(id)sender {
[self presentDetectorsAlertController];
}
- (IBAction)switchCamera:(id)sender {
self.isUsingFrontCamera = !self.isUsingFrontCamera;
[self removeDetectionAnnotations];
[self setUpCaptureSessionInput];
}
#pragma mark - AutoML Image Labeling
/**
* Detects labels on the specified image using AutoML-trained models via Custom Image Labeling API.
*
* @param image The input image.
*/
- (void)detectImageLabelsInImage:(MLKVisionImage *)image {
[self requestAutoMLRemoteModelIfNeeded];
// [START config_automl_label]
MLKCommonImageLabelerOptions *options;
MLKCustomRemoteModel *remoteModel = (MLKCustomRemoteModel *)[self remoteModel];
if ([self.modelManager isModelDownloaded:remoteModel]) {
NSLog(@"Use AutoML remote model.");
options = [[MLKCustomImageLabelerOptions alloc] initWithRemoteModel:remoteModel];
} else {
NSLog(@"Use AutoML local model.");
NSString *localModelFilePath =
[[NSBundle mainBundle] pathForResource:MLKAutoMLLocalModelManifestFilename
ofType:MLKAutoMLManifestFileType];
if (localModelFilePath == nil) {
NSLog(@"Failed to find AutoML local model manifest file: %@",
MLKAutoMLLocalModelManifestFilename);
return;
}
MLKLocalModel *localModel = [[MLKLocalModel alloc] initWithManifestPath:localModelFilePath];
options = [[MLKCustomImageLabelerOptions alloc] initWithLocalModel:localModel];
}
options.confidenceThreshold = @(kLabelConfidenceThreshold);
// [END config_automl_label]
// [START init_automl_label]
MLKImageLabeler *autoMLImageLabeler = [MLKImageLabeler imageLabelerWithOptions:options];
// [END init_automl_label]
// [START detect_automl_label]
NSError *error;
NSArray<MLKImageLabel *> *labels = [autoMLImageLabeler resultsInImage:image error:&error];
// [START_EXCLUDE]
__weak typeof(self) weakSelf = self;
dispatch_sync(dispatch_get_main_queue(), ^{
__strong typeof(weakSelf) strongSelf = weakSelf;
[strongSelf updatePreviewOverlayView];
[strongSelf removeDetectionAnnotations];
// [END_EXCLUDE]
if (error != nil) {
// [START_EXCLUDE]
NSLog(@"Failed to detect labels with error: %@.", error.localizedDescription);
// [END_EXCLUDE]
return;
}
if (labels.count == 0) {
return;
}
// [START_EXCLUDE]
CGRect annotationFrame = strongSelf.annotationOverlayView.frame;
CGRect resultsRect =
CGRectMake(annotationFrame.origin.x + kLayoutPadding,
annotationFrame.size.height - kLayoutPadding - kResultsLabelHeight,
annotationFrame.size.width - 2 * kLayoutPadding, kResultsLabelHeight);
UILabel *resultsLabel = [[UILabel alloc] initWithFrame:resultsRect];
resultsLabel.textColor = UIColor.yellowColor;
NSMutableArray *labelStrings = [NSMutableArray arrayWithCapacity:labels.count];
for (MLKImageLabel *label in labels) {
[labelStrings addObject:[NSString stringWithFormat:@"Label: %@, Confidence: %f", label.text,
label.confidence]];
}
resultsLabel.text = [labelStrings componentsJoinedByString:@"\n"];
resultsLabel.adjustsFontSizeToFitWidth = YES;
resultsLabel.numberOfLines = kResultsLabelLines;
[strongSelf.annotationOverlayView addSubview:resultsLabel];
});
// [END_EXCLUDE]
// [END detect_automl_label]
}
- (void)detectObjectsInImage:(MLKVisionImage *)image
width:(CGFloat)width
height:(CGFloat)height
shouldEnableClassification:(BOOL)shouldEnableClassification
shouldEnableMultipleObjects:(BOOL)shouldEnableMultipleObjects {
[self requestAutoMLRemoteModelIfNeeded];
MLKCustomRemoteModel *remoteModel = (MLKCustomRemoteModel *)[self remoteModel];
MLKCustomObjectDetectorOptions *options;
if ([self.modelManager isModelDownloaded:remoteModel]) {
NSLog(@"Use AutoML remote model.");
options = [[MLKCustomObjectDetectorOptions alloc] initWithRemoteModel:remoteModel];
} else {
NSLog(@"Use AutoML local model.");
NSString *localModelFilePath =
[[NSBundle mainBundle] pathForResource:MLKAutoMLLocalModelManifestFilename
ofType:MLKAutoMLManifestFileType];
if (localModelFilePath == nil) {
NSLog(@"Failed to find AutoML local model manifest file: %@",
MLKAutoMLLocalModelManifestFilename);
return;
}
MLKLocalModel *localModel = [[MLKLocalModel alloc] initWithManifestPath:localModelFilePath];
options = [[MLKCustomObjectDetectorOptions alloc] initWithLocalModel:localModel];
}
options.shouldEnableClassification = shouldEnableClassification;
options.shouldEnableMultipleObjects = shouldEnableMultipleObjects;
options.detectorMode = MLKObjectDetectorModeStream;
// Due to the UI space, We will only display one label per detected object.
options.maxPerObjectLabelCount = 1;
MLKObjectDetector *autoMLObjectDetector = [MLKObjectDetector objectDetectorWithOptions:options];
NSError *error;
NSArray<MLKObject *> *objects = [autoMLObjectDetector resultsInImage:image error:&error];
__weak typeof(self) weakSelf = self;
dispatch_sync(dispatch_get_main_queue(), ^{
__strong typeof(weakSelf) strongSelf = weakSelf;
[strongSelf updatePreviewOverlayView];
[strongSelf removeDetectionAnnotations];
if (error != nil) {
NSLog(@"Failed to detect object with error: %@", error.localizedDescription);
return;
}
if (objects.count == 0) {
NSLog(@"Object detector returned no results.");
return;
}
for (MLKObject *object in objects) {
NSMutableString *description = [[NSMutableString alloc] init];
CGRect normalizedRect =
CGRectMake(object.frame.origin.x / width, object.frame.origin.y / height,
object.frame.size.width / width, object.frame.size.height / height);
CGRect standardizedRect = CGRectStandardize(
[strongSelf.previewLayer rectForMetadataOutputRectOfInterest:normalizedRect]);
[UIUtilities addRectangle:standardizedRect
toView:strongSelf.annotationOverlayView
color:UIColor.greenColor];
UILabel *label = [[UILabel alloc] initWithFrame:standardizedRect];
if (object.trackingID != nil) {
[description appendFormat:@"Object ID: %@\n", object.trackingID];
}
[description appendString:@"Labels:\n"];
int i = 0;
for (MLKObjectLabel *l in object.labels) {
NSString *labelString = [NSString stringWithFormat:@"Label %d: %@, %f, %lu\n", i++, l.text,
l.confidence, (unsigned long)l.index];
[description appendString:labelString];
}
label.text = description;
label.numberOfLines = 0;
label.adjustsFontSizeToFitWidth = YES;
[strongSelf.annotationOverlayView addSubview:label];
}
});
}
- (void)requestAutoMLRemoteModelIfNeeded {
MLKRemoteModel *remoteModel = [self remoteModel];
if ([self.modelManager isModelDownloaded:remoteModel]) {
return;
}
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
__strong typeof(weakSelf) strongSelf = weakSelf;
strongSelf.downloadProgressView.hidden = NO;
MLKModelDownloadConditions *conditions =
[[MLKModelDownloadConditions alloc] initWithAllowsCellularAccess:YES
allowsBackgroundDownloading:YES];
strongSelf.downloadProgressView.observedProgress =
[strongSelf.modelManager downloadModel:remoteModel conditions:conditions];
NSLog(@"Start downloading AutoML remote model.");
});
}
#pragma mark - Notifications
- (void)remoteModelDownloadDidSucceed:(NSNotification *)notification {
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
__strong typeof(weakSelf) strongSelf = weakSelf;
strongSelf.downloadProgressView.hidden = YES;
MLKRemoteModel *remotemodel = notification.userInfo[MLKModelDownloadUserInfoKeyRemoteModel];
if (remotemodel == nil) {
NSLog(@"MLKitModelDownloadDidSucceed notification posted without a RemoteModel instance.");
return;
}
NSLog(@"Successfully downloaded the remote model with name: %@. The model is ready for "
@"detection.",
remotemodel.name);
});
}
- (void)remoteModelDownloadDidFail:(NSNotification *)notification {
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
__strong typeof(weakSelf) strongSelf = weakSelf;
strongSelf.downloadProgressView.hidden = YES;
MLKRemoteModel *remoteModel = notification.userInfo[MLKModelDownloadUserInfoKeyRemoteModel];
NSError *error = notification.userInfo[MLKModelDownloadUserInfoKeyError];
if (error == nil) {
NSLog(@"MLKitModelDownloadDidFail notification posted without a RemoteModel instance or "
@"error.");
return;
}
NSLog(@"Failed to download the remote model with name: %@, error: %@.", remoteModel,
error.localizedDescription);
});
}
#pragma mark - Private
- (MLKRemoteModel *)remoteModel {
MLKFirebaseModelSource *firebaseModelSource =
[[MLKFirebaseModelSource alloc] initWithName:MLKRemoteAutoMLModelName];
return [[MLKCustomRemoteModel alloc] initWithRemoteModelSource:firebaseModelSource];
}
- (void)setUpCaptureSessionOutput {
__weak typeof(self) weakSelf = self;
dispatch_async(self.sessionQueue, ^{
__strong typeof(weakSelf) strongSelf = weakSelf;
[strongSelf.captureSession beginConfiguration];
// When performing latency tests to determine ideal capture settings,
// run the app in 'release' mode to get accurate performance metrics
strongSelf.captureSession.sessionPreset = AVCaptureSessionPresetMedium;
AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
output.videoSettings = @{
(id)
kCVPixelBufferPixelFormatTypeKey : [NSNumber numberWithUnsignedInt:kCVPixelFormatType_32BGRA]
};
dispatch_queue_t outputQueue = dispatch_queue_create(videoDataOutputQueueLabel.UTF8String, nil);
[output setSampleBufferDelegate:strongSelf queue:outputQueue];
if ([strongSelf.captureSession canAddOutput:output]) {
[strongSelf.captureSession addOutput:output];
[strongSelf.captureSession commitConfiguration];
} else {
NSLog(@"%@", @"Failed to add capture session output.");
}
});
}
- (void)setUpCaptureSessionInput {
__weak typeof(self) weakSelf = self;
dispatch_async(self.sessionQueue, ^{
__strong typeof(weakSelf) strongSelf = weakSelf;
AVCaptureDevicePosition cameraPosition =
strongSelf.isUsingFrontCamera ? AVCaptureDevicePositionFront : AVCaptureDevicePositionBack;
AVCaptureDevice *device = [strongSelf captureDeviceForPosition:cameraPosition];
if (device) {
[strongSelf.captureSession beginConfiguration];
NSArray<AVCaptureInput *> *currentInputs = strongSelf.captureSession.inputs;
for (AVCaptureInput *input in currentInputs) {
[strongSelf.captureSession removeInput:input];
}
NSError *error;
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device
error:&error];
if (error) {
NSLog(@"Failed to create capture device input: %@", error.localizedDescription);
return;
} else {
if ([strongSelf.captureSession canAddInput:input]) {
[strongSelf.captureSession addInput:input];
} else {
NSLog(@"%@", @"Failed to add capture session input.");
}
}
[strongSelf.captureSession commitConfiguration];
} else {
NSLog(@"Failed to get capture device for camera position: %ld", cameraPosition);
}
});
}
- (void)startSession {
__weak typeof(self) weakSelf = self;
dispatch_async(self.sessionQueue, ^{
__strong typeof(weakSelf) strongSelf = weakSelf;
[strongSelf.captureSession startRunning];
});
}
- (void)stopSession {
__weak typeof(self) weakSelf = self;
dispatch_async(self.sessionQueue, ^{
__strong typeof(weakSelf) strongSelf = weakSelf;
[strongSelf.captureSession stopRunning];
});
}
- (void)setUpPreviewOverlayView {
[self.cameraView addSubview:self.previewOverlayView];
[NSLayoutConstraint activateConstraints:@[
[self.previewOverlayView.centerYAnchor constraintEqualToAnchor:self.cameraView.centerYAnchor],
[self.previewOverlayView.centerXAnchor constraintEqualToAnchor:self.cameraView.centerXAnchor],
[self.previewOverlayView.leadingAnchor constraintEqualToAnchor:self.cameraView.leadingAnchor],
[self.previewOverlayView.trailingAnchor constraintEqualToAnchor:self.cameraView.trailingAnchor]
]];
}
- (void)setUpAnnotationOverlayView {
[self.cameraView addSubview:self.annotationOverlayView];
[NSLayoutConstraint activateConstraints:@[
[self.annotationOverlayView.topAnchor constraintEqualToAnchor:self.cameraView.topAnchor],
[self.annotationOverlayView.leadingAnchor
constraintEqualToAnchor:self.cameraView.leadingAnchor],
[self.annotationOverlayView.trailingAnchor
constraintEqualToAnchor:self.cameraView.trailingAnchor],
[self.annotationOverlayView.bottomAnchor constraintEqualToAnchor:self.cameraView.bottomAnchor]
]];
}
- (AVCaptureDevice *)captureDeviceForPosition:(AVCaptureDevicePosition)position {
if (@available(iOS 10, *)) {
AVCaptureDeviceDiscoverySession *discoverySession = [AVCaptureDeviceDiscoverySession
discoverySessionWithDeviceTypes:@[ AVCaptureDeviceTypeBuiltInWideAngleCamera ]
mediaType:AVMediaTypeVideo
position:AVCaptureDevicePositionUnspecified];
for (AVCaptureDevice *device in discoverySession.devices) {
if (device.position == position) {
return device;
}
}
}
return nil;
}
- (void)presentDetectorsAlertController {
UIAlertController *alertController =
[UIAlertController alertControllerWithTitle:alertControllerTitle
message:alertControllerMessage
preferredStyle:UIAlertControllerStyleAlert];
for (NSNumber *detectorType in self.detectors) {
NSInteger detector = detectorType.integerValue;
__weak typeof(self) weakSelf = self;
UIAlertAction *action = [UIAlertAction actionWithTitle:[self stringForDetector:detector]
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *_Nonnull action) {
__strong typeof(weakSelf) strongSelf =
weakSelf;
strongSelf.currentDetector = detector;
[strongSelf removeDetectionAnnotations];
}];
if (detector == self.currentDetector) {
[action setEnabled:NO];
}
[alertController addAction:action];
}
[alertController addAction:[UIAlertAction actionWithTitle:cancelActionTitleText
style:UIAlertActionStyleCancel
handler:nil]];
[self presentViewController:alertController animated:YES completion:nil];
}
- (void)removeDetectionAnnotations {
for (UIView *annotationView in self.annotationOverlayView.subviews) {
[annotationView removeFromSuperview];
}
}
- (void)updatePreviewOverlayView {
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(self.lastFrame);
if (imageBuffer == nil) {
return;
}
CIImage *ciImage = [CIImage imageWithCVPixelBuffer:imageBuffer];
CIContext *context = [[CIContext alloc] initWithOptions:nil];
CGImageRef cgImage = [context createCGImage:ciImage fromRect:ciImage.extent];
if (cgImage == nil) {
return;
}
UIImage *rotatedImage = [UIImage imageWithCGImage:cgImage
scale:kImageScale
orientation:UIImageOrientationRight];
if (self.isUsingFrontCamera) {
CGImageRef rotatedCGImage = rotatedImage.CGImage;
if (rotatedCGImage == nil) {
return;
}
UIImage *mirroredImage = [UIImage imageWithCGImage:rotatedCGImage
scale:kImageScale
orientation:UIImageOrientationLeftMirrored];
self.previewOverlayView.image = mirroredImage;
} else {
self.previewOverlayView.image = rotatedImage;
}
CGImageRelease(cgImage);
}
- (NSArray<NSValue *> *)convertedPointsFromPoints:(NSArray<NSValue *> *)points
width:(CGFloat)width
height:(CGFloat)height {
NSMutableArray *result = [NSMutableArray arrayWithCapacity:points.count];
for (NSValue *point in points) {
CGPoint cgPointValue = point.CGPointValue;
CGPoint normalizedPoint = CGPointMake(cgPointValue.x / width, cgPointValue.y / height);
CGPoint cgPoint = [self.previewLayer pointForCaptureDevicePointOfInterest:normalizedPoint];
[result addObject:[NSValue valueWithCGPoint:cgPoint]];
}
return result;
}
#pragma mark - AVCaptureVideoDataOutputSampleBufferDelegate
- (void)captureOutput:(AVCaptureOutput *)output
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
fromConnection:(AVCaptureConnection *)connection {
CVImageBufferRef imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer);
if (imageBuffer == nil) {
NSLog(@"%@", @"Failed to get image buffer from sample buffer.");
return;
}
// Evaluate `self.currentDetector` once to ensure consistency throughout this method since it
// can be concurrently modified from the main thread.
Detector activeDetector = self.currentDetector;
self.lastFrame = sampleBuffer;
MLKVisionImage *visionImage = [[MLKVisionImage alloc] initWithBuffer:sampleBuffer];
UIImageOrientation orientation = [UIUtilities
imageOrientationFromDevicePosition:self.isUsingFrontCamera ? AVCaptureDevicePositionFront
: AVCaptureDevicePositionBack];
visionImage.orientation = orientation;
CGFloat imageWidth = CVPixelBufferGetWidth(imageBuffer);
CGFloat imageHeight = CVPixelBufferGetHeight(imageBuffer);
BOOL shouldEnableClassification = NO;
BOOL shouldEnableMultipleObjects = NO;
switch (activeDetector) {
case DetectorObjectsAutoMLSingleWithClassifier:
case DetectorObjectsAutoMLMultipleWithClassifier:
shouldEnableClassification = YES;
default:
break;
}
switch (activeDetector) {
case DetectorObjectsAutoMLMultipleNoClassifier:
case DetectorObjectsAutoMLMultipleWithClassifier:
shouldEnableMultipleObjects = YES;
default:
break;
}
switch (activeDetector) {
case DetectorImageLabelsAutoML:
[self detectImageLabelsInImage:visionImage];
break;
case DetectorObjectsAutoMLSingleNoClassifier:
case DetectorObjectsAutoMLSingleWithClassifier:
case DetectorObjectsAutoMLMultipleNoClassifier:
case DetectorObjectsAutoMLMultipleWithClassifier:
[self detectObjectsInImage:visionImage
width:imageWidth
height:imageHeight
shouldEnableClassification:shouldEnableClassification
shouldEnableMultipleObjects:shouldEnableMultipleObjects];
break;
}
}
@end
NS_ASSUME_NONNULL_END