├── .gitignore ├── LICENSE ├── README.md ├── Vision Text Detection Demo.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ │ └── Hollis.xcuserdatad │ │ └── UserInterfaceState.xcuserstate └── xcuserdata │ └── Hollis.xcuserdatad │ └── xcschemes │ └── xcschememanagement.plist ├── Vision Text Detection Demo ├── AppDelegate.swift ├── Assets.xcassets │ └── AppIcon.appiconset │ │ └── Contents.json ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard ├── Info.plist ├── PreviewView.swift └── VNTextViewController.swift └── demo.gif /.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 | *.xcuserstate 25 | 26 | ## Obj-C/Swift specific 27 | *.hmap 28 | *.ipa 29 | *.dSYM.zip 30 | *.dSYM 31 | 32 | ## Playgrounds 33 | timeline.xctimeline 34 | playground.xcworkspace 35 | 36 | # Swift Package Manager 37 | # 38 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 39 | # Packages/ 40 | # Package.pins 41 | .build/ 42 | 43 | # CocoaPods 44 | # 45 | # We recommend against adding the Pods directory to your .gitignore. However 46 | # you should judge for yourself, the pros and cons are mentioned at: 47 | # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control 48 | # 49 | # Pods/ 50 | 51 | # Carthage 52 | # 53 | # Add this line if you want to avoid checking in source code from Carthage dependencies. 54 | # Carthage/Checkouts 55 | 56 | Carthage/Build 57 | 58 | # fastlane 59 | # 60 | # It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the 61 | # screenshots whenever they are needed. 62 | # For more information about the recommended setup visit: 63 | # https://docs.fastlane.tools/best-practices/source-control/#source-control 64 | 65 | fastlane/report.xml 66 | fastlane/Preview.html 67 | fastlane/screenshots 68 | fastlane/test_output 69 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Hollis Liu 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 | # Vision.framework Text Detection Demo 2 | 3 | ![intro](/demo.gif) 4 | 5 | ## Introduction 6 | Vision is one of the many exciting new frameworks introduced at WWDC 17. Enhanced Text detection is among the many new features in Vision.framework. But since Apple hasn't provided any demo code on the Vision text detection, here is a quick sample project. 7 | 8 | This demo app: 9 | 1. takes a live video feed, detects text regions (VNTextObservation) and draws a green box around each region 10 | 2. draws blue boxes around each character box detected (VNRectangleObservation) 11 | 12 | ## Requirements 13 | Running this app requires at least Xcode 9 beta 1 and a physical device running iOS 11 beta 1. 14 | 15 | ## Other Notes 16 | There is no OCR framework in the iOS SDK still. As a result, a seperate OCR engine is needed to recognize detected text content. 17 | -------------------------------------------------------------------------------- /Vision Text Detection Demo.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 48; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 20272F581F39F2E400540574 /* PreviewView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 20272F571F39F2E400540574 /* PreviewView.swift */; }; 11 | CB9A44BF1EEEC0D4000A9D1F /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB9A44BE1EEEC0D4000A9D1F /* AppDelegate.swift */; }; 12 | CB9A44C11EEEC0D4000A9D1F /* VNTextViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CB9A44C01EEEC0D4000A9D1F /* VNTextViewController.swift */; }; 13 | CB9A44C41EEEC0D4000A9D1F /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = CB9A44C21EEEC0D4000A9D1F /* Main.storyboard */; }; 14 | CB9A44C61EEEC0D4000A9D1F /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = CB9A44C51EEEC0D4000A9D1F /* Assets.xcassets */; }; 15 | CB9A44C91EEEC0D4000A9D1F /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = CB9A44C71EEEC0D4000A9D1F /* LaunchScreen.storyboard */; }; 16 | /* End PBXBuildFile section */ 17 | 18 | /* Begin PBXFileReference section */ 19 | 20272F571F39F2E400540574 /* PreviewView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreviewView.swift; sourceTree = ""; }; 20 | CB9A44BB1EEEC0D4000A9D1F /* Vision Text Detection Demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Vision Text Detection Demo.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 21 | CB9A44BE1EEEC0D4000A9D1F /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 22 | CB9A44C01EEEC0D4000A9D1F /* VNTextViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VNTextViewController.swift; sourceTree = ""; }; 23 | CB9A44C31EEEC0D4000A9D1F /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 24 | CB9A44C51EEEC0D4000A9D1F /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 25 | CB9A44C81EEEC0D4000A9D1F /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 26 | CB9A44CA1EEEC0D4000A9D1F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 27 | CB9A44D01EEEC855000A9D1F /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; 28 | /* End PBXFileReference section */ 29 | 30 | /* Begin PBXFrameworksBuildPhase section */ 31 | CB9A44B81EEEC0D4000A9D1F /* Frameworks */ = { 32 | isa = PBXFrameworksBuildPhase; 33 | buildActionMask = 2147483647; 34 | files = ( 35 | ); 36 | runOnlyForDeploymentPostprocessing = 0; 37 | }; 38 | /* End PBXFrameworksBuildPhase section */ 39 | 40 | /* Begin PBXGroup section */ 41 | CB9A44B21EEEC0D4000A9D1F = { 42 | isa = PBXGroup; 43 | children = ( 44 | CB9A44D01EEEC855000A9D1F /* README.md */, 45 | CB9A44BD1EEEC0D4000A9D1F /* Vision Text Detection Demo */, 46 | CB9A44BC1EEEC0D4000A9D1F /* Products */, 47 | ); 48 | sourceTree = ""; 49 | }; 50 | CB9A44BC1EEEC0D4000A9D1F /* Products */ = { 51 | isa = PBXGroup; 52 | children = ( 53 | CB9A44BB1EEEC0D4000A9D1F /* Vision Text Detection Demo.app */, 54 | ); 55 | name = Products; 56 | sourceTree = ""; 57 | }; 58 | CB9A44BD1EEEC0D4000A9D1F /* Vision Text Detection Demo */ = { 59 | isa = PBXGroup; 60 | children = ( 61 | CB9A44BE1EEEC0D4000A9D1F /* AppDelegate.swift */, 62 | CB9A44C01EEEC0D4000A9D1F /* VNTextViewController.swift */, 63 | CB9A44C21EEEC0D4000A9D1F /* Main.storyboard */, 64 | CB9A44C51EEEC0D4000A9D1F /* Assets.xcassets */, 65 | CB9A44C71EEEC0D4000A9D1F /* LaunchScreen.storyboard */, 66 | CB9A44CA1EEEC0D4000A9D1F /* Info.plist */, 67 | 20272F571F39F2E400540574 /* PreviewView.swift */, 68 | ); 69 | path = "Vision Text Detection Demo"; 70 | sourceTree = ""; 71 | }; 72 | /* End PBXGroup section */ 73 | 74 | /* Begin PBXNativeTarget section */ 75 | CB9A44BA1EEEC0D4000A9D1F /* Vision Text Detection Demo */ = { 76 | isa = PBXNativeTarget; 77 | buildConfigurationList = CB9A44CD1EEEC0D4000A9D1F /* Build configuration list for PBXNativeTarget "Vision Text Detection Demo" */; 78 | buildPhases = ( 79 | CB9A44B71EEEC0D4000A9D1F /* Sources */, 80 | CB9A44B81EEEC0D4000A9D1F /* Frameworks */, 81 | CB9A44B91EEEC0D4000A9D1F /* Resources */, 82 | ); 83 | buildRules = ( 84 | ); 85 | dependencies = ( 86 | ); 87 | name = "Vision Text Detection Demo"; 88 | productName = "Vision Text Detection Demo"; 89 | productReference = CB9A44BB1EEEC0D4000A9D1F /* Vision Text Detection Demo.app */; 90 | productType = "com.apple.product-type.application"; 91 | }; 92 | /* End PBXNativeTarget section */ 93 | 94 | /* Begin PBXProject section */ 95 | CB9A44B31EEEC0D4000A9D1F /* Project object */ = { 96 | isa = PBXProject; 97 | attributes = { 98 | LastSwiftUpdateCheck = 0900; 99 | LastUpgradeCheck = 0900; 100 | ORGANIZATIONNAME = "Hanjie Liu"; 101 | TargetAttributes = { 102 | CB9A44BA1EEEC0D4000A9D1F = { 103 | CreatedOnToolsVersion = 9.0; 104 | }; 105 | }; 106 | }; 107 | buildConfigurationList = CB9A44B61EEEC0D4000A9D1F /* Build configuration list for PBXProject "Vision Text Detection Demo" */; 108 | compatibilityVersion = "Xcode 8.0"; 109 | developmentRegion = en; 110 | hasScannedForEncodings = 0; 111 | knownRegions = ( 112 | en, 113 | Base, 114 | ); 115 | mainGroup = CB9A44B21EEEC0D4000A9D1F; 116 | productRefGroup = CB9A44BC1EEEC0D4000A9D1F /* Products */; 117 | projectDirPath = ""; 118 | projectRoot = ""; 119 | targets = ( 120 | CB9A44BA1EEEC0D4000A9D1F /* Vision Text Detection Demo */, 121 | ); 122 | }; 123 | /* End PBXProject section */ 124 | 125 | /* Begin PBXResourcesBuildPhase section */ 126 | CB9A44B91EEEC0D4000A9D1F /* Resources */ = { 127 | isa = PBXResourcesBuildPhase; 128 | buildActionMask = 2147483647; 129 | files = ( 130 | CB9A44C91EEEC0D4000A9D1F /* LaunchScreen.storyboard in Resources */, 131 | CB9A44C61EEEC0D4000A9D1F /* Assets.xcassets in Resources */, 132 | CB9A44C41EEEC0D4000A9D1F /* Main.storyboard in Resources */, 133 | ); 134 | runOnlyForDeploymentPostprocessing = 0; 135 | }; 136 | /* End PBXResourcesBuildPhase section */ 137 | 138 | /* Begin PBXSourcesBuildPhase section */ 139 | CB9A44B71EEEC0D4000A9D1F /* Sources */ = { 140 | isa = PBXSourcesBuildPhase; 141 | buildActionMask = 2147483647; 142 | files = ( 143 | CB9A44C11EEEC0D4000A9D1F /* VNTextViewController.swift in Sources */, 144 | CB9A44BF1EEEC0D4000A9D1F /* AppDelegate.swift in Sources */, 145 | 20272F581F39F2E400540574 /* PreviewView.swift in Sources */, 146 | ); 147 | runOnlyForDeploymentPostprocessing = 0; 148 | }; 149 | /* End PBXSourcesBuildPhase section */ 150 | 151 | /* Begin PBXVariantGroup section */ 152 | CB9A44C21EEEC0D4000A9D1F /* Main.storyboard */ = { 153 | isa = PBXVariantGroup; 154 | children = ( 155 | CB9A44C31EEEC0D4000A9D1F /* Base */, 156 | ); 157 | name = Main.storyboard; 158 | sourceTree = ""; 159 | }; 160 | CB9A44C71EEEC0D4000A9D1F /* LaunchScreen.storyboard */ = { 161 | isa = PBXVariantGroup; 162 | children = ( 163 | CB9A44C81EEEC0D4000A9D1F /* Base */, 164 | ); 165 | name = LaunchScreen.storyboard; 166 | sourceTree = ""; 167 | }; 168 | /* End PBXVariantGroup section */ 169 | 170 | /* Begin XCBuildConfiguration section */ 171 | CB9A44CB1EEEC0D4000A9D1F /* Debug */ = { 172 | isa = XCBuildConfiguration; 173 | buildSettings = { 174 | ALWAYS_SEARCH_USER_PATHS = NO; 175 | CLANG_ANALYZER_NONNULL = YES; 176 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 177 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 178 | CLANG_CXX_LIBRARY = "libc++"; 179 | CLANG_ENABLE_MODULES = YES; 180 | CLANG_ENABLE_OBJC_ARC = YES; 181 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 182 | CLANG_WARN_BOOL_CONVERSION = YES; 183 | CLANG_WARN_COMMA = YES; 184 | CLANG_WARN_CONSTANT_CONVERSION = YES; 185 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 186 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 187 | CLANG_WARN_EMPTY_BODY = YES; 188 | CLANG_WARN_ENUM_CONVERSION = YES; 189 | CLANG_WARN_INFINITE_RECURSION = YES; 190 | CLANG_WARN_INT_CONVERSION = YES; 191 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 192 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 193 | CLANG_WARN_STRICT_PROTOTYPES = YES; 194 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 195 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 196 | CLANG_WARN_UNREACHABLE_CODE = YES; 197 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 198 | CODE_SIGN_IDENTITY = "iPhone Developer"; 199 | COPY_PHASE_STRIP = NO; 200 | DEBUG_INFORMATION_FORMAT = dwarf; 201 | ENABLE_STRICT_OBJC_MSGSEND = YES; 202 | ENABLE_TESTABILITY = YES; 203 | GCC_C_LANGUAGE_STANDARD = gnu11; 204 | GCC_DYNAMIC_NO_PIC = NO; 205 | GCC_NO_COMMON_BLOCKS = YES; 206 | GCC_OPTIMIZATION_LEVEL = 0; 207 | GCC_PREPROCESSOR_DEFINITIONS = ( 208 | "DEBUG=1", 209 | "$(inherited)", 210 | ); 211 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 212 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 213 | GCC_WARN_UNDECLARED_SELECTOR = YES; 214 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 215 | GCC_WARN_UNUSED_FUNCTION = YES; 216 | GCC_WARN_UNUSED_VARIABLE = YES; 217 | IPHONEOS_DEPLOYMENT_TARGET = 11.0; 218 | MTL_ENABLE_DEBUG_INFO = YES; 219 | ONLY_ACTIVE_ARCH = YES; 220 | SDKROOT = iphoneos; 221 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 222 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 223 | }; 224 | name = Debug; 225 | }; 226 | CB9A44CC1EEEC0D4000A9D1F /* Release */ = { 227 | isa = XCBuildConfiguration; 228 | buildSettings = { 229 | ALWAYS_SEARCH_USER_PATHS = NO; 230 | CLANG_ANALYZER_NONNULL = YES; 231 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 232 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 233 | CLANG_CXX_LIBRARY = "libc++"; 234 | CLANG_ENABLE_MODULES = YES; 235 | CLANG_ENABLE_OBJC_ARC = YES; 236 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 237 | CLANG_WARN_BOOL_CONVERSION = YES; 238 | CLANG_WARN_COMMA = YES; 239 | CLANG_WARN_CONSTANT_CONVERSION = YES; 240 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 241 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 242 | CLANG_WARN_EMPTY_BODY = YES; 243 | CLANG_WARN_ENUM_CONVERSION = YES; 244 | CLANG_WARN_INFINITE_RECURSION = YES; 245 | CLANG_WARN_INT_CONVERSION = YES; 246 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 247 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 248 | CLANG_WARN_STRICT_PROTOTYPES = YES; 249 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 250 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 251 | CLANG_WARN_UNREACHABLE_CODE = YES; 252 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 253 | CODE_SIGN_IDENTITY = "iPhone Developer"; 254 | COPY_PHASE_STRIP = NO; 255 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 256 | ENABLE_NS_ASSERTIONS = NO; 257 | ENABLE_STRICT_OBJC_MSGSEND = YES; 258 | GCC_C_LANGUAGE_STANDARD = gnu11; 259 | GCC_NO_COMMON_BLOCKS = YES; 260 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 261 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 262 | GCC_WARN_UNDECLARED_SELECTOR = YES; 263 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 264 | GCC_WARN_UNUSED_FUNCTION = YES; 265 | GCC_WARN_UNUSED_VARIABLE = YES; 266 | IPHONEOS_DEPLOYMENT_TARGET = 11.0; 267 | MTL_ENABLE_DEBUG_INFO = NO; 268 | SDKROOT = iphoneos; 269 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 270 | VALIDATE_PRODUCT = YES; 271 | }; 272 | name = Release; 273 | }; 274 | CB9A44CE1EEEC0D4000A9D1F /* Debug */ = { 275 | isa = XCBuildConfiguration; 276 | buildSettings = { 277 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 278 | DEVELOPMENT_TEAM = 6F9VT4DEVJ; 279 | INFOPLIST_FILE = "Vision Text Detection Demo/Info.plist"; 280 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 281 | PRODUCT_BUNDLE_IDENTIFIER = "com.hanjieliu.Vision-Text-Detection-Demo"; 282 | PRODUCT_NAME = "$(TARGET_NAME)"; 283 | SWIFT_VERSION = 4.0; 284 | TARGETED_DEVICE_FAMILY = "1,2"; 285 | }; 286 | name = Debug; 287 | }; 288 | CB9A44CF1EEEC0D4000A9D1F /* Release */ = { 289 | isa = XCBuildConfiguration; 290 | buildSettings = { 291 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 292 | DEVELOPMENT_TEAM = 6F9VT4DEVJ; 293 | INFOPLIST_FILE = "Vision Text Detection Demo/Info.plist"; 294 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 295 | PRODUCT_BUNDLE_IDENTIFIER = "com.hanjieliu.Vision-Text-Detection-Demo"; 296 | PRODUCT_NAME = "$(TARGET_NAME)"; 297 | SWIFT_VERSION = 4.0; 298 | TARGETED_DEVICE_FAMILY = "1,2"; 299 | }; 300 | name = Release; 301 | }; 302 | /* End XCBuildConfiguration section */ 303 | 304 | /* Begin XCConfigurationList section */ 305 | CB9A44B61EEEC0D4000A9D1F /* Build configuration list for PBXProject "Vision Text Detection Demo" */ = { 306 | isa = XCConfigurationList; 307 | buildConfigurations = ( 308 | CB9A44CB1EEEC0D4000A9D1F /* Debug */, 309 | CB9A44CC1EEEC0D4000A9D1F /* Release */, 310 | ); 311 | defaultConfigurationIsVisible = 0; 312 | defaultConfigurationName = Release; 313 | }; 314 | CB9A44CD1EEEC0D4000A9D1F /* Build configuration list for PBXNativeTarget "Vision Text Detection Demo" */ = { 315 | isa = XCConfigurationList; 316 | buildConfigurations = ( 317 | CB9A44CE1EEEC0D4000A9D1F /* Debug */, 318 | CB9A44CF1EEEC0D4000A9D1F /* Release */, 319 | ); 320 | defaultConfigurationIsVisible = 0; 321 | }; 322 | /* End XCConfigurationList section */ 323 | }; 324 | rootObject = CB9A44B31EEEC0D4000A9D1F /* Project object */; 325 | } 326 | -------------------------------------------------------------------------------- /Vision Text Detection Demo.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /Vision Text Detection Demo.xcodeproj/project.xcworkspace/xcuserdata/Hollis.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hollisliu/iOS-Vision-Text-Detection-Demo/f92152e1356cc60830b197ae1f5a944906b5037f/Vision Text Detection Demo.xcodeproj/project.xcworkspace/xcuserdata/Hollis.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /Vision Text Detection Demo.xcodeproj/xcuserdata/Hollis.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | Vision Text Detection Demo.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /Vision Text Detection Demo/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // Vision Text Detection Demo 4 | // 5 | // Created by Hanjie Liu on 6/12/17. 6 | // Copyright © 2017 Hanjie Liu. 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 | -------------------------------------------------------------------------------- /Vision Text Detection Demo/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 | } -------------------------------------------------------------------------------- /Vision Text Detection Demo/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 | -------------------------------------------------------------------------------- /Vision Text Detection Demo/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 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /Vision Text Detection Demo/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | NSCameraUsageDescription 6 | Vision Text Detection from Camera 7 | CFBundleDevelopmentRegion 8 | $(DEVELOPMENT_LANGUAGE) 9 | CFBundleExecutable 10 | $(EXECUTABLE_NAME) 11 | CFBundleIdentifier 12 | $(PRODUCT_BUNDLE_IDENTIFIER) 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | $(PRODUCT_NAME) 17 | CFBundlePackageType 18 | APPL 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleVersion 22 | 1 23 | LSRequiresIPhoneOS 24 | 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationPortrait 36 | UIInterfaceOrientationLandscapeLeft 37 | UIInterfaceOrientationLandscapeRight 38 | 39 | UISupportedInterfaceOrientations~ipad 40 | 41 | UIInterfaceOrientationPortrait 42 | UIInterfaceOrientationPortraitUpsideDown 43 | UIInterfaceOrientationLandscapeLeft 44 | UIInterfaceOrientationLandscapeRight 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /Vision Text Detection Demo/PreviewView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // PreviewView.swift 3 | // Vision Text Detection Demo 4 | // 5 | // Created by Khurram Shehzad on 08/08/2017. 6 | // Copyright © 2017 Hanjie Liu. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import AVFoundation 11 | class PreviewView: UIView { 12 | var videoPreviewLayer: AVCaptureVideoPreviewLayer { 13 | guard let layer = layer as? AVCaptureVideoPreviewLayer else { 14 | fatalError("Expected `AVCaptureVideoPreviewLayer` type for layer. Check PreviewView.layerClass implementation.") 15 | } 16 | 17 | return layer 18 | } 19 | 20 | var session: AVCaptureSession? { 21 | get { 22 | return videoPreviewLayer.session 23 | } 24 | set { 25 | videoPreviewLayer.session = newValue 26 | } 27 | } 28 | 29 | // MARK: UIView 30 | 31 | override class var layerClass: AnyClass { 32 | return AVCaptureVideoPreviewLayer.self 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Vision Text Detection Demo/VNTextViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // Vision Text Detection Demo 4 | // 5 | // Created by Hanjie Liu on 6/12/17. 6 | // Copyright © 2017 Hanjie Liu. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import Vision 11 | import AVFoundation 12 | 13 | class VNTextViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate { 14 | private var requests = [VNRequest]() 15 | private let session = AVCaptureSession() 16 | 17 | @IBOutlet weak var previewView: PreviewView! 18 | 19 | override func viewDidLoad() { 20 | super.viewDidLoad() 21 | 22 | setupCamera() 23 | setupVision() 24 | } 25 | 26 | // MARK: - Vision Setup 27 | func setupVision() { 28 | let textRequest = VNDetectTextRectanglesRequest(completionHandler: self.textDetectionHandler) 29 | textRequest.reportCharacterBoxes = true 30 | 31 | self.requests = [textRequest] 32 | } 33 | 34 | func textDetectionHandler(request: VNRequest, error: Error?) { 35 | guard let observations = request.results else {print("no result"); return} 36 | 37 | let result = observations.map({$0 as? VNTextObservation}) 38 | 39 | DispatchQueue.main.async() { 40 | self.previewView.layer.sublayers?.removeSubrange(1...) 41 | for region in result { 42 | guard let rg = region else {continue} 43 | self.drawRegionBox(box: rg) 44 | if let boxes = region?.characterBoxes { 45 | for characterBox in boxes { 46 | self.drawTextBox(box: characterBox) 47 | } 48 | } 49 | } 50 | } 51 | } 52 | 53 | // MARK: - Draw 54 | func drawRegionBox(box: VNTextObservation) { 55 | guard let boxes = box.characterBoxes else {return} 56 | var xMin: CGFloat = 9999.0 57 | var xMax: CGFloat = 0.0 58 | var yMin: CGFloat = 9999.0 59 | var yMax: CGFloat = 0.0 60 | 61 | for char in boxes { 62 | if char.bottomLeft.x < xMin {xMin = char.bottomLeft.x} 63 | if char.bottomRight.x > xMax {xMax = char.bottomRight.x} 64 | if char.bottomRight.y < yMin {yMin = char.bottomRight.y} 65 | if char.topRight.y > yMax {yMax = char.topRight.y} 66 | } 67 | 68 | let xCoord = xMin * previewView.frame.size.width 69 | let yCoord = (1 - yMax) * previewView.frame.size.height 70 | let width = (xMax - xMin) * previewView.frame.size.width 71 | let height = (yMax - yMin) * previewView.frame.size.height 72 | 73 | let layer = CALayer() 74 | layer.frame = CGRect(x: xCoord, y: yCoord, width: width, height: height) 75 | layer.borderWidth = 2.0 76 | layer.borderColor = UIColor.green.cgColor 77 | 78 | previewView.layer.addSublayer(layer) 79 | } 80 | 81 | func drawTextBox(box: VNRectangleObservation) { 82 | let xCoord = box.topLeft.x * previewView.frame.size.width 83 | let yCoord = (1 - box.topLeft.y) * previewView.frame.size.height 84 | let width = (box.topRight.x - box.bottomLeft.x) * previewView.frame.size.width 85 | let height = (box.topLeft.y - box.bottomLeft.y) * previewView.frame.size.height 86 | 87 | let layer = CALayer() 88 | layer.frame = CGRect(x: xCoord, y: yCoord, width: width, height: height) 89 | layer.borderWidth = 1.0 90 | layer.borderColor = UIColor.blue.cgColor 91 | 92 | previewView.layer.addSublayer(layer) 93 | } 94 | 95 | 96 | // MARK: - Camera Delegate and Setup 97 | func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) { 98 | guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else {return} 99 | 100 | var requestOptions:[VNImageOption : Any] = [:] 101 | 102 | if let camData = CMGetAttachment(sampleBuffer, kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix, nil) { 103 | requestOptions = [.cameraIntrinsics:camData] 104 | } 105 | 106 | let imageRequestHandler = VNImageRequestHandler(cvPixelBuffer: pixelBuffer, orientation: CGImagePropertyOrientation(rawValue: 6)!, options: requestOptions) 107 | 108 | do { 109 | try imageRequestHandler.perform(self.requests) 110 | } catch { 111 | print(error) 112 | } 113 | } 114 | 115 | func setupCamera() { 116 | previewView.session = session 117 | let availableCameraDevices = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .back) 118 | 119 | var activeDevice: AVCaptureDevice? 120 | 121 | for device in availableCameraDevices.devices as [AVCaptureDevice]{ 122 | if device.position == .back { 123 | activeDevice = device 124 | break 125 | } 126 | } 127 | 128 | do { 129 | let camInput = try AVCaptureDeviceInput(device: activeDevice!) 130 | 131 | if session.canAddInput(camInput) { 132 | session.addInput(camInput) 133 | } 134 | } catch { 135 | print("no camera") 136 | } 137 | session.sessionPreset = .high 138 | guard auth() else {return} 139 | 140 | let videoOutput = AVCaptureVideoDataOutput() 141 | 142 | videoOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "buffer queue", qos: .userInteractive, attributes: .concurrent, autoreleaseFrequency: .inherit, target: nil)) 143 | 144 | if session.canAddOutput(videoOutput) { 145 | session.addOutput(videoOutput) 146 | } 147 | previewView.videoPreviewLayer.videoGravity = .resize 148 | session.startRunning() 149 | } 150 | 151 | private func auth() -> Bool{ 152 | let authorizationStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.video) 153 | switch authorizationStatus { 154 | case .notDetermined: 155 | AVCaptureDevice.requestAccess(for: AVMediaType.video, 156 | completionHandler: { (granted:Bool) -> Void in 157 | if granted { 158 | DispatchQueue.main.async { 159 | self.previewView.setNeedsDisplay() 160 | } 161 | } 162 | }) 163 | return true 164 | case .authorized: 165 | return true 166 | case .denied, .restricted: return false 167 | } 168 | } 169 | 170 | } 171 | 172 | -------------------------------------------------------------------------------- /demo.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hollisliu/iOS-Vision-Text-Detection-Demo/f92152e1356cc60830b197ae1f5a944906b5037f/demo.gif --------------------------------------------------------------------------------