├── .gitattributes ├── .gitignore ├── AddingMLModel.gif ├── LICENSE ├── README.md ├── VisionSample.xcodeproj ├── project.pbxproj └── project.xcworkspace │ └── contents.xcworkspacedata ├── VisionSample ├── AppDelegate.swift ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard ├── Info.plist └── ViewController.swift └── demo.gif /.gitattributes: -------------------------------------------------------------------------------- 1 | *.mlmodel filter=lfs diff=lfs merge=lfs -text 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Xcode 2 | # 3 | # gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore 4 | 5 | ## Build generated 6 | build/ 7 | DerivedData/ 8 | 9 | ## Various settings 10 | *.pbxuser 11 | !default.pbxuser 12 | *.mode1v3 13 | !default.mode1v3 14 | *.mode2v3 15 | !default.mode2v3 16 | *.perspectivev3 17 | !default.perspectivev3 18 | xcuserdata/ 19 | 20 | ## Other 21 | *.moved-aside 22 | *.xccheckout 23 | *.xcscmblueprint 24 | 25 | ## Big machine learning models 26 | *.mlmodel 27 | -------------------------------------------------------------------------------- /AddingMLModel.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/atomic14/VisionCoreMLSample/88279db263482635e4d34755e6e6ebd44cc27b6a/AddingMLModel.gif -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Chris 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Getting started 2 | 3 | [![Recognising objects](demo.gif)](https://www.youtube.com/watch?v=S33BNcnlxdU) 4 | 5 | Click [here](https://www.youtube.com/watch?v=S33BNcnlxdU) for a better demo. 6 | 7 | You can find a detailed breakdown of the code here: https://blog.cmgresearch.com/2017/06/22/vision-kit-and-coreml.html 8 | 9 | You will need the beta version of xCode and a device running the iOS 11 beta (make sure you only install the beta software on a test device!). 10 | 11 | ### CoreML Model 12 | 13 | ~~If you have [Git LFS](https://git-lfs.github.com) installed, the required CoreML will download as part of the git clone. Otherwise you can download the CoreML model - Resnet50.mlmodel from here:~~ 14 | 15 | Unfortunately we've had to remove the model from Git LFS as we were running out bandwidth! 16 | 17 | You can download the CoreML model - Resnet50.mlmodel from here: 18 | 19 | [https://developer.apple.com/machine-learning/](https://developer.apple.com/machine-learning/) and drag the downloaded file into your project. Make sure you click on the "copy" checkbox. 20 | 21 | ![Adding ML Model to Project](AddingMLModel.gif) 22 | -------------------------------------------------------------------------------- /VisionSample.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 48; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 88C6A9C31EF863CC00CC4360 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88C6A9C21EF863CC00CC4360 /* AppDelegate.swift */; }; 11 | 88C6A9C51EF863CC00CC4360 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 88C6A9C41EF863CC00CC4360 /* ViewController.swift */; }; 12 | 88C6A9C81EF863CC00CC4360 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 88C6A9C61EF863CC00CC4360 /* Main.storyboard */; }; 13 | 88C6A9CA1EF863CC00CC4360 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 88C6A9C91EF863CC00CC4360 /* Assets.xcassets */; }; 14 | 88C6A9CD1EF863CC00CC4360 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 88C6A9CB1EF863CC00CC4360 /* LaunchScreen.storyboard */; }; 15 | /* End PBXBuildFile section */ 16 | 17 | /* Begin PBXFileReference section */ 18 | 88C6A9BF1EF863CC00CC4360 /* VisionSample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = VisionSample.app; sourceTree = BUILT_PRODUCTS_DIR; }; 19 | 88C6A9C21EF863CC00CC4360 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 20 | 88C6A9C41EF863CC00CC4360 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 21 | 88C6A9C71EF863CC00CC4360 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 22 | 88C6A9C91EF863CC00CC4360 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 23 | 88C6A9CC1EF863CC00CC4360 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 24 | 88C6A9CE1EF863CC00CC4360 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 25 | /* End PBXFileReference section */ 26 | 27 | /* Begin PBXFrameworksBuildPhase section */ 28 | 88C6A9BC1EF863CC00CC4360 /* Frameworks */ = { 29 | isa = PBXFrameworksBuildPhase; 30 | buildActionMask = 2147483647; 31 | files = ( 32 | ); 33 | runOnlyForDeploymentPostprocessing = 0; 34 | }; 35 | /* End PBXFrameworksBuildPhase section */ 36 | 37 | /* Begin PBXGroup section */ 38 | 88C6A9B61EF863CB00CC4360 = { 39 | isa = PBXGroup; 40 | children = ( 41 | 88C6A9C11EF863CC00CC4360 /* VisionSample */, 42 | 88C6A9C01EF863CC00CC4360 /* Products */, 43 | ); 44 | sourceTree = ""; 45 | }; 46 | 88C6A9C01EF863CC00CC4360 /* Products */ = { 47 | isa = PBXGroup; 48 | children = ( 49 | 88C6A9BF1EF863CC00CC4360 /* VisionSample.app */, 50 | ); 51 | name = Products; 52 | sourceTree = ""; 53 | }; 54 | 88C6A9C11EF863CC00CC4360 /* VisionSample */ = { 55 | isa = PBXGroup; 56 | children = ( 57 | 88C6A9C21EF863CC00CC4360 /* AppDelegate.swift */, 58 | 88C6A9C41EF863CC00CC4360 /* ViewController.swift */, 59 | 88C6A9C61EF863CC00CC4360 /* Main.storyboard */, 60 | 88C6A9C91EF863CC00CC4360 /* Assets.xcassets */, 61 | 88C6A9CB1EF863CC00CC4360 /* LaunchScreen.storyboard */, 62 | 88C6A9CE1EF863CC00CC4360 /* Info.plist */, 63 | ); 64 | path = VisionSample; 65 | sourceTree = ""; 66 | }; 67 | /* End PBXGroup section */ 68 | 69 | /* Begin PBXNativeTarget section */ 70 | 88C6A9BE1EF863CC00CC4360 /* VisionSample */ = { 71 | isa = PBXNativeTarget; 72 | buildConfigurationList = 88C6A9D11EF863CC00CC4360 /* Build configuration list for PBXNativeTarget "VisionSample" */; 73 | buildPhases = ( 74 | 88C6A9BB1EF863CC00CC4360 /* Sources */, 75 | 88C6A9BC1EF863CC00CC4360 /* Frameworks */, 76 | 88C6A9BD1EF863CC00CC4360 /* Resources */, 77 | ); 78 | buildRules = ( 79 | ); 80 | dependencies = ( 81 | ); 82 | name = VisionSample; 83 | productName = VisionSample; 84 | productReference = 88C6A9BF1EF863CC00CC4360 /* VisionSample.app */; 85 | productType = "com.apple.product-type.application"; 86 | }; 87 | /* End PBXNativeTarget section */ 88 | 89 | /* Begin PBXProject section */ 90 | 88C6A9B71EF863CB00CC4360 /* Project object */ = { 91 | isa = PBXProject; 92 | attributes = { 93 | LastSwiftUpdateCheck = 0900; 94 | LastUpgradeCheck = 0900; 95 | ORGANIZATIONNAME = "MRM Brand Ltd"; 96 | TargetAttributes = { 97 | 88C6A9BE1EF863CC00CC4360 = { 98 | CreatedOnToolsVersion = 9.0; 99 | }; 100 | }; 101 | }; 102 | buildConfigurationList = 88C6A9BA1EF863CB00CC4360 /* Build configuration list for PBXProject "VisionSample" */; 103 | compatibilityVersion = "Xcode 8.0"; 104 | developmentRegion = en; 105 | hasScannedForEncodings = 0; 106 | knownRegions = ( 107 | en, 108 | Base, 109 | ); 110 | mainGroup = 88C6A9B61EF863CB00CC4360; 111 | productRefGroup = 88C6A9C01EF863CC00CC4360 /* Products */; 112 | projectDirPath = ""; 113 | projectRoot = ""; 114 | targets = ( 115 | 88C6A9BE1EF863CC00CC4360 /* VisionSample */, 116 | ); 117 | }; 118 | /* End PBXProject section */ 119 | 120 | /* Begin PBXResourcesBuildPhase section */ 121 | 88C6A9BD1EF863CC00CC4360 /* Resources */ = { 122 | isa = PBXResourcesBuildPhase; 123 | buildActionMask = 2147483647; 124 | files = ( 125 | 88C6A9CD1EF863CC00CC4360 /* LaunchScreen.storyboard in Resources */, 126 | 88C6A9CA1EF863CC00CC4360 /* Assets.xcassets in Resources */, 127 | 88C6A9C81EF863CC00CC4360 /* Main.storyboard in Resources */, 128 | ); 129 | runOnlyForDeploymentPostprocessing = 0; 130 | }; 131 | /* End PBXResourcesBuildPhase section */ 132 | 133 | /* Begin PBXSourcesBuildPhase section */ 134 | 88C6A9BB1EF863CC00CC4360 /* Sources */ = { 135 | isa = PBXSourcesBuildPhase; 136 | buildActionMask = 2147483647; 137 | files = ( 138 | 88C6A9C51EF863CC00CC4360 /* ViewController.swift in Sources */, 139 | 88C6A9C31EF863CC00CC4360 /* AppDelegate.swift in Sources */, 140 | ); 141 | runOnlyForDeploymentPostprocessing = 0; 142 | }; 143 | /* End PBXSourcesBuildPhase section */ 144 | 145 | /* Begin PBXVariantGroup section */ 146 | 88C6A9C61EF863CC00CC4360 /* Main.storyboard */ = { 147 | isa = PBXVariantGroup; 148 | children = ( 149 | 88C6A9C71EF863CC00CC4360 /* Base */, 150 | ); 151 | name = Main.storyboard; 152 | sourceTree = ""; 153 | }; 154 | 88C6A9CB1EF863CC00CC4360 /* LaunchScreen.storyboard */ = { 155 | isa = PBXVariantGroup; 156 | children = ( 157 | 88C6A9CC1EF863CC00CC4360 /* Base */, 158 | ); 159 | name = LaunchScreen.storyboard; 160 | sourceTree = ""; 161 | }; 162 | /* End PBXVariantGroup section */ 163 | 164 | /* Begin XCBuildConfiguration section */ 165 | 88C6A9CF1EF863CC00CC4360 /* Debug */ = { 166 | isa = XCBuildConfiguration; 167 | buildSettings = { 168 | ALWAYS_SEARCH_USER_PATHS = NO; 169 | CLANG_ANALYZER_NONNULL = YES; 170 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 171 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 172 | CLANG_CXX_LIBRARY = "libc++"; 173 | CLANG_ENABLE_MODULES = YES; 174 | CLANG_ENABLE_OBJC_ARC = YES; 175 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 176 | CLANG_WARN_BOOL_CONVERSION = YES; 177 | CLANG_WARN_COMMA = YES; 178 | CLANG_WARN_CONSTANT_CONVERSION = YES; 179 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 180 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 181 | CLANG_WARN_EMPTY_BODY = YES; 182 | CLANG_WARN_ENUM_CONVERSION = YES; 183 | CLANG_WARN_INFINITE_RECURSION = YES; 184 | CLANG_WARN_INT_CONVERSION = YES; 185 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 186 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 187 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 188 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 189 | CLANG_WARN_STRICT_PROTOTYPES = YES; 190 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 191 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 192 | CLANG_WARN_UNREACHABLE_CODE = YES; 193 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 194 | CODE_SIGN_IDENTITY = "iPhone Developer"; 195 | COPY_PHASE_STRIP = NO; 196 | DEBUG_INFORMATION_FORMAT = dwarf; 197 | ENABLE_STRICT_OBJC_MSGSEND = YES; 198 | ENABLE_TESTABILITY = YES; 199 | GCC_C_LANGUAGE_STANDARD = gnu11; 200 | GCC_DYNAMIC_NO_PIC = NO; 201 | GCC_NO_COMMON_BLOCKS = YES; 202 | GCC_OPTIMIZATION_LEVEL = 0; 203 | GCC_PREPROCESSOR_DEFINITIONS = ( 204 | "DEBUG=1", 205 | "$(inherited)", 206 | ); 207 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 208 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 209 | GCC_WARN_UNDECLARED_SELECTOR = YES; 210 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 211 | GCC_WARN_UNUSED_FUNCTION = YES; 212 | GCC_WARN_UNUSED_VARIABLE = YES; 213 | IPHONEOS_DEPLOYMENT_TARGET = 11.0; 214 | MTL_ENABLE_DEBUG_INFO = YES; 215 | ONLY_ACTIVE_ARCH = YES; 216 | SDKROOT = iphoneos; 217 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 218 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 219 | }; 220 | name = Debug; 221 | }; 222 | 88C6A9D01EF863CC00CC4360 /* Release */ = { 223 | isa = XCBuildConfiguration; 224 | buildSettings = { 225 | ALWAYS_SEARCH_USER_PATHS = NO; 226 | CLANG_ANALYZER_NONNULL = YES; 227 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 228 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 229 | CLANG_CXX_LIBRARY = "libc++"; 230 | CLANG_ENABLE_MODULES = YES; 231 | CLANG_ENABLE_OBJC_ARC = YES; 232 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 233 | CLANG_WARN_BOOL_CONVERSION = YES; 234 | CLANG_WARN_COMMA = YES; 235 | CLANG_WARN_CONSTANT_CONVERSION = YES; 236 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 237 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 238 | CLANG_WARN_EMPTY_BODY = YES; 239 | CLANG_WARN_ENUM_CONVERSION = YES; 240 | CLANG_WARN_INFINITE_RECURSION = YES; 241 | CLANG_WARN_INT_CONVERSION = YES; 242 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 243 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 244 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 245 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 246 | CLANG_WARN_STRICT_PROTOTYPES = YES; 247 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 248 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 249 | CLANG_WARN_UNREACHABLE_CODE = YES; 250 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 251 | CODE_SIGN_IDENTITY = "iPhone Developer"; 252 | COPY_PHASE_STRIP = NO; 253 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 254 | ENABLE_NS_ASSERTIONS = NO; 255 | ENABLE_STRICT_OBJC_MSGSEND = YES; 256 | GCC_C_LANGUAGE_STANDARD = gnu11; 257 | GCC_NO_COMMON_BLOCKS = YES; 258 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 259 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 260 | GCC_WARN_UNDECLARED_SELECTOR = YES; 261 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 262 | GCC_WARN_UNUSED_FUNCTION = YES; 263 | GCC_WARN_UNUSED_VARIABLE = YES; 264 | IPHONEOS_DEPLOYMENT_TARGET = 11.0; 265 | MTL_ENABLE_DEBUG_INFO = NO; 266 | SDKROOT = iphoneos; 267 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 268 | VALIDATE_PRODUCT = YES; 269 | }; 270 | name = Release; 271 | }; 272 | 88C6A9D21EF863CC00CC4360 /* Debug */ = { 273 | isa = XCBuildConfiguration; 274 | buildSettings = { 275 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 276 | DEVELOPMENT_TEAM = E22377CEYL; 277 | INFOPLIST_FILE = VisionSample/Info.plist; 278 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 279 | PRODUCT_BUNDLE_IDENTIFIER = com.mrmbrand.VisionSample; 280 | PRODUCT_NAME = "$(TARGET_NAME)"; 281 | SWIFT_VERSION = 4.0; 282 | TARGETED_DEVICE_FAMILY = "1,2"; 283 | }; 284 | name = Debug; 285 | }; 286 | 88C6A9D31EF863CC00CC4360 /* Release */ = { 287 | isa = XCBuildConfiguration; 288 | buildSettings = { 289 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 290 | DEVELOPMENT_TEAM = E22377CEYL; 291 | INFOPLIST_FILE = VisionSample/Info.plist; 292 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 293 | PRODUCT_BUNDLE_IDENTIFIER = com.mrmbrand.VisionSample; 294 | PRODUCT_NAME = "$(TARGET_NAME)"; 295 | SWIFT_VERSION = 4.0; 296 | TARGETED_DEVICE_FAMILY = "1,2"; 297 | }; 298 | name = Release; 299 | }; 300 | /* End XCBuildConfiguration section */ 301 | 302 | /* Begin XCConfigurationList section */ 303 | 88C6A9BA1EF863CB00CC4360 /* Build configuration list for PBXProject "VisionSample" */ = { 304 | isa = XCConfigurationList; 305 | buildConfigurations = ( 306 | 88C6A9CF1EF863CC00CC4360 /* Debug */, 307 | 88C6A9D01EF863CC00CC4360 /* Release */, 308 | ); 309 | defaultConfigurationIsVisible = 0; 310 | defaultConfigurationName = Release; 311 | }; 312 | 88C6A9D11EF863CC00CC4360 /* Build configuration list for PBXNativeTarget "VisionSample" */ = { 313 | isa = XCConfigurationList; 314 | buildConfigurations = ( 315 | 88C6A9D21EF863CC00CC4360 /* Debug */, 316 | 88C6A9D31EF863CC00CC4360 /* Release */, 317 | ); 318 | defaultConfigurationIsVisible = 0; 319 | defaultConfigurationName = Release; 320 | }; 321 | /* End XCConfigurationList section */ 322 | }; 323 | rootObject = 88C6A9B71EF863CB00CC4360 /* Project object */; 324 | } 325 | -------------------------------------------------------------------------------- /VisionSample.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /VisionSample/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // VisionSample 4 | // 5 | // Created by chris on 19/06/2017. 6 | // Copyright © 2017 MRM Brand Ltd. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | @UIApplicationMain 12 | class AppDelegate: UIResponder, UIApplicationDelegate { 13 | 14 | var window: UIWindow? 15 | 16 | 17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 18 | // Override point for customization after application launch. 19 | return true 20 | } 21 | 22 | func applicationWillResignActive(_ application: UIApplication) { 23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 24 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 25 | } 26 | 27 | func applicationDidEnterBackground(_ application: UIApplication) { 28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 30 | } 31 | 32 | func applicationWillEnterForeground(_ application: UIApplication) { 33 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 34 | } 35 | 36 | func applicationDidBecomeActive(_ application: UIApplication) { 37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 38 | } 39 | 40 | func applicationWillTerminate(_ application: UIApplication) { 41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 42 | } 43 | 44 | 45 | } 46 | 47 | -------------------------------------------------------------------------------- /VisionSample/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "20x20", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "20x20", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "29x29", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "29x29", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "40x40", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "40x40", 31 | "scale" : "3x" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "size" : "60x60", 36 | "scale" : "2x" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "size" : "60x60", 41 | "scale" : "3x" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "size" : "20x20", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "size" : "20x20", 51 | "scale" : "2x" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "size" : "29x29", 56 | "scale" : "1x" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "size" : "29x29", 61 | "scale" : "2x" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "size" : "40x40", 66 | "scale" : "1x" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "size" : "40x40", 71 | "scale" : "2x" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "size" : "76x76", 76 | "scale" : "1x" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "size" : "76x76", 81 | "scale" : "2x" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "size" : "83.5x83.5", 86 | "scale" : "2x" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "size" : "1024x1024", 91 | "scale" : "1x" 92 | } 93 | ], 94 | "info" : { 95 | "version" : 1, 96 | "author" : "xcode" 97 | } 98 | } -------------------------------------------------------------------------------- /VisionSample/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /VisionSample/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 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 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 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 | -------------------------------------------------------------------------------- /VisionSample/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | NSCameraUsageDescription 24 | AI Man 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | 37 | UISupportedInterfaceOrientations~ipad 38 | 39 | UIInterfaceOrientationPortrait 40 | UIInterfaceOrientationPortraitUpsideDown 41 | UIInterfaceOrientationLandscapeLeft 42 | UIInterfaceOrientationLandscapeRight 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /VisionSample/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // VisionSample 4 | // 5 | // Created by chris on 19/06/2017. 6 | // Copyright © 2017 MRM Brand Ltd. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import AVFoundation 11 | import Vision 12 | 13 | class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate { 14 | // video capture session 15 | let session = AVCaptureSession() 16 | // preview layer 17 | var previewLayer: AVCaptureVideoPreviewLayer! 18 | // queue for processing video frames 19 | let captureQueue = DispatchQueue(label: "captureQueue") 20 | // overlay layer 21 | var gradientLayer: CAGradientLayer! 22 | // vision request 23 | var visionRequests = [VNRequest]() 24 | 25 | var recognitionThreshold : Float = 0.25 26 | 27 | @IBOutlet weak var thresholdStackView: UIStackView! 28 | @IBOutlet weak var threshholdLabel: UILabel! 29 | @IBOutlet weak var threshholdSlider: UISlider! 30 | 31 | @IBOutlet weak var previewView: UIView! 32 | @IBOutlet weak var resultView: UILabel! 33 | 34 | override func viewDidLoad() { 35 | super.viewDidLoad() 36 | // get hold of the default video camera 37 | guard let camera = AVCaptureDevice.default(for: .video) else { 38 | fatalError("No video camera available") 39 | } 40 | do { 41 | // add the preview layer 42 | previewLayer = AVCaptureVideoPreviewLayer(session: session) 43 | previewView.layer.addSublayer(previewLayer) 44 | // add a slight gradient overlay so we can read the results easily 45 | gradientLayer = CAGradientLayer() 46 | gradientLayer.colors = [ 47 | UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.7).cgColor, 48 | UIColor.init(red: 0, green: 0, blue: 0, alpha: 0.0).cgColor, 49 | ] 50 | gradientLayer.locations = [0.0, 0.3] 51 | self.previewView.layer.addSublayer(gradientLayer) 52 | 53 | // create the capture input and the video output 54 | let cameraInput = try AVCaptureDeviceInput(device: camera) 55 | 56 | let videoOutput = AVCaptureVideoDataOutput() 57 | videoOutput.setSampleBufferDelegate(self, queue: captureQueue) 58 | videoOutput.alwaysDiscardsLateVideoFrames = true 59 | videoOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA] 60 | session.sessionPreset = .high 61 | 62 | // wire up the session 63 | session.addInput(cameraInput) 64 | session.addOutput(videoOutput) 65 | 66 | // make sure we are in portrait mode 67 | let conn = videoOutput.connection(with: .video) 68 | conn?.videoOrientation = .portrait 69 | 70 | // Start the session 71 | session.startRunning() 72 | 73 | // set up the vision model 74 | guard let resNet50Model = try? VNCoreMLModel(for: Resnet50().model) else { 75 | fatalError("Could not load model") 76 | } 77 | // set up the request using our vision model 78 | let classificationRequest = VNCoreMLRequest(model: resNet50Model, completionHandler: handleClassifications) 79 | classificationRequest.imageCropAndScaleOption = .centerCrop 80 | visionRequests = [classificationRequest] 81 | } catch { 82 | fatalError(error.localizedDescription) 83 | } 84 | 85 | updateThreshholdLabel() 86 | } 87 | 88 | func updateThreshholdLabel () { 89 | self.threshholdLabel.text = "Threshold: " + String(format: "%.2f", recognitionThreshold) 90 | } 91 | 92 | override func viewDidLayoutSubviews() { 93 | super.viewDidLayoutSubviews() 94 | previewLayer.frame = self.previewView.bounds; 95 | gradientLayer.frame = self.previewView.bounds; 96 | 97 | let orientation: UIDeviceOrientation = UIDevice.current.orientation; 98 | switch (orientation) { 99 | case .portrait: 100 | previewLayer?.connection?.videoOrientation = .portrait 101 | case .landscapeRight: 102 | previewLayer?.connection?.videoOrientation = .landscapeLeft 103 | case .landscapeLeft: 104 | previewLayer?.connection?.videoOrientation = .landscapeRight 105 | case .portraitUpsideDown: 106 | previewLayer?.connection?.videoOrientation = .portraitUpsideDown 107 | default: 108 | previewLayer?.connection?.videoOrientation = .portrait 109 | } 110 | } 111 | 112 | func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) { 113 | guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { 114 | return 115 | } 116 | 117 | connection.videoOrientation = .portrait 118 | 119 | var requestOptions:[VNImageOption: Any] = [:] 120 | 121 | if let cameraIntrinsicData = CMGetAttachment(sampleBuffer, kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix, nil) { 122 | requestOptions = [.cameraIntrinsics: cameraIntrinsicData] 123 | } 124 | 125 | // for orientation see kCGImagePropertyOrientation 126 | let imageRequestHandler = VNImageRequestHandler(cvPixelBuffer: pixelBuffer, orientation: .upMirrored, options: requestOptions) 127 | do { 128 | try imageRequestHandler.perform(self.visionRequests) 129 | } catch { 130 | print(error) 131 | } 132 | } 133 | 134 | @IBAction func userTapped(sender: Any) { 135 | self.thresholdStackView.isHidden = !self.thresholdStackView.isHidden 136 | } 137 | 138 | @IBAction func sliderValueChanged(slider: UISlider) { 139 | self.recognitionThreshold = slider.value 140 | updateThreshholdLabel() 141 | } 142 | 143 | func handleClassifications(request: VNRequest, error: Error?) { 144 | if let theError = error { 145 | print("Error: \(theError.localizedDescription)") 146 | return 147 | } 148 | guard let observations = request.results else { 149 | print("No results") 150 | return 151 | } 152 | 153 | let classifications = observations[0...4] // top 4 results 154 | .flatMap({ $0 as? VNClassificationObservation }) 155 | .flatMap({$0.confidence > recognitionThreshold ? $0 : nil}) 156 | .map({ "\($0.identifier) \(String(format:"%.2f", $0.confidence))" }) 157 | .joined(separator: "\n") 158 | 159 | DispatchQueue.main.async { 160 | self.resultView.text = classifications 161 | } 162 | 163 | } 164 | } 165 | 166 | -------------------------------------------------------------------------------- /demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/atomic14/VisionCoreMLSample/88279db263482635e4d34755e6e6ebd44cc27b6a/demo.gif --------------------------------------------------------------------------------