Skip to content

Commit

Permalink
Recurrence examples of issues 265. googlesamples#265
Browse files Browse the repository at this point in the history
  • Loading branch information
JaminZhou committed May 11, 2021
1 parent 74d5edb commit 45a5284
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
24 changes: 21 additions & 3 deletions ios/quickstarts/vision/VisionExample/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14868" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="0Q6-lG-sCk">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="18122" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="0Q6-lG-sCk">
<device id="retina4_7" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14824"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="18093"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand Down Expand Up @@ -37,7 +38,7 @@
<subviews>
<pickerView contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Xs4-mo-RjW">
<rect key="frame" x="0.0" y="488" width="375" height="180"/>
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<constraints>
<constraint firstAttribute="height" constant="180" id="f3t-1V-izd"/>
</constraints>
Expand Down Expand Up @@ -142,12 +143,20 @@
<rect key="frame" x="0.0" y="44" width="375" height="623"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</view>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Portrait" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="i4m-Xi-iHo">
<rect key="frame" x="159" y="44" width="57" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<nil key="textColor"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstItem="i4m-Xi-iHo" firstAttribute="centerX" secondItem="Vwd-Qp-BMC" secondAttribute="centerX" id="4T8-r5-WeE"/>
<constraint firstItem="Vwd-Qp-BMC" firstAttribute="top" secondItem="bWY-Sx-TCF" secondAttribute="topMargin" id="GtM-D5-MQE"/>
<constraint firstItem="Vwd-Qp-BMC" firstAttribute="leading" secondItem="bWY-Sx-TCF" secondAttribute="leading" id="bTr-O6-WSG"/>
<constraint firstAttribute="trailing" secondItem="Vwd-Qp-BMC" secondAttribute="trailing" id="cN6-F5-qVY"/>
<constraint firstItem="i4m-Xi-iHo" firstAttribute="top" secondItem="Vwd-Qp-BMC" secondAttribute="top" id="gmZ-LD-MxY"/>
<constraint firstItem="Vwd-Qp-BMC" firstAttribute="bottom" secondItem="7NG-lt-hPa" secondAttribute="top" id="ja0-Ki-fxw"/>
</constraints>
</view>
Expand All @@ -164,10 +173,16 @@
<action selector="selectDetector:" destination="YXl-5B-mBF" id="AjI-Jr-07c"/>
</connections>
</barButtonItem>
<barButtonItem title="Orientation" id="DZy-cx-dCQ" userLabel="Orientation Button">
<connections>
<action selector="selectOrientation:" destination="YXl-5B-mBF" id="b1l-UU-GQI"/>
</connections>
</barButtonItem>
</rightBarButtonItems>
</navigationItem>
<connections>
<outlet property="cameraView" destination="Vwd-Qp-BMC" id="yRq-e8-30h"/>
<outlet property="hintLabel" destination="i4m-Xi-iHo" id="pNj-FK-5LU"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="Tub-Fx-HUo" userLabel="First Responder" sceneMemberID="firstResponder"/>
Expand All @@ -180,5 +195,8 @@
<image name="photo_library" width="36" height="36"/>
<image name="switch_camera" width="36" height="36"/>
<image name="video_camera" width="36" height="36"/>
<systemColor name="systemBackgroundColor">
<color white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</systemColor>
</resources>
</document>
39 changes: 32 additions & 7 deletions ios/quickstarts/vision/VisionExampleObjC/CameraViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@ typedef NS_ENUM(NSInteger, Detector) {
@property(nonatomic) bool isUsingFrontCamera;
@property(nonatomic, nonnull) AVCaptureVideoPreviewLayer *previewLayer;
@property(nonatomic) AVCaptureSession *captureSession;
@property(nonatomic) AVCaptureConnection *captureConnection;
@property(nonatomic) dispatch_queue_t sessionQueue;
@property(nonatomic) UIView *annotationOverlayView;
@property(nonatomic) UIImageView *previewOverlayView;
@property(weak, nonatomic) IBOutlet UIView *cameraView;
@property(weak, nonatomic) IBOutlet UILabel *hintLabel;
@property(nonatomic) CMSampleBufferRef lastFrame;

/** Initialized when one of the pose detector rows are chosen. Reset to `nil` when neither are. */
Expand Down Expand Up @@ -148,8 +150,8 @@ - (void)viewDidLoad {
@(DetectorPoseAccurate),
@(DetectorSegmentationSelfie),
];
self.currentDetector = DetectorOnDeviceFace;
_isUsingFrontCamera = YES;
self.currentDetector = DetectorOnDeviceBarcode;
_isUsingFrontCamera = NO;
_captureSession = [[AVCaptureSession alloc] init];
_sessionQueue = dispatch_queue_create(sessionQueueLabel.UTF8String, nil);
_previewOverlayView = [[UIImageView alloc] initWithFrame:CGRectZero];
Expand All @@ -159,10 +161,14 @@ - (void)viewDidLoad {
_annotationOverlayView.translatesAutoresizingMaskIntoConstraints = NO;

self.previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:_captureSession];
[self setUpPreviewOverlayView];
self.previewLayer.connection.videoOrientation = AVCaptureVideoOrientationPortrait;
self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
[self.view.layer insertSublayer:self.previewLayer atIndex:0];
//In Portrait mode, the conversion from CVImageBufferRef to UIImage is too slow. Use previewLayer instead
// [self setUpPreviewOverlayView];
[self setUpAnnotationOverlayView];
[self setUpCaptureSessionOutput];
[self setUpCaptureSessionInput];
[self setUpCaptureSessionOutput];
}

- (void)viewDidAppear:(BOOL)animated {
Expand All @@ -177,6 +183,7 @@ - (void)viewDidDisappear:(BOOL)animated {

- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
_cameraView.backgroundColor = UIColor.clearColor;
_previewLayer.frame = _cameraView.frame;
}

Expand All @@ -190,6 +197,16 @@ - (IBAction)switchCamera:(id)sender {
[self setUpCaptureSessionInput];
}

- (IBAction)selectOrientation:(id)sender {
if (self.captureConnection.videoOrientation == AVCaptureVideoOrientationPortrait) {
self.captureConnection.videoOrientation = AVCaptureVideoOrientationLandscapeRight;
self.hintLabel.text = @"LandscapeRight";
} else {
self.captureConnection.videoOrientation = AVCaptureVideoOrientationPortrait;
self.hintLabel.text = @"Portrait";
}
}

#pragma mark - On-Device Detections

- (void)detectFacesOnDeviceInImage:(MLKVisionImage *)image
Expand Down Expand Up @@ -411,6 +428,8 @@ - (void)scanBarcodesOnDeviceInImage:(MLKVisionImage *)image
if (barcodes.count == 0) {
NSLog(@"On-Device barcode scanner returned no results.");
return;
} else {
NSLog(@"Got results!");
}
for (MLKBarcode *barcode in barcodes) {
CGRect normalizedRect = CGRectMake(barcode.frame.origin.x / width, // X
Expand Down Expand Up @@ -499,7 +518,7 @@ - (void)setUpCaptureSessionOutput {
[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;
strongSelf.captureSession.sessionPreset = AVCaptureSessionPresetHigh;

AVCaptureVideoDataOutput *output = [[AVCaptureVideoDataOutput alloc] init];
output.videoSettings = @{
Expand All @@ -512,6 +531,8 @@ - (void)setUpCaptureSessionOutput {
if ([strongSelf.captureSession canAddOutput:output]) {
[strongSelf.captureSession addOutput:output];
[strongSelf.captureSession commitConfiguration];
strongSelf.captureConnection = [output connectionWithMediaType:AVMediaTypeVideo];
strongSelf.captureConnection.videoOrientation = AVCaptureVideoOrientationPortrait;
} else {
NSLog(@"%@", @"Failed to add capture session output.");
}
Expand Down Expand Up @@ -572,8 +593,8 @@ - (void)stopSession {
- (void)setUpPreviewOverlayView {
[_cameraView addSubview:_previewOverlayView];
[NSLayoutConstraint activateConstraints:@[
[_previewOverlayView.centerYAnchor constraintEqualToAnchor:_cameraView.centerYAnchor],
[_previewOverlayView.centerXAnchor constraintEqualToAnchor:_cameraView.centerXAnchor],
[_previewOverlayView.topAnchor constraintEqualToAnchor:_cameraView.topAnchor],
[_previewOverlayView.bottomAnchor constraintEqualToAnchor:_cameraView.bottomAnchor],
[_previewOverlayView.leadingAnchor constraintEqualToAnchor:_cameraView.leadingAnchor],
[_previewOverlayView.trailingAnchor constraintEqualToAnchor:_cameraView.trailingAnchor]
]];
Expand Down Expand Up @@ -644,6 +665,10 @@ - (void)updatePreviewOverlayViewWithImageBuffer:(CVImageBufferRef)imageBuffer {
}
UIImageOrientation orientation =
_isUsingFrontCamera ? UIImageOrientationLeftMirrored : UIImageOrientationRight;
if (self.captureConnection.videoOrientation == AVCaptureVideoOrientationPortrait) {
orientation = _isUsingFrontCamera ? UIImageOrientationUpMirrored : UIImageOrientationUp;
}
return; //In Portrait mode, the conversion from CVImageBufferRef to UIImage is too slow. Use previewLayer instead
UIImage *image = [UIUtilities UIImageFromImageBuffer:imageBuffer orientation:orientation];
_previewOverlayView.image = image;
}
Expand Down

0 comments on commit 45a5284

Please sign in to comment.