├── .gitignore ├── LICENSE ├── README.md ├── TextDetection-CoreML.xcodeproj ├── project.pbxproj └── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ └── IDEWorkspaceChecks.plist ├── TextDetection-CoreML ├── AppDelegate.swift ├── Assets.xcassets │ ├── AppIcon.appiconset │ │ └── Contents.json │ └── Contents.json ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard ├── DrawingView.swift ├── Info.plist ├── Measure.swift ├── VideoCapture.swift └── ViewController.swift ├── TextDetection-CoreMLTests ├── Info.plist └── TextDetection_CoreMLTests.swift └── resource ├── TextDetection-CoreML_DEMO001.gif └── prerequest_001_plist.png /.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 | ## Obj-C/Swift specific 26 | *.hmap 27 | *.ipa 28 | *.dSYM.zip 29 | *.dSYM 30 | 31 | ## Playgrounds 32 | timeline.xctimeline 33 | playground.xcworkspace 34 | 35 | # Swift Package Manager 36 | # 37 | # Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. 38 | # Packages/ 39 | # Package.pins 40 | # Package.resolved 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/**/*.png 68 | fastlane/test_output 69 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 tucan9389 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 | # TextDetection-CoreML 2 | 3 | ![platform-ios](https://img.shields.io/badge/platform-ios-lightgrey.svg) 4 | ![swift-version](https://img.shields.io/badge/swift-4.2-red.svg) 5 | ![lisence](https://img.shields.io/badge/license-MIT-black.svg) 6 | 7 | This project is Text Detection on iOS using [Vision](https://developer.apple.com/documentation/vision) built-in model.
If you are interested in iOS + Machine Learning, visit [here](https://github.com/motlabs/iOS-Proejcts-with-ML-Models) you can see various DEMOs.
8 | 9 | ![TextDetection-CoreML_DEMO001](resource/TextDetection-CoreML_DEMO001.gif) 10 | 11 | ## Requirements 12 | 13 | - Xcode 9.2+ 14 | - iOS 12.0+ 15 | - Swift 4.2 16 | 17 | ## Performance 18 | 19 | ### Inference Time 20 | 21 | | device | inference time | 22 | | -------- | -------------- | 23 | | iPhone X | 10 ms | 24 | 25 | ## Build & Run 26 | 27 | ### 1. Prerequisites 28 | 29 | #### Add permission in info.plist for device's camera access 30 | 31 | ![prerequest_001_plist](resource/prerequest_001_plist.png) 32 | 33 | ### 2. Dependencies 34 | 35 | No external library yet. 36 | 37 | ### 3. Code 38 | 39 | #### 3.1 Import Vision framework 40 | 41 | ```swift 42 | import Vision 43 | ``` 44 | 45 | #### 3.2 Define properties for Vision 46 | 47 | ```swift 48 | // properties on ViewController 49 | var request: VNDetectTextRectanglesRequest? 50 | ``` 51 | 52 | #### 3.3 Configure and prepare 53 | 54 | ```swift 55 | override func viewDidLoad() { 56 | super.viewDidLoad() 57 | 58 | let request = VNDetectTextRectanglesRequest(completionHandler: self.visionRequestDidComplete) 59 | request.reportCharacterBoxes = true 60 | self.request = request 61 | } 62 | 63 | func visionRequestDidComplete(request: VNRequest, error: Error?) { 64 | /* ------------------------------------------------------ */ 65 | /* something postprocessing what you want after inference */ 66 | /* ------------------------------------------------------ */ 67 | } 68 | ``` 69 | 70 | #### 3.4 Inference 🏃‍♂️ 71 | 72 | ```swift 73 | // on the inference point 74 | let handler = VNImageRequestHandler(cvPixelBuffer: pixelBuffer) 75 | if let request = request { 76 | try? handler.perform([self.request]) 77 | } 78 | ``` 79 | 80 | -------------------------------------------------------------------------------- /TextDetection-CoreML.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 55042E6B221DB3A100E8381A /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55042E6A221DB3A100E8381A /* AppDelegate.swift */; }; 11 | 55042E6D221DB3A100E8381A /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55042E6C221DB3A100E8381A /* ViewController.swift */; }; 12 | 55042E70221DB3A100E8381A /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 55042E6E221DB3A100E8381A /* Main.storyboard */; }; 13 | 55042E72221DB3A200E8381A /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 55042E71221DB3A200E8381A /* Assets.xcassets */; }; 14 | 55042E75221DB3A200E8381A /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 55042E73221DB3A200E8381A /* LaunchScreen.storyboard */; }; 15 | 55042E80221DB3A200E8381A /* TextDetection_CoreMLTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55042E7F221DB3A200E8381A /* TextDetection_CoreMLTests.swift */; }; 16 | 55042E8C221DB4B400E8381A /* VideoCapture.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55042E8A221DB4B400E8381A /* VideoCapture.swift */; }; 17 | 55042E8D221DB4B400E8381A /* Measure.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55042E8B221DB4B400E8381A /* Measure.swift */; }; 18 | 55042E8F221DB56700E8381A /* DrawingView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55042E8E221DB56700E8381A /* DrawingView.swift */; }; 19 | /* End PBXBuildFile section */ 20 | 21 | /* Begin PBXContainerItemProxy section */ 22 | 55042E7C221DB3A200E8381A /* PBXContainerItemProxy */ = { 23 | isa = PBXContainerItemProxy; 24 | containerPortal = 55042E5F221DB3A100E8381A /* Project object */; 25 | proxyType = 1; 26 | remoteGlobalIDString = 55042E66221DB3A100E8381A; 27 | remoteInfo = "TextDetection-CoreML"; 28 | }; 29 | /* End PBXContainerItemProxy section */ 30 | 31 | /* Begin PBXFileReference section */ 32 | 55042E67221DB3A100E8381A /* TextDetection-CoreML.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "TextDetection-CoreML.app"; sourceTree = BUILT_PRODUCTS_DIR; }; 33 | 55042E6A221DB3A100E8381A /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 34 | 55042E6C221DB3A100E8381A /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 35 | 55042E6F221DB3A100E8381A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 36 | 55042E71221DB3A200E8381A /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 37 | 55042E74221DB3A200E8381A /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 38 | 55042E76221DB3A200E8381A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 39 | 55042E7B221DB3A200E8381A /* TextDetection-CoreMLTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = "TextDetection-CoreMLTests.xctest"; sourceTree = BUILT_PRODUCTS_DIR; }; 40 | 55042E7F221DB3A200E8381A /* TextDetection_CoreMLTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextDetection_CoreMLTests.swift; sourceTree = ""; }; 41 | 55042E81221DB3A200E8381A /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 42 | 55042E8A221DB4B400E8381A /* VideoCapture.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VideoCapture.swift; sourceTree = ""; }; 43 | 55042E8B221DB4B400E8381A /* Measure.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Measure.swift; sourceTree = ""; }; 44 | 55042E8E221DB56700E8381A /* DrawingView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DrawingView.swift; sourceTree = ""; }; 45 | /* End PBXFileReference section */ 46 | 47 | /* Begin PBXFrameworksBuildPhase section */ 48 | 55042E64221DB3A100E8381A /* Frameworks */ = { 49 | isa = PBXFrameworksBuildPhase; 50 | buildActionMask = 2147483647; 51 | files = ( 52 | ); 53 | runOnlyForDeploymentPostprocessing = 0; 54 | }; 55 | 55042E78221DB3A200E8381A /* Frameworks */ = { 56 | isa = PBXFrameworksBuildPhase; 57 | buildActionMask = 2147483647; 58 | files = ( 59 | ); 60 | runOnlyForDeploymentPostprocessing = 0; 61 | }; 62 | /* End PBXFrameworksBuildPhase section */ 63 | 64 | /* Begin PBXGroup section */ 65 | 55042E5E221DB3A100E8381A = { 66 | isa = PBXGroup; 67 | children = ( 68 | 55042E69221DB3A100E8381A /* TextDetection-CoreML */, 69 | 55042E7E221DB3A200E8381A /* TextDetection-CoreMLTests */, 70 | 55042E68221DB3A100E8381A /* Products */, 71 | ); 72 | sourceTree = ""; 73 | }; 74 | 55042E68221DB3A100E8381A /* Products */ = { 75 | isa = PBXGroup; 76 | children = ( 77 | 55042E67221DB3A100E8381A /* TextDetection-CoreML.app */, 78 | 55042E7B221DB3A200E8381A /* TextDetection-CoreMLTests.xctest */, 79 | ); 80 | name = Products; 81 | sourceTree = ""; 82 | }; 83 | 55042E69221DB3A100E8381A /* TextDetection-CoreML */ = { 84 | isa = PBXGroup; 85 | children = ( 86 | 55042E6A221DB3A100E8381A /* AppDelegate.swift */, 87 | 55042E6C221DB3A100E8381A /* ViewController.swift */, 88 | 55042E8E221DB56700E8381A /* DrawingView.swift */, 89 | 55042E8B221DB4B400E8381A /* Measure.swift */, 90 | 55042E8A221DB4B400E8381A /* VideoCapture.swift */, 91 | 55042E6E221DB3A100E8381A /* Main.storyboard */, 92 | 55042E71221DB3A200E8381A /* Assets.xcassets */, 93 | 55042E73221DB3A200E8381A /* LaunchScreen.storyboard */, 94 | 55042E76221DB3A200E8381A /* Info.plist */, 95 | ); 96 | path = "TextDetection-CoreML"; 97 | sourceTree = ""; 98 | }; 99 | 55042E7E221DB3A200E8381A /* TextDetection-CoreMLTests */ = { 100 | isa = PBXGroup; 101 | children = ( 102 | 55042E7F221DB3A200E8381A /* TextDetection_CoreMLTests.swift */, 103 | 55042E81221DB3A200E8381A /* Info.plist */, 104 | ); 105 | path = "TextDetection-CoreMLTests"; 106 | sourceTree = ""; 107 | }; 108 | /* End PBXGroup section */ 109 | 110 | /* Begin PBXNativeTarget section */ 111 | 55042E66221DB3A100E8381A /* TextDetection-CoreML */ = { 112 | isa = PBXNativeTarget; 113 | buildConfigurationList = 55042E84221DB3A200E8381A /* Build configuration list for PBXNativeTarget "TextDetection-CoreML" */; 114 | buildPhases = ( 115 | 55042E63221DB3A100E8381A /* Sources */, 116 | 55042E64221DB3A100E8381A /* Frameworks */, 117 | 55042E65221DB3A100E8381A /* Resources */, 118 | ); 119 | buildRules = ( 120 | ); 121 | dependencies = ( 122 | ); 123 | name = "TextDetection-CoreML"; 124 | productName = "TextDetection-CoreML"; 125 | productReference = 55042E67221DB3A100E8381A /* TextDetection-CoreML.app */; 126 | productType = "com.apple.product-type.application"; 127 | }; 128 | 55042E7A221DB3A200E8381A /* TextDetection-CoreMLTests */ = { 129 | isa = PBXNativeTarget; 130 | buildConfigurationList = 55042E87221DB3A200E8381A /* Build configuration list for PBXNativeTarget "TextDetection-CoreMLTests" */; 131 | buildPhases = ( 132 | 55042E77221DB3A200E8381A /* Sources */, 133 | 55042E78221DB3A200E8381A /* Frameworks */, 134 | 55042E79221DB3A200E8381A /* Resources */, 135 | ); 136 | buildRules = ( 137 | ); 138 | dependencies = ( 139 | 55042E7D221DB3A200E8381A /* PBXTargetDependency */, 140 | ); 141 | name = "TextDetection-CoreMLTests"; 142 | productName = "TextDetection-CoreMLTests"; 143 | productReference = 55042E7B221DB3A200E8381A /* TextDetection-CoreMLTests.xctest */; 144 | productType = "com.apple.product-type.bundle.unit-test"; 145 | }; 146 | /* End PBXNativeTarget section */ 147 | 148 | /* Begin PBXProject section */ 149 | 55042E5F221DB3A100E8381A /* Project object */ = { 150 | isa = PBXProject; 151 | attributes = { 152 | LastSwiftUpdateCheck = 1010; 153 | LastUpgradeCheck = 1010; 154 | ORGANIZATIONNAME = tucan9389; 155 | TargetAttributes = { 156 | 55042E66221DB3A100E8381A = { 157 | CreatedOnToolsVersion = 10.1; 158 | }; 159 | 55042E7A221DB3A200E8381A = { 160 | CreatedOnToolsVersion = 10.1; 161 | TestTargetID = 55042E66221DB3A100E8381A; 162 | }; 163 | }; 164 | }; 165 | buildConfigurationList = 55042E62221DB3A100E8381A /* Build configuration list for PBXProject "TextDetection-CoreML" */; 166 | compatibilityVersion = "Xcode 9.3"; 167 | developmentRegion = en; 168 | hasScannedForEncodings = 0; 169 | knownRegions = ( 170 | en, 171 | Base, 172 | ); 173 | mainGroup = 55042E5E221DB3A100E8381A; 174 | productRefGroup = 55042E68221DB3A100E8381A /* Products */; 175 | projectDirPath = ""; 176 | projectRoot = ""; 177 | targets = ( 178 | 55042E66221DB3A100E8381A /* TextDetection-CoreML */, 179 | 55042E7A221DB3A200E8381A /* TextDetection-CoreMLTests */, 180 | ); 181 | }; 182 | /* End PBXProject section */ 183 | 184 | /* Begin PBXResourcesBuildPhase section */ 185 | 55042E65221DB3A100E8381A /* Resources */ = { 186 | isa = PBXResourcesBuildPhase; 187 | buildActionMask = 2147483647; 188 | files = ( 189 | 55042E75221DB3A200E8381A /* LaunchScreen.storyboard in Resources */, 190 | 55042E72221DB3A200E8381A /* Assets.xcassets in Resources */, 191 | 55042E70221DB3A100E8381A /* Main.storyboard in Resources */, 192 | ); 193 | runOnlyForDeploymentPostprocessing = 0; 194 | }; 195 | 55042E79221DB3A200E8381A /* Resources */ = { 196 | isa = PBXResourcesBuildPhase; 197 | buildActionMask = 2147483647; 198 | files = ( 199 | ); 200 | runOnlyForDeploymentPostprocessing = 0; 201 | }; 202 | /* End PBXResourcesBuildPhase section */ 203 | 204 | /* Begin PBXSourcesBuildPhase section */ 205 | 55042E63221DB3A100E8381A /* Sources */ = { 206 | isa = PBXSourcesBuildPhase; 207 | buildActionMask = 2147483647; 208 | files = ( 209 | 55042E6D221DB3A100E8381A /* ViewController.swift in Sources */, 210 | 55042E8C221DB4B400E8381A /* VideoCapture.swift in Sources */, 211 | 55042E8F221DB56700E8381A /* DrawingView.swift in Sources */, 212 | 55042E8D221DB4B400E8381A /* Measure.swift in Sources */, 213 | 55042E6B221DB3A100E8381A /* AppDelegate.swift in Sources */, 214 | ); 215 | runOnlyForDeploymentPostprocessing = 0; 216 | }; 217 | 55042E77221DB3A200E8381A /* Sources */ = { 218 | isa = PBXSourcesBuildPhase; 219 | buildActionMask = 2147483647; 220 | files = ( 221 | 55042E80221DB3A200E8381A /* TextDetection_CoreMLTests.swift in Sources */, 222 | ); 223 | runOnlyForDeploymentPostprocessing = 0; 224 | }; 225 | /* End PBXSourcesBuildPhase section */ 226 | 227 | /* Begin PBXTargetDependency section */ 228 | 55042E7D221DB3A200E8381A /* PBXTargetDependency */ = { 229 | isa = PBXTargetDependency; 230 | target = 55042E66221DB3A100E8381A /* TextDetection-CoreML */; 231 | targetProxy = 55042E7C221DB3A200E8381A /* PBXContainerItemProxy */; 232 | }; 233 | /* End PBXTargetDependency section */ 234 | 235 | /* Begin PBXVariantGroup section */ 236 | 55042E6E221DB3A100E8381A /* Main.storyboard */ = { 237 | isa = PBXVariantGroup; 238 | children = ( 239 | 55042E6F221DB3A100E8381A /* Base */, 240 | ); 241 | name = Main.storyboard; 242 | sourceTree = ""; 243 | }; 244 | 55042E73221DB3A200E8381A /* LaunchScreen.storyboard */ = { 245 | isa = PBXVariantGroup; 246 | children = ( 247 | 55042E74221DB3A200E8381A /* Base */, 248 | ); 249 | name = LaunchScreen.storyboard; 250 | sourceTree = ""; 251 | }; 252 | /* End PBXVariantGroup section */ 253 | 254 | /* Begin XCBuildConfiguration section */ 255 | 55042E82221DB3A200E8381A /* Debug */ = { 256 | isa = XCBuildConfiguration; 257 | buildSettings = { 258 | ALWAYS_SEARCH_USER_PATHS = NO; 259 | CLANG_ANALYZER_NONNULL = YES; 260 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 261 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 262 | CLANG_CXX_LIBRARY = "libc++"; 263 | CLANG_ENABLE_MODULES = YES; 264 | CLANG_ENABLE_OBJC_ARC = YES; 265 | CLANG_ENABLE_OBJC_WEAK = YES; 266 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 267 | CLANG_WARN_BOOL_CONVERSION = YES; 268 | CLANG_WARN_COMMA = YES; 269 | CLANG_WARN_CONSTANT_CONVERSION = YES; 270 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 271 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 272 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 273 | CLANG_WARN_EMPTY_BODY = YES; 274 | CLANG_WARN_ENUM_CONVERSION = YES; 275 | CLANG_WARN_INFINITE_RECURSION = YES; 276 | CLANG_WARN_INT_CONVERSION = YES; 277 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 278 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 279 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 280 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 281 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 282 | CLANG_WARN_STRICT_PROTOTYPES = YES; 283 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 284 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 285 | CLANG_WARN_UNREACHABLE_CODE = YES; 286 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 287 | CODE_SIGN_IDENTITY = "iPhone Developer"; 288 | COPY_PHASE_STRIP = NO; 289 | DEBUG_INFORMATION_FORMAT = dwarf; 290 | ENABLE_STRICT_OBJC_MSGSEND = YES; 291 | ENABLE_TESTABILITY = YES; 292 | GCC_C_LANGUAGE_STANDARD = gnu11; 293 | GCC_DYNAMIC_NO_PIC = NO; 294 | GCC_NO_COMMON_BLOCKS = YES; 295 | GCC_OPTIMIZATION_LEVEL = 0; 296 | GCC_PREPROCESSOR_DEFINITIONS = ( 297 | "DEBUG=1", 298 | "$(inherited)", 299 | ); 300 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 301 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 302 | GCC_WARN_UNDECLARED_SELECTOR = YES; 303 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 304 | GCC_WARN_UNUSED_FUNCTION = YES; 305 | GCC_WARN_UNUSED_VARIABLE = YES; 306 | IPHONEOS_DEPLOYMENT_TARGET = 12.1; 307 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 308 | MTL_FAST_MATH = YES; 309 | ONLY_ACTIVE_ARCH = YES; 310 | SDKROOT = iphoneos; 311 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 312 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 313 | }; 314 | name = Debug; 315 | }; 316 | 55042E83221DB3A200E8381A /* Release */ = { 317 | isa = XCBuildConfiguration; 318 | buildSettings = { 319 | ALWAYS_SEARCH_USER_PATHS = NO; 320 | CLANG_ANALYZER_NONNULL = YES; 321 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 322 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 323 | CLANG_CXX_LIBRARY = "libc++"; 324 | CLANG_ENABLE_MODULES = YES; 325 | CLANG_ENABLE_OBJC_ARC = YES; 326 | CLANG_ENABLE_OBJC_WEAK = YES; 327 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 328 | CLANG_WARN_BOOL_CONVERSION = YES; 329 | CLANG_WARN_COMMA = YES; 330 | CLANG_WARN_CONSTANT_CONVERSION = YES; 331 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 332 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 333 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 334 | CLANG_WARN_EMPTY_BODY = YES; 335 | CLANG_WARN_ENUM_CONVERSION = YES; 336 | CLANG_WARN_INFINITE_RECURSION = YES; 337 | CLANG_WARN_INT_CONVERSION = YES; 338 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 339 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 340 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 341 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 342 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 343 | CLANG_WARN_STRICT_PROTOTYPES = YES; 344 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 345 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 346 | CLANG_WARN_UNREACHABLE_CODE = YES; 347 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 348 | CODE_SIGN_IDENTITY = "iPhone Developer"; 349 | COPY_PHASE_STRIP = NO; 350 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 351 | ENABLE_NS_ASSERTIONS = NO; 352 | ENABLE_STRICT_OBJC_MSGSEND = YES; 353 | GCC_C_LANGUAGE_STANDARD = gnu11; 354 | GCC_NO_COMMON_BLOCKS = YES; 355 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 356 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 357 | GCC_WARN_UNDECLARED_SELECTOR = YES; 358 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 359 | GCC_WARN_UNUSED_FUNCTION = YES; 360 | GCC_WARN_UNUSED_VARIABLE = YES; 361 | IPHONEOS_DEPLOYMENT_TARGET = 12.1; 362 | MTL_ENABLE_DEBUG_INFO = NO; 363 | MTL_FAST_MATH = YES; 364 | SDKROOT = iphoneos; 365 | SWIFT_COMPILATION_MODE = wholemodule; 366 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 367 | VALIDATE_PRODUCT = YES; 368 | }; 369 | name = Release; 370 | }; 371 | 55042E85221DB3A200E8381A /* Debug */ = { 372 | isa = XCBuildConfiguration; 373 | buildSettings = { 374 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 375 | CODE_SIGN_STYLE = Automatic; 376 | DEVELOPMENT_TEAM = 5WXJ4Z4H69; 377 | INFOPLIST_FILE = "TextDetection-CoreML/Info.plist"; 378 | LD_RUNPATH_SEARCH_PATHS = ( 379 | "$(inherited)", 380 | "@executable_path/Frameworks", 381 | ); 382 | PRODUCT_BUNDLE_IDENTIFIER = "com.tucan9389.TextDetection-CoreML"; 383 | PRODUCT_NAME = "$(TARGET_NAME)"; 384 | SWIFT_VERSION = 4.2; 385 | TARGETED_DEVICE_FAMILY = "1,2"; 386 | }; 387 | name = Debug; 388 | }; 389 | 55042E86221DB3A200E8381A /* Release */ = { 390 | isa = XCBuildConfiguration; 391 | buildSettings = { 392 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 393 | CODE_SIGN_STYLE = Automatic; 394 | DEVELOPMENT_TEAM = 5WXJ4Z4H69; 395 | INFOPLIST_FILE = "TextDetection-CoreML/Info.plist"; 396 | LD_RUNPATH_SEARCH_PATHS = ( 397 | "$(inherited)", 398 | "@executable_path/Frameworks", 399 | ); 400 | PRODUCT_BUNDLE_IDENTIFIER = "com.tucan9389.TextDetection-CoreML"; 401 | PRODUCT_NAME = "$(TARGET_NAME)"; 402 | SWIFT_VERSION = 4.2; 403 | TARGETED_DEVICE_FAMILY = "1,2"; 404 | }; 405 | name = Release; 406 | }; 407 | 55042E88221DB3A200E8381A /* Debug */ = { 408 | isa = XCBuildConfiguration; 409 | buildSettings = { 410 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 411 | BUNDLE_LOADER = "$(TEST_HOST)"; 412 | CODE_SIGN_STYLE = Automatic; 413 | DEVELOPMENT_TEAM = 5WXJ4Z4H69; 414 | INFOPLIST_FILE = "TextDetection-CoreMLTests/Info.plist"; 415 | LD_RUNPATH_SEARCH_PATHS = ( 416 | "$(inherited)", 417 | "@executable_path/Frameworks", 418 | "@loader_path/Frameworks", 419 | ); 420 | PRODUCT_BUNDLE_IDENTIFIER = "com.tucan9389.TextDetection-CoreMLTests"; 421 | PRODUCT_NAME = "$(TARGET_NAME)"; 422 | SWIFT_VERSION = 4.2; 423 | TARGETED_DEVICE_FAMILY = "1,2"; 424 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/TextDetection-CoreML.app/TextDetection-CoreML"; 425 | }; 426 | name = Debug; 427 | }; 428 | 55042E89221DB3A200E8381A /* Release */ = { 429 | isa = XCBuildConfiguration; 430 | buildSettings = { 431 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 432 | BUNDLE_LOADER = "$(TEST_HOST)"; 433 | CODE_SIGN_STYLE = Automatic; 434 | DEVELOPMENT_TEAM = 5WXJ4Z4H69; 435 | INFOPLIST_FILE = "TextDetection-CoreMLTests/Info.plist"; 436 | LD_RUNPATH_SEARCH_PATHS = ( 437 | "$(inherited)", 438 | "@executable_path/Frameworks", 439 | "@loader_path/Frameworks", 440 | ); 441 | PRODUCT_BUNDLE_IDENTIFIER = "com.tucan9389.TextDetection-CoreMLTests"; 442 | PRODUCT_NAME = "$(TARGET_NAME)"; 443 | SWIFT_VERSION = 4.2; 444 | TARGETED_DEVICE_FAMILY = "1,2"; 445 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/TextDetection-CoreML.app/TextDetection-CoreML"; 446 | }; 447 | name = Release; 448 | }; 449 | /* End XCBuildConfiguration section */ 450 | 451 | /* Begin XCConfigurationList section */ 452 | 55042E62221DB3A100E8381A /* Build configuration list for PBXProject "TextDetection-CoreML" */ = { 453 | isa = XCConfigurationList; 454 | buildConfigurations = ( 455 | 55042E82221DB3A200E8381A /* Debug */, 456 | 55042E83221DB3A200E8381A /* Release */, 457 | ); 458 | defaultConfigurationIsVisible = 0; 459 | defaultConfigurationName = Release; 460 | }; 461 | 55042E84221DB3A200E8381A /* Build configuration list for PBXNativeTarget "TextDetection-CoreML" */ = { 462 | isa = XCConfigurationList; 463 | buildConfigurations = ( 464 | 55042E85221DB3A200E8381A /* Debug */, 465 | 55042E86221DB3A200E8381A /* Release */, 466 | ); 467 | defaultConfigurationIsVisible = 0; 468 | defaultConfigurationName = Release; 469 | }; 470 | 55042E87221DB3A200E8381A /* Build configuration list for PBXNativeTarget "TextDetection-CoreMLTests" */ = { 471 | isa = XCConfigurationList; 472 | buildConfigurations = ( 473 | 55042E88221DB3A200E8381A /* Debug */, 474 | 55042E89221DB3A200E8381A /* Release */, 475 | ); 476 | defaultConfigurationIsVisible = 0; 477 | defaultConfigurationName = Release; 478 | }; 479 | /* End XCConfigurationList section */ 480 | }; 481 | rootObject = 55042E5F221DB3A100E8381A /* Project object */; 482 | } 483 | -------------------------------------------------------------------------------- /TextDetection-CoreML.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /TextDetection-CoreML.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /TextDetection-CoreML/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // TextDetection-CoreML 4 | // 5 | // Created by GwakDoyoung on 21/02/2019. 6 | // Copyright © 2019 tucan9389. 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: [UIApplication.LaunchOptionsKey: 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 | -------------------------------------------------------------------------------- /TextDetection-CoreML/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 | } -------------------------------------------------------------------------------- /TextDetection-CoreML/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /TextDetection-CoreML/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 | -------------------------------------------------------------------------------- /TextDetection-CoreML/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 | 45 | 55 | 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 | -------------------------------------------------------------------------------- /TextDetection-CoreML/DrawingView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // DrawingView.swift 3 | // TextDetection-CoreML 4 | // 5 | // Created by GwakDoyoung on 21/02/2019. 6 | // Copyright © 2019 tucan9389. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import Vision 11 | 12 | class DrawingView: UIView { 13 | 14 | 15 | public var regions: [VNTextObservation?]? { 16 | didSet { 17 | setNeedsDisplay() 18 | } 19 | } 20 | 21 | override func draw(_ rect: CGRect) { 22 | guard let ctx = UIGraphicsGetCurrentContext() else { return } 23 | ctx.clear(rect); 24 | guard let regions = regions else { return } 25 | 26 | let frameSize = self.bounds.size 27 | 28 | for region in regions { 29 | if let boxes = region?.characterBoxes { 30 | for box in boxes { 31 | let points = [ 32 | box.topLeft, 33 | box.topRight, 34 | box.bottomRight, 35 | box.bottomLeft 36 | ].map({ CGPoint(x: $0.x, y: 1-$0.y)*frameSize }) 37 | drawPolygon(ctx: ctx, points: points, 38 | color: UIColor(red: 1, green: 0, blue: 0, alpha: 0.6).cgColor) 39 | } 40 | var points = (boxes.compactMap{ [$0.topLeft, $0.topRight] } + boxes.reversed().compactMap{ [$0.bottomRight, $0.bottomLeft] }).reduce([], +) 41 | points = points.map({ CGPoint(x: $0.x, y: 1-$0.y)*frameSize }) 42 | drawPolygon(ctx: ctx, points: points, 43 | color: UIColor(red: 0, green: 1, blue: 0, alpha: 0.3).cgColor, 44 | fill: true) 45 | } 46 | } 47 | } 48 | 49 | private func drawLine(ctx: CGContext, from p1: CGPoint, to p2: CGPoint, color: CGColor) { 50 | ctx.setStrokeColor(color) 51 | ctx.setLineWidth(1.0) 52 | 53 | ctx.move(to: p1) 54 | ctx.addLine(to: p2) 55 | 56 | ctx.strokePath(); 57 | } 58 | 59 | private func drawPolygon(ctx: CGContext, points: [CGPoint], color: CGColor, fill: Bool = false) { 60 | if fill { 61 | ctx.setStrokeColor(UIColor.clear.cgColor) 62 | ctx.setFillColor(color) 63 | ctx.setLineWidth(0.0) 64 | } else { 65 | ctx.setStrokeColor(color) 66 | ctx.setLineWidth(1.0) 67 | } 68 | 69 | 70 | for i in 0.. CGPoint { 90 | return CGPoint(x: left.x * right.x, y: left.y * right.y) 91 | } 92 | 93 | func * (left: CGPoint, right: CGSize) -> CGPoint { 94 | return CGPoint(x: left.x * right.width, y: left.y * right.height) 95 | } 96 | -------------------------------------------------------------------------------- /TextDetection-CoreML/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 | for inference 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 | -------------------------------------------------------------------------------- /TextDetection-CoreML/Measure.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Measure.swift 3 | // TurtleApp-CoreML 4 | // 5 | // Created by GwakDoyoung on 03/07/2018. 6 | // Copyright © 2018 GwakDoyoung. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | 11 | protocol 📏Delegate { 12 | func updateMeasure(inferenceTime: Double, executionTime: Double, fps: Int) 13 | } 14 | // Performance Measurement 15 | class 📏 { 16 | 17 | var delegate: 📏Delegate? 18 | 19 | var index: Int = -1 20 | var measurements: [Dictionary] 21 | 22 | init() { 23 | let measurement = [ 24 | "start": CACurrentMediaTime(), 25 | "end": CACurrentMediaTime() 26 | ] 27 | measurements = Array>(repeating: measurement, count: 30) 28 | } 29 | 30 | // start 31 | func 🎬👏() { 32 | index += 1 33 | index %= 30 34 | measurements[index] = [:] 35 | 36 | 🏷(for: index, with: "start") 37 | } 38 | 39 | // stop 40 | func 🎬🤚() { 41 | 🏷(for: index, with: "end") 42 | 43 | let beforeMeasurement = getBeforeMeasurment(for: index) 44 | let currentMeasurement = measurements[index] 45 | if let startTime = currentMeasurement["start"], 46 | let endInferenceTime = currentMeasurement["endInference"], 47 | let endTime = currentMeasurement["end"], 48 | let beforeStartTime = beforeMeasurement["start"] { 49 | delegate?.updateMeasure(inferenceTime: endInferenceTime - startTime, 50 | executionTime: endTime - startTime, 51 | fps: Int(1/(startTime - beforeStartTime))) 52 | } 53 | 54 | } 55 | 56 | // labeling with 57 | func 🏷(with msg: String? = "") { 58 | 🏷(for: index, with: msg) 59 | } 60 | 61 | private func 🏷(for index: Int, with msg: String? = "") { 62 | if let message = msg { 63 | measurements[index][message] = CACurrentMediaTime() 64 | } 65 | } 66 | 67 | private func getBeforeMeasurment(for index: Int) -> Dictionary { 68 | return measurements[(index + 30 - 1) % 30] 69 | } 70 | 71 | // log 72 | func 🖨() { 73 | 74 | } 75 | } 76 | 77 | class MeasureLogView: UIView { 78 | let etimeLabel = UILabel(frame: .zero) 79 | let fpsLabel = UILabel(frame: .zero) 80 | 81 | 82 | required init?(coder aDecoder: NSCoder) { 83 | fatalError("init(coder:) has not been implemented") 84 | } 85 | 86 | } 87 | -------------------------------------------------------------------------------- /TextDetection-CoreML/VideoCapture.swift: -------------------------------------------------------------------------------- 1 | // 2 | // VideoCapture.swift 3 | // Awesome ML 4 | // 5 | // Created by Eugene Bokhan on 3/13/18. 6 | // Copyright © 2018 Eugene Bokhan. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import AVFoundation 11 | import CoreVideo 12 | 13 | public protocol VideoCaptureDelegate: class { 14 | func videoCapture(_ capture: VideoCapture, didCaptureVideoFrame: CVPixelBuffer?, timestamp: CMTime) 15 | } 16 | 17 | public class VideoCapture: NSObject { 18 | public var previewLayer: AVCaptureVideoPreviewLayer? 19 | public weak var delegate: VideoCaptureDelegate? 20 | public var fps = 15 21 | 22 | let captureSession = AVCaptureSession() 23 | let videoOutput = AVCaptureVideoDataOutput() 24 | let queue = DispatchQueue(label: "com.tucan9389.camera-queue") 25 | 26 | var lastTimestamp = CMTime() 27 | 28 | public func setUp(sessionPreset: AVCaptureSession.Preset = .vga640x480, 29 | completion: @escaping (Bool) -> Void) { 30 | self.setUpCamera(sessionPreset: sessionPreset, completion: { success in 31 | completion(success) 32 | }) 33 | } 34 | 35 | func setUpCamera(sessionPreset: AVCaptureSession.Preset, completion: @escaping (_ success: Bool) -> Void) { 36 | 37 | captureSession.beginConfiguration() 38 | captureSession.sessionPreset = sessionPreset 39 | 40 | guard let captureDevice = AVCaptureDevice.default(.builtInWideAngleCamera, 41 | for: .video, 42 | position: .back) else { 43 | 44 | print("Error: no video devices available") 45 | return 46 | } 47 | 48 | guard let videoInput = try? AVCaptureDeviceInput(device: captureDevice) else { 49 | print("Error: could not create AVCaptureDeviceInput") 50 | return 51 | } 52 | 53 | if captureSession.canAddInput(videoInput) { 54 | captureSession.addInput(videoInput) 55 | } 56 | 57 | let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession) 58 | previewLayer.videoGravity = AVLayerVideoGravity.resizeAspect 59 | previewLayer.connection?.videoOrientation = .portrait 60 | self.previewLayer = previewLayer 61 | 62 | let settings: [String : Any] = [ 63 | kCVPixelBufferPixelFormatTypeKey as String: NSNumber(value: kCVPixelFormatType_32BGRA), 64 | ] 65 | 66 | videoOutput.videoSettings = settings 67 | videoOutput.alwaysDiscardsLateVideoFrames = true 68 | videoOutput.setSampleBufferDelegate(self, queue: queue) 69 | if captureSession.canAddOutput(videoOutput) { 70 | captureSession.addOutput(videoOutput) 71 | } 72 | 73 | // We want the buffers to be in portrait orientation otherwise they are 74 | // rotated by 90 degrees. Need to set this _after_ addOutput()! 75 | videoOutput.connection(with: AVMediaType.video)?.videoOrientation = .portrait 76 | 77 | captureSession.commitConfiguration() 78 | 79 | let success = true 80 | completion(success) 81 | } 82 | 83 | public func start() { 84 | if !captureSession.isRunning { 85 | captureSession.startRunning() 86 | } 87 | } 88 | 89 | public func stop() { 90 | if captureSession.isRunning { 91 | captureSession.stopRunning() 92 | } 93 | } 94 | } 95 | 96 | extension VideoCapture: AVCaptureVideoDataOutputSampleBufferDelegate { 97 | public func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) { 98 | // Because lowering the capture device's FPS looks ugly in the preview, 99 | // we capture at full speed but only call the delegate at its desired 100 | // framerate. 101 | let timestamp = CMSampleBufferGetPresentationTimeStamp(sampleBuffer) 102 | let deltaTime = timestamp - lastTimestamp 103 | if deltaTime >= CMTimeMake(value: 1, timescale: Int32(fps)) { 104 | lastTimestamp = timestamp 105 | let imageBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) 106 | delegate?.videoCapture(self, didCaptureVideoFrame: imageBuffer, timestamp: timestamp) 107 | } 108 | } 109 | 110 | public func captureOutput(_ output: AVCaptureOutput, didDrop sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) { 111 | //print("dropped frame") 112 | } 113 | } 114 | 115 | -------------------------------------------------------------------------------- /TextDetection-CoreML/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // TextDetection-CoreML 4 | // 5 | // Created by GwakDoyoung on 21/02/2019. 6 | // Copyright © 2019 tucan9389. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import Vision 11 | import CoreMedia 12 | 13 | class ViewController: UIViewController { 14 | 15 | // MARK: - UI Properties 16 | @IBOutlet weak var videoPreview: UIView! 17 | @IBOutlet weak var drawingView: DrawingView! 18 | 19 | @IBOutlet weak var inferenceLabel: UILabel! 20 | @IBOutlet weak var etimeLabel: UILabel! 21 | @IBOutlet weak var fpsLabel: UILabel! 22 | 23 | // MARK: - Vision 프로퍼티 24 | var request: VNDetectTextRectanglesRequest? 25 | 26 | // MARK - Performance Measurement Property 27 | private let 👨‍🔧 = 📏() 28 | 29 | // MARK: - AV Property 30 | var videoCapture: VideoCapture! 31 | 32 | override func viewDidLoad() { 33 | super.viewDidLoad() 34 | 35 | // setup the model 36 | setUpModel() 37 | 38 | // setup camera 39 | setUpCamera() 40 | 41 | // setup delegate for performance measurement 42 | 👨‍🔧.delegate = self 43 | } 44 | 45 | override func didReceiveMemoryWarning() { 46 | super.didReceiveMemoryWarning() 47 | } 48 | 49 | override func viewWillAppear(_ animated: Bool) { 50 | super.viewWillAppear(animated) 51 | self.videoCapture.start() 52 | } 53 | 54 | override func viewWillDisappear(_ animated: Bool) { 55 | super.viewWillDisappear(animated) 56 | self.videoCapture.stop() 57 | } 58 | 59 | // MARK: - Setup Core ML 60 | func setUpModel() { 61 | let request = VNDetectTextRectanglesRequest(completionHandler: self.visionRequestDidComplete) 62 | request.reportCharacterBoxes = true 63 | self.request = request 64 | } 65 | 66 | // MARK: - SetUp Video 67 | func setUpCamera() { 68 | videoCapture = VideoCapture() 69 | videoCapture.delegate = self 70 | videoCapture.setUp(sessionPreset: .vga640x480) { success in 71 | 72 | if success { 73 | // add preview view on the layer 74 | if let previewLayer = self.videoCapture.previewLayer { 75 | self.videoPreview.layer.addSublayer(previewLayer) 76 | self.resizePreviewLayer() 77 | } 78 | 79 | // start video preview when setup is done 80 | self.videoCapture.start() 81 | } 82 | } 83 | } 84 | 85 | override func viewDidLayoutSubviews() { 86 | super.viewDidLayoutSubviews() 87 | resizePreviewLayer() 88 | } 89 | 90 | func resizePreviewLayer() { 91 | videoCapture.previewLayer?.frame = videoPreview.bounds 92 | } 93 | } 94 | 95 | // MARK: - VideoCaptureDelegate 96 | extension ViewController: VideoCaptureDelegate { 97 | func videoCapture(_ capture: VideoCapture, didCaptureVideoFrame pixelBuffer: CVPixelBuffer?, timestamp: CMTime) { 98 | // the captured image from camera is contained on pixelBuffer 99 | if let pixelBuffer = pixelBuffer { 100 | // start of measure 101 | self.👨‍🔧.🎬👏() 102 | 103 | // predict! 104 | self.predictUsingVision(pixelBuffer: pixelBuffer) 105 | } 106 | } 107 | } 108 | 109 | extension ViewController { 110 | func predictUsingVision(pixelBuffer: CVPixelBuffer) { 111 | // Vision이 입력이미지를 자동으로 크기조정을 해줄 것임. 112 | let handler = VNImageRequestHandler(cvPixelBuffer: pixelBuffer) 113 | if let request = request { 114 | try? handler.perform([request]) 115 | } 116 | } 117 | 118 | func visionRequestDidComplete(request: VNRequest, error: Error?) { 119 | self.👨‍🔧.🏷(with: "endInference") 120 | guard let observations = request.results else { 121 | // end of measure 122 | self.👨‍🔧.🎬🤚() 123 | return 124 | } 125 | 126 | DispatchQueue.main.async { 127 | let regions: [VNTextObservation?] = observations.map({$0 as? VNTextObservation}) 128 | 129 | self.drawingView.regions = regions 130 | 131 | // end of measure 132 | self.👨‍🔧.🎬🤚() 133 | } 134 | } 135 | } 136 | 137 | // MARK: - 📏(Performance Measurement) Delegate 138 | extension ViewController: 📏Delegate { 139 | func updateMeasure(inferenceTime: Double, executionTime: Double, fps: Int) { 140 | //print(executionTime, fps) 141 | self.inferenceLabel.text = "inference: \(Int(inferenceTime*1000.0)) mm" 142 | self.etimeLabel.text = "execution: \(Int(executionTime*1000.0)) mm" 143 | self.fpsLabel.text = "fps: \(fps)" 144 | } 145 | } 146 | -------------------------------------------------------------------------------- /TextDetection-CoreMLTests/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 | BNDL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | 22 | 23 | -------------------------------------------------------------------------------- /TextDetection-CoreMLTests/TextDetection_CoreMLTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // TextDetection_CoreMLTests.swift 3 | // TextDetection-CoreMLTests 4 | // 5 | // Created by GwakDoyoung on 21/02/2019. 6 | // Copyright © 2019 tucan9389. All rights reserved. 7 | // 8 | 9 | import XCTest 10 | @testable import TextDetection_CoreML 11 | 12 | class TextDetection_CoreMLTests: XCTestCase { 13 | 14 | override func setUp() { 15 | // Put setup code here. This method is called before the invocation of each test method in the class. 16 | } 17 | 18 | override func tearDown() { 19 | // Put teardown code here. This method is called after the invocation of each test method in the class. 20 | } 21 | 22 | func testExample() { 23 | // This is an example of a functional test case. 24 | // Use XCTAssert and related functions to verify your tests produce the correct results. 25 | } 26 | 27 | func testPerformanceExample() { 28 | // This is an example of a performance test case. 29 | self.measure { 30 | // Put the code you want to measure the time of here. 31 | } 32 | } 33 | 34 | } 35 | -------------------------------------------------------------------------------- /resource/TextDetection-CoreML_DEMO001.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tucan9389/TextDetection-CoreML/5f9dffb0d820b7cd798958f8be49f69d6db9058f/resource/TextDetection-CoreML_DEMO001.gif -------------------------------------------------------------------------------- /resource/prerequest_001_plist.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tucan9389/TextDetection-CoreML/5f9dffb0d820b7cd798958f8be49f69d6db9058f/resource/prerequest_001_plist.png --------------------------------------------------------------------------------