├── README.md ├── TrafficLight.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ └── contents.xcworkspacedata └── xcuserdata │ ├── kiwi.xcuserdatad │ ├── xcdebugger │ │ └── Breakpoints_v2.xcbkptlist │ └── xcschemes │ │ └── xcschememanagement.plist │ └── kyx.xcuserdatad │ ├── xcdebugger │ └── Breakpoints_v2.xcbkptlist │ └── xcschemes │ ├── TrafficLight.xcscheme │ └── xcschememanagement.plist └── TrafficLight ├── AppDelegate.swift ├── Assets.xcassets ├── AppIcon.appiconset │ └── Contents.json ├── Contents.json ├── go.imageset │ ├── Contents.json │ └── go@2x.png ├── nothing.imageset │ ├── Contents.json │ └── nothing@2x.png └── stop.imageset │ ├── Contents.json │ └── stop@2x.png ├── Base.lproj ├── LaunchScreen.storyboard └── Main.storyboard ├── Info.plist └── ViewController.swift /README.md: -------------------------------------------------------------------------------- 1 | # TrafficLight 2 | A sample project use Apple CoreML & Vision to detect traffic lights 3 | 4 | # How to use 5 | This project doesn't contain a ML model, that means you can't just download and run, you need to find a model or train one by yourself. 6 | The One I used for testing is from davidbrai's project [Recognizing Traffic Lights with Deep Learning] (https://github.com/davidbrai/deep-learning-traffic-lights) 7 | David's Model works great, it's the my first recommendation if you wanna try this code. 8 | 9 | You can use Apple's coremltools to convert it to a MLModel, set the input/output interface you want, 10 | and integrate your model with my code. 11 | 12 | # About 13 | TrafficLight Project provide full support for video capture, AVCapture arguments setting, and CoreML, Vision Integration, 14 | it's more about a sample or a toy,dont try it in realworld driving. 15 | -------------------------------------------------------------------------------- /TrafficLight.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 48; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 551A04A01EF4794900F30BA2 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 551A049F1EF4794900F30BA2 /* AppDelegate.swift */; }; 11 | 551A04A21EF4794900F30BA2 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 551A04A11EF4794900F30BA2 /* ViewController.swift */; }; 12 | 551A04A51EF4794900F30BA2 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 551A04A31EF4794900F30BA2 /* Main.storyboard */; }; 13 | 551A04A71EF4794900F30BA2 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 551A04A61EF4794900F30BA2 /* Assets.xcassets */; }; 14 | 551A04AA1EF4794900F30BA2 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 551A04A81EF4794900F30BA2 /* LaunchScreen.storyboard */; }; 15 | /* End PBXBuildFile section */ 16 | 17 | /* Begin PBXFileReference section */ 18 | 551A049C1EF4794900F30BA2 /* TrafficLight.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = TrafficLight.app; sourceTree = BUILT_PRODUCTS_DIR; }; 19 | 551A049F1EF4794900F30BA2 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 20 | 551A04A11EF4794900F30BA2 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 21 | 551A04A41EF4794900F30BA2 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 22 | 551A04A61EF4794900F30BA2 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 23 | 551A04A91EF4794900F30BA2 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 24 | 551A04AB1EF4794900F30BA2 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 25 | /* End PBXFileReference section */ 26 | 27 | /* Begin PBXFrameworksBuildPhase section */ 28 | 551A04991EF4794900F30BA2 /* Frameworks */ = { 29 | isa = PBXFrameworksBuildPhase; 30 | buildActionMask = 2147483647; 31 | files = ( 32 | ); 33 | runOnlyForDeploymentPostprocessing = 0; 34 | }; 35 | /* End PBXFrameworksBuildPhase section */ 36 | 37 | /* Begin PBXGroup section */ 38 | 551A04931EF4794900F30BA2 = { 39 | isa = PBXGroup; 40 | children = ( 41 | 551A049E1EF4794900F30BA2 /* TrafficLight */, 42 | 551A049D1EF4794900F30BA2 /* Products */, 43 | ); 44 | sourceTree = ""; 45 | }; 46 | 551A049D1EF4794900F30BA2 /* Products */ = { 47 | isa = PBXGroup; 48 | children = ( 49 | 551A049C1EF4794900F30BA2 /* TrafficLight.app */, 50 | ); 51 | name = Products; 52 | sourceTree = ""; 53 | }; 54 | 551A049E1EF4794900F30BA2 /* TrafficLight */ = { 55 | isa = PBXGroup; 56 | children = ( 57 | 551A049F1EF4794900F30BA2 /* AppDelegate.swift */, 58 | 551A04A11EF4794900F30BA2 /* ViewController.swift */, 59 | 551A04A31EF4794900F30BA2 /* Main.storyboard */, 60 | 551A04A61EF4794900F30BA2 /* Assets.xcassets */, 61 | 551A04A81EF4794900F30BA2 /* LaunchScreen.storyboard */, 62 | 551A04AB1EF4794900F30BA2 /* Info.plist */, 63 | ); 64 | path = TrafficLight; 65 | sourceTree = ""; 66 | }; 67 | /* End PBXGroup section */ 68 | 69 | /* Begin PBXNativeTarget section */ 70 | 551A049B1EF4794900F30BA2 /* TrafficLight */ = { 71 | isa = PBXNativeTarget; 72 | buildConfigurationList = 551A04AE1EF4794900F30BA2 /* Build configuration list for PBXNativeTarget "TrafficLight" */; 73 | buildPhases = ( 74 | 551A04981EF4794900F30BA2 /* Sources */, 75 | 551A04991EF4794900F30BA2 /* Frameworks */, 76 | 551A049A1EF4794900F30BA2 /* Resources */, 77 | ); 78 | buildRules = ( 79 | ); 80 | dependencies = ( 81 | ); 82 | name = TrafficLight; 83 | productName = TrafficLight; 84 | productReference = 551A049C1EF4794900F30BA2 /* TrafficLight.app */; 85 | productType = "com.apple.product-type.application"; 86 | }; 87 | /* End PBXNativeTarget section */ 88 | 89 | /* Begin PBXProject section */ 90 | 551A04941EF4794900F30BA2 /* Project object */ = { 91 | isa = PBXProject; 92 | attributes = { 93 | LastSwiftUpdateCheck = 0900; 94 | LastUpgradeCheck = 0900; 95 | ORGANIZATIONNAME = DIFF; 96 | TargetAttributes = { 97 | 551A049B1EF4794900F30BA2 = { 98 | CreatedOnToolsVersion = 9.0; 99 | LastSwiftMigration = 0900; 100 | }; 101 | }; 102 | }; 103 | buildConfigurationList = 551A04971EF4794900F30BA2 /* Build configuration list for PBXProject "TrafficLight" */; 104 | compatibilityVersion = "Xcode 8.0"; 105 | developmentRegion = en; 106 | hasScannedForEncodings = 0; 107 | knownRegions = ( 108 | en, 109 | Base, 110 | ); 111 | mainGroup = 551A04931EF4794900F30BA2; 112 | productRefGroup = 551A049D1EF4794900F30BA2 /* Products */; 113 | projectDirPath = ""; 114 | projectRoot = ""; 115 | targets = ( 116 | 551A049B1EF4794900F30BA2 /* TrafficLight */, 117 | ); 118 | }; 119 | /* End PBXProject section */ 120 | 121 | /* Begin PBXResourcesBuildPhase section */ 122 | 551A049A1EF4794900F30BA2 /* Resources */ = { 123 | isa = PBXResourcesBuildPhase; 124 | buildActionMask = 2147483647; 125 | files = ( 126 | 551A04AA1EF4794900F30BA2 /* LaunchScreen.storyboard in Resources */, 127 | 551A04A71EF4794900F30BA2 /* Assets.xcassets in Resources */, 128 | 551A04A51EF4794900F30BA2 /* Main.storyboard in Resources */, 129 | ); 130 | runOnlyForDeploymentPostprocessing = 0; 131 | }; 132 | /* End PBXResourcesBuildPhase section */ 133 | 134 | /* Begin PBXSourcesBuildPhase section */ 135 | 551A04981EF4794900F30BA2 /* Sources */ = { 136 | isa = PBXSourcesBuildPhase; 137 | buildActionMask = 2147483647; 138 | files = ( 139 | 551A04A21EF4794900F30BA2 /* ViewController.swift in Sources */, 140 | 551A04A01EF4794900F30BA2 /* AppDelegate.swift in Sources */, 141 | ); 142 | runOnlyForDeploymentPostprocessing = 0; 143 | }; 144 | /* End PBXSourcesBuildPhase section */ 145 | 146 | /* Begin PBXVariantGroup section */ 147 | 551A04A31EF4794900F30BA2 /* Main.storyboard */ = { 148 | isa = PBXVariantGroup; 149 | children = ( 150 | 551A04A41EF4794900F30BA2 /* Base */, 151 | ); 152 | name = Main.storyboard; 153 | sourceTree = ""; 154 | }; 155 | 551A04A81EF4794900F30BA2 /* LaunchScreen.storyboard */ = { 156 | isa = PBXVariantGroup; 157 | children = ( 158 | 551A04A91EF4794900F30BA2 /* Base */, 159 | ); 160 | name = LaunchScreen.storyboard; 161 | sourceTree = ""; 162 | }; 163 | /* End PBXVariantGroup section */ 164 | 165 | /* Begin XCBuildConfiguration section */ 166 | 551A04AC1EF4794900F30BA2 /* Debug */ = { 167 | isa = XCBuildConfiguration; 168 | buildSettings = { 169 | ALWAYS_SEARCH_USER_PATHS = NO; 170 | CLANG_ANALYZER_NONNULL = YES; 171 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 172 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 173 | CLANG_CXX_LIBRARY = "libc++"; 174 | CLANG_ENABLE_MODULES = YES; 175 | CLANG_ENABLE_OBJC_ARC = YES; 176 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 177 | CLANG_WARN_BOOL_CONVERSION = YES; 178 | CLANG_WARN_COMMA = YES; 179 | CLANG_WARN_CONSTANT_CONVERSION = YES; 180 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 181 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 182 | CLANG_WARN_EMPTY_BODY = YES; 183 | CLANG_WARN_ENUM_CONVERSION = YES; 184 | CLANG_WARN_INFINITE_RECURSION = YES; 185 | CLANG_WARN_INT_CONVERSION = YES; 186 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 187 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 188 | CLANG_WARN_STRICT_PROTOTYPES = YES; 189 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 190 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 191 | CLANG_WARN_UNREACHABLE_CODE = YES; 192 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 193 | CODE_SIGN_IDENTITY = "iPhone Developer"; 194 | COPY_PHASE_STRIP = NO; 195 | DEBUG_INFORMATION_FORMAT = dwarf; 196 | ENABLE_STRICT_OBJC_MSGSEND = YES; 197 | ENABLE_TESTABILITY = YES; 198 | GCC_C_LANGUAGE_STANDARD = gnu11; 199 | GCC_DYNAMIC_NO_PIC = NO; 200 | GCC_NO_COMMON_BLOCKS = YES; 201 | GCC_OPTIMIZATION_LEVEL = 0; 202 | GCC_PREPROCESSOR_DEFINITIONS = ( 203 | "DEBUG=1", 204 | "$(inherited)", 205 | ); 206 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 207 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 208 | GCC_WARN_UNDECLARED_SELECTOR = YES; 209 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 210 | GCC_WARN_UNUSED_FUNCTION = YES; 211 | GCC_WARN_UNUSED_VARIABLE = YES; 212 | IPHONEOS_DEPLOYMENT_TARGET = 11.0; 213 | MTL_ENABLE_DEBUG_INFO = YES; 214 | ONLY_ACTIVE_ARCH = YES; 215 | SDKROOT = iphoneos; 216 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 217 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 218 | }; 219 | name = Debug; 220 | }; 221 | 551A04AD1EF4794900F30BA2 /* Release */ = { 222 | isa = XCBuildConfiguration; 223 | buildSettings = { 224 | ALWAYS_SEARCH_USER_PATHS = NO; 225 | CLANG_ANALYZER_NONNULL = YES; 226 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 227 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 228 | CLANG_CXX_LIBRARY = "libc++"; 229 | CLANG_ENABLE_MODULES = YES; 230 | CLANG_ENABLE_OBJC_ARC = YES; 231 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 232 | CLANG_WARN_BOOL_CONVERSION = YES; 233 | CLANG_WARN_COMMA = YES; 234 | CLANG_WARN_CONSTANT_CONVERSION = YES; 235 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 236 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 237 | CLANG_WARN_EMPTY_BODY = YES; 238 | CLANG_WARN_ENUM_CONVERSION = YES; 239 | CLANG_WARN_INFINITE_RECURSION = YES; 240 | CLANG_WARN_INT_CONVERSION = YES; 241 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 242 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 243 | CLANG_WARN_STRICT_PROTOTYPES = YES; 244 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 245 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 246 | CLANG_WARN_UNREACHABLE_CODE = YES; 247 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 248 | CODE_SIGN_IDENTITY = "iPhone Developer"; 249 | COPY_PHASE_STRIP = NO; 250 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 251 | ENABLE_NS_ASSERTIONS = NO; 252 | ENABLE_STRICT_OBJC_MSGSEND = YES; 253 | GCC_C_LANGUAGE_STANDARD = gnu11; 254 | GCC_NO_COMMON_BLOCKS = YES; 255 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 256 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 257 | GCC_WARN_UNDECLARED_SELECTOR = YES; 258 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 259 | GCC_WARN_UNUSED_FUNCTION = YES; 260 | GCC_WARN_UNUSED_VARIABLE = YES; 261 | IPHONEOS_DEPLOYMENT_TARGET = 11.0; 262 | MTL_ENABLE_DEBUG_INFO = NO; 263 | SDKROOT = iphoneos; 264 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 265 | VALIDATE_PRODUCT = YES; 266 | }; 267 | name = Release; 268 | }; 269 | 551A04AF1EF4794900F30BA2 /* Debug */ = { 270 | isa = XCBuildConfiguration; 271 | buildSettings = { 272 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 273 | CLANG_ENABLE_MODULES = YES; 274 | COREML_CODEGEN_LANGUAGE = Swift; 275 | COREML_CODEGEN_SWIFT_GLOBAL_MODULE = YES; 276 | DEVELOPMENT_TEAM = ZA78SFJ4TY; 277 | INFOPLIST_FILE = TrafficLight/Info.plist; 278 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 279 | PRODUCT_BUNDLE_IDENTIFIER = DIFF.TrafficLight; 280 | PRODUCT_NAME = "$(TARGET_NAME)"; 281 | SWIFT_OBJC_BRIDGING_HEADER = ""; 282 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 283 | SWIFT_VERSION = 4.0; 284 | TARGETED_DEVICE_FAMILY = "1,2"; 285 | }; 286 | name = Debug; 287 | }; 288 | 551A04B01EF4794900F30BA2 /* Release */ = { 289 | isa = XCBuildConfiguration; 290 | buildSettings = { 291 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 292 | CLANG_ENABLE_MODULES = YES; 293 | COREML_CODEGEN_LANGUAGE = Swift; 294 | COREML_CODEGEN_SWIFT_GLOBAL_MODULE = YES; 295 | DEVELOPMENT_TEAM = ZA78SFJ4TY; 296 | INFOPLIST_FILE = TrafficLight/Info.plist; 297 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 298 | PRODUCT_BUNDLE_IDENTIFIER = DIFF.TrafficLight; 299 | PRODUCT_NAME = "$(TARGET_NAME)"; 300 | SWIFT_OBJC_BRIDGING_HEADER = ""; 301 | SWIFT_VERSION = 4.0; 302 | TARGETED_DEVICE_FAMILY = "1,2"; 303 | }; 304 | name = Release; 305 | }; 306 | /* End XCBuildConfiguration section */ 307 | 308 | /* Begin XCConfigurationList section */ 309 | 551A04971EF4794900F30BA2 /* Build configuration list for PBXProject "TrafficLight" */ = { 310 | isa = XCConfigurationList; 311 | buildConfigurations = ( 312 | 551A04AC1EF4794900F30BA2 /* Debug */, 313 | 551A04AD1EF4794900F30BA2 /* Release */, 314 | ); 315 | defaultConfigurationIsVisible = 0; 316 | defaultConfigurationName = Release; 317 | }; 318 | 551A04AE1EF4794900F30BA2 /* Build configuration list for PBXNativeTarget "TrafficLight" */ = { 319 | isa = XCConfigurationList; 320 | buildConfigurations = ( 321 | 551A04AF1EF4794900F30BA2 /* Debug */, 322 | 551A04B01EF4794900F30BA2 /* Release */, 323 | ); 324 | defaultConfigurationIsVisible = 0; 325 | defaultConfigurationName = Release; 326 | }; 327 | /* End XCConfigurationList section */ 328 | }; 329 | rootObject = 551A04941EF4794900F30BA2 /* Project object */; 330 | } 331 | -------------------------------------------------------------------------------- /TrafficLight.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /TrafficLight.xcodeproj/xcuserdata/kiwi.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /TrafficLight.xcodeproj/xcuserdata/kiwi.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | TrafficLight.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /TrafficLight.xcodeproj/xcuserdata/kyx.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | -------------------------------------------------------------------------------- /TrafficLight.xcodeproj/xcuserdata/kyx.xcuserdatad/xcschemes/TrafficLight.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 69 | 70 | 71 | 72 | 73 | 74 | 80 | 82 | 88 | 89 | 90 | 91 | 93 | 94 | 97 | 98 | 99 | -------------------------------------------------------------------------------- /TrafficLight.xcodeproj/xcuserdata/kyx.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | TrafficLight.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | 551A049B1EF4794900F30BA2 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /TrafficLight/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // TrafficLight 4 | // 5 | // Created by kyx on 2017/6/16. 6 | // Copyright © 2017年 DIFF. 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 | -------------------------------------------------------------------------------- /TrafficLight/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 | "info" : { 90 | "version" : 1, 91 | "author" : "xcode" 92 | } 93 | } -------------------------------------------------------------------------------- /TrafficLight/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /TrafficLight/Assets.xcassets/go.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "filename" : "go@2x.png", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /TrafficLight/Assets.xcassets/go.imageset/go@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/life4coding/TrafficLight/0466d1f6cf018abd63b01a0a0e1077484809bf42/TrafficLight/Assets.xcassets/go.imageset/go@2x.png -------------------------------------------------------------------------------- /TrafficLight/Assets.xcassets/nothing.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "filename" : "nothing@2x.png", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /TrafficLight/Assets.xcassets/nothing.imageset/nothing@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/life4coding/TrafficLight/0466d1f6cf018abd63b01a0a0e1077484809bf42/TrafficLight/Assets.xcassets/nothing.imageset/nothing@2x.png -------------------------------------------------------------------------------- /TrafficLight/Assets.xcassets/stop.imageset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "universal", 5 | "scale" : "1x" 6 | }, 7 | { 8 | "idiom" : "universal", 9 | "filename" : "stop@2x.png", 10 | "scale" : "2x" 11 | }, 12 | { 13 | "idiom" : "universal", 14 | "scale" : "3x" 15 | } 16 | ], 17 | "info" : { 18 | "version" : 1, 19 | "author" : "xcode" 20 | } 21 | } -------------------------------------------------------------------------------- /TrafficLight/Assets.xcassets/stop.imageset/stop@2x.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/life4coding/TrafficLight/0466d1f6cf018abd63b01a0a0e1077484809bf42/TrafficLight/Assets.xcassets/stop.imageset/stop@2x.png -------------------------------------------------------------------------------- /TrafficLight/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 | -------------------------------------------------------------------------------- /TrafficLight/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 | 31 | 38 | 45 | 46 | 47 | 48 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | -------------------------------------------------------------------------------- /TrafficLight/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 | To Reg Traffic light 25 | UILaunchStoryboardName 26 | LaunchScreen 27 | UIMainStoryboardFile 28 | Main 29 | UIRequiredDeviceCapabilities 30 | 31 | armv7 32 | 33 | UISupportedInterfaceOrientations 34 | 35 | UIInterfaceOrientationLandscapeLeft 36 | UIInterfaceOrientationLandscapeRight 37 | UIInterfaceOrientationPortrait 38 | 39 | UISupportedInterfaceOrientations~ipad 40 | 41 | UIInterfaceOrientationPortrait 42 | UIInterfaceOrientationPortraitUpsideDown 43 | UIInterfaceOrientationLandscapeLeft 44 | UIInterfaceOrientationLandscapeRight 45 | 46 | 47 | 48 | -------------------------------------------------------------------------------- /TrafficLight/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.swift 3 | // TrafficLight 4 | // 5 | // Created by kyx on 2017/6/16. 6 | // Copyright © 2017年 DIFF. All rights reserved. 7 | // 8 | 9 | import UIKit 10 | import CoreGraphics 11 | import AVFoundation 12 | import Vision 13 | 14 | class ViewController: UIViewController,AVCaptureVideoDataOutputSampleBufferDelegate { 15 | 16 | 17 | @IBOutlet weak var stopPic: UIImageView! 18 | @IBOutlet weak var go: UIImageView! 19 | @IBOutlet weak var nothing: UIImageView! 20 | @IBOutlet weak var predictLabel: UILabel! 21 | 22 | let TrafficLightModel = TFM() 23 | 24 | var requests = [VNRequest]() 25 | var captureSession = AVCaptureSession() 26 | var previewLayer: AVCaptureVideoPreviewLayer? 27 | 28 | override func viewDidLoad() { 29 | super.viewDidLoad() 30 | 31 | setupVision() 32 | setUpCaptureSession() 33 | // Do any additional setup after loading the view, typically from a nib. 34 | } 35 | 36 | 37 | func setUpCaptureSession(){ 38 | 39 | captureSession = AVCaptureSession.init() 40 | captureSession.sessionPreset = .inputPriority 41 | 42 | 43 | guard let catptureDevice = configureDevice() else{return} 44 | 45 | guard let input = try? AVCaptureDeviceInput.init(device: catptureDevice) else {fatalError("cant init captureDeviceInput")} 46 | 47 | captureSession.addInput(input) 48 | 49 | let output = AVCaptureVideoDataOutput.init() 50 | 51 | let queue = DispatchQueue.init(label: "com.kiwi.videocapturequeue") 52 | output.setSampleBufferDelegate(self, queue: queue) 53 | output.videoSettings = [kCVPixelBufferPixelFormatTypeKey as AnyHashable as! String: NSNumber(value: kCVPixelFormatType_32BGRA)] 54 | output.alwaysDiscardsLateVideoFrames = true 55 | 56 | guard captureSession.canAddOutput(output) else { 57 | fatalError() 58 | } 59 | 60 | captureSession.addOutput(output) 61 | startCapturingWithSession(captureSession: captureSession) 62 | } 63 | 64 | func configureDevice()->AVCaptureDevice?{ 65 | 66 | guard let device = AVCaptureDevice.default(for: .video) else {return nil} 67 | 68 | var customFormats = [AVCaptureDevice.Format]() 69 | 70 | let customFPS = Float64(3) 71 | for format in device.formats{ 72 | 73 | for range in format.videoSupportedFrameRateRanges where range.minFrameRate <= customFPS && customFPS <= range.maxFrameRate { 74 | customFormats.append(format) 75 | } 76 | } 77 | 78 | let customSize = CGSize.init(width: 227, height: 227) 79 | 80 | var sizeFormat : AVCaptureDevice.Format? 81 | for format in customFormats{ 82 | 83 | let desc = format.formatDescription 84 | let dimesions = CMVideoFormatDescriptionGetDimensions(desc) 85 | 86 | if dimesions.width >= Int32(customSize.width) && dimesions.height >= Int32(customSize.height){ 87 | 88 | sizeFormat = format 89 | } 90 | } 91 | 92 | do { try device.lockForConfiguration() }catch{fatalError(" error when request configration") } 93 | 94 | device.activeFormat = sizeFormat! 95 | 96 | device.activeVideoMaxFrameDuration = CMTimeMake(1, Int32(customFPS)) 97 | device.activeVideoMinFrameDuration = CMTimeMake(1, Int32(customFPS)) 98 | 99 | device.focusMode = .continuousAutoFocus 100 | 101 | device.unlockForConfiguration() 102 | 103 | return device 104 | 105 | } 106 | 107 | func startCapturingWithSession(captureSession cap: AVCaptureSession){ 108 | 109 | if previewLayer == nil { 110 | previewLayer = AVCaptureVideoPreviewLayer.init(session: cap) 111 | previewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill 112 | previewLayer?.frame.origin = self.view.frame.origin 113 | previewLayer?.frame.size = self.view.frame.size 114 | 115 | self.view.layer.insertSublayer(previewLayer!, at:1) 116 | } 117 | 118 | cap.startRunning() 119 | 120 | } 121 | 122 | func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) { 123 | 124 | handleImageBufferWithCoreML(imageBuffer: sampleBuffer) 125 | 126 | //handleImageBufferWithVision(imageBuffer: sampleBuffer) 127 | 128 | } 129 | 130 | func setupVision() { 131 | guard let visionModel = try? VNCoreMLModel(for: TrafficLightModel.model) else { 132 | fatalError("error when load model") 133 | } 134 | let classificationRequest = VNCoreMLRequest(model: visionModel) { (request: VNRequest, error: Error?) in 135 | guard let observations = request.results else { 136 | print(" error :\(error!)") 137 | return 138 | } 139 | 140 | let classifications = observations[0...2] 141 | .flatMap({ $0 as? VNClassificationObservation }) 142 | .filter({ $0.confidence > 0 }) 143 | .map({ "\($0.identifier) \($0.confidence)" }) 144 | DispatchQueue.main.async { 145 | self.predictLabel.text = classifications.joined(separator: "\n") 146 | } 147 | } 148 | classificationRequest.imageCropAndScaleOption = VNImageCropAndScaleOption.centerCrop 149 | 150 | self.requests = [classificationRequest] 151 | } 152 | 153 | 154 | func handleImageBufferWithCoreML(imageBuffer: CMSampleBuffer) { 155 | guard let pixelBuffer = CMSampleBufferGetImageBuffer(imageBuffer) else { 156 | return 157 | } 158 | do { 159 | 160 | let prediction = try self.TrafficLightModel.prediction(data: resize(pixelBuffer: pixelBuffer)!) 161 | DispatchQueue.main.async { 162 | 163 | self.predictLabel.lineBreakMode = NSLineBreakMode.byWordWrapping 164 | self.predictLabel.numberOfLines = 0 165 | self.predictLabel.text = " none \(String(describing: prediction.prob["none"]!)) \n green \(String(describing: prediction.prob["green"]!)) \n red \(String(describing: prediction.prob["red"]!))" 166 | } 167 | } 168 | catch let error as NSError { 169 | fatalError("Unexpected error ocurred: \(error.localizedDescription).") 170 | } 171 | } 172 | 173 | func handleImageBufferWithVision(imageBuffer: CMSampleBuffer) { 174 | guard let pixelBuffer = CMSampleBufferGetImageBuffer(imageBuffer) else { 175 | return 176 | } 177 | 178 | var requestOptions:[VNImageOption : Any] = [:] 179 | 180 | if let cameraIntrinsicData = CMGetAttachment(imageBuffer, kCMSampleBufferAttachmentKey_CameraIntrinsicMatrix, nil) { 181 | requestOptions = [.cameraIntrinsics:cameraIntrinsicData] 182 | } 183 | 184 | let imageRH = VNImageRequestHandler.init(cvPixelBuffer: pixelBuffer, options: requestOptions) 185 | 186 | do { 187 | try imageRH.perform(self.requests) 188 | } catch { 189 | print(error) 190 | } 191 | } 192 | 193 | func resize(pixelBuffer: CVPixelBuffer) -> CVPixelBuffer? { 194 | let imageSide = 227 195 | var ciImage = CIImage(cvPixelBuffer: pixelBuffer, options: nil) 196 | let transform = CGAffineTransform(scaleX: CGFloat(imageSide) / CGFloat(CVPixelBufferGetWidth(pixelBuffer)), y: CGFloat(imageSide) / CGFloat(CVPixelBufferGetHeight(pixelBuffer))) 197 | ciImage = ciImage.transformed(by: transform).cropped(to: CGRect(x: 0, y: 0, width: imageSide, height: imageSide)) 198 | let ciContext = CIContext() 199 | 200 | var resBuf: CVPixelBuffer? 201 | CVPixelBufferCreate(kCFAllocatorDefault, imageSide, imageSide, CVPixelBufferGetPixelFormatType(pixelBuffer), nil, &resBuf) 202 | ciContext.render(ciImage, to: resBuf!) 203 | return resBuf 204 | } 205 | 206 | } 207 | --------------------------------------------------------------------------------