Skip to content
This repository has been archived by the owner on Jan 22, 2021. It is now read-only.

Commit

Permalink
Merge pull request #80 from IBM/loadModelFromServer
Browse files Browse the repository at this point in the history
Load model from server
  • Loading branch information
sanjeevghimire committed Jun 1, 2018
2 parents 3b6e229 + bf98109 commit 2e4b5e9
Show file tree
Hide file tree
Showing 14 changed files with 412 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Server/Sources/Application/Model/ScoreEntry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct ScoreEntry: Codable {

extension ScoreEntry: Hashable {
var hashValue: Int {
return id?.hashValue ?? Int(INT_MAX)
return id?.hashValue ?? Int.max
}

static func == (lhs: ScoreEntry, rhs: ScoreEntry) -> Bool {
Expand Down
1 change: 1 addition & 0 deletions Server/Sources/Application/Routes/RouteAutheticator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func setupBasicAuth(app: App) {
if result {
Log.debug("Successfully authenticated!")
callback(UserProfile(id: userId, displayName: userId, provider: "HTTPBasic-Kitura"))
return
}
} catch {
Log.error("VerifyPassword internal error")
Expand Down
8 changes: 4 additions & 4 deletions iOS/rainbow.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -912,9 +912,9 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_LAUNCHIMAGE_NAME = LaunchImage;
CODE_SIGN_ENTITLEMENTS = rainbow/rainbow.entitlements;
CODE_SIGN_IDENTITY = "iPhone Developer";
CODE_SIGN_IDENTITY = "iPhone Distribution";
CODE_SIGN_STYLE = Manual;
DEVELOPMENT_TEAM = "";
DEVELOPMENT_TEAM = RQ632LL2X3;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/Carthage/Build/iOS",
Expand All @@ -927,8 +927,8 @@
);
PRODUCT_BUNDLE_IDENTIFIER = com.okun.io.vivaml;
PRODUCT_NAME = "$(TARGET_NAME)";
PROVISIONING_PROFILE = "";
PROVISIONING_PROFILE_SPECIFIER = "";
PROVISIONING_PROFILE = "4a53f4b4-d362-4b9e-b5f8-36375d8f9dfb";
PROVISIONING_PROFILE_SPECIFIER = "Viva ML App Store Profile 2018";
SWIFT_VERSION = 4.0;
TARGETED_DEVICE_FAMILY = 1;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0940"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "533818FD209757B900DE66CB"
BuildableName = "rainbow.app"
BlueprintName = "rainbow"
ReferencedContainer = "container:rainbow.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "533818FD209757B900DE66CB"
BuildableName = "rainbow.app"
BlueprintName = "rainbow"
ReferencedContainer = "container:rainbow.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "533818FD209757B900DE66CB"
BuildableName = "rainbow.app"
BlueprintName = "rainbow"
ReferencedContainer = "container:rainbow.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<EnvironmentVariables>
<EnvironmentVariable
key = "DEBUGMODE"
value = "3"
isEnabled = "YES">
</EnvironmentVariable>
</EnvironmentVariables>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "533818FD209757B900DE66CB"
BuildableName = "rainbow.app"
BlueprintName = "rainbow"
ReferencedContainer = "container:rainbow.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0940"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "533818FD209757B900DE66CB"
BuildableName = "rainbow.app"
BlueprintName = "rainbow"
ReferencedContainer = "container:rainbow.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "533818FD209757B900DE66CB"
BuildableName = "rainbow.app"
BlueprintName = "rainbow"
ReferencedContainer = "container:rainbow.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "533818FD209757B900DE66CB"
BuildableName = "rainbow.app"
BlueprintName = "rainbow"
ReferencedContainer = "container:rainbow.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<EnvironmentVariables>
<EnvironmentVariable
key = "DEBUGMODE"
value = "2"
isEnabled = "YES">
</EnvironmentVariable>
</EnvironmentVariables>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "533818FD209757B900DE66CB"
BuildableName = "rainbow.app"
BlueprintName = "rainbow"
ReferencedContainer = "container:rainbow.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
1 change: 0 additions & 1 deletion iOS/rainbow/Controller/Booklet/BookletBaseController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,4 @@ class BookletBaseController: UIViewController {
super.viewWillAppear(animated)
UIApplication.shared.statusBarStyle = UIStatusBarStyle.default
}

}
56 changes: 54 additions & 2 deletions iOS/rainbow/Controller/Camera/CameraController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import Foundation
import Lumina
import AudioToolbox
import SVProgressHUD
import VisualRecognitionV3

enum GameCameraState {
case shouldStartNewGame // when the game should be started, or has finished and could be restarted
Expand Down Expand Up @@ -37,7 +38,13 @@ class CameraController: LuminaViewController {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.delegate = self
self.streamingModels = [LuminaModel(model: ProjectRainbowModel_1753554316().model, type: "WatsonML")]
VisualRecognitionUpdate.loadLatestModel(useCloudAPI: false) { model, _ in
guard let model = model else {
SVProgressHUD.showError(withStatus: "Could not load visual model from device")
return
}
self.streamingModels = [LuminaModel(model: model, type: "WatsonML")]
}
self.setShutterButton(visible: false)
self.setTorchButton(visible: true)
self.setCancelButton(visible: false)
Expand Down Expand Up @@ -81,6 +88,11 @@ class CameraController: LuminaViewController {
super.viewWillAppear(animated)
determineGameState()
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
checkIfLatestModelAvailable()
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
Expand All @@ -104,7 +116,6 @@ class CameraController: LuminaViewController {
continueGame()
}
} catch {
// NotificationCenter.default.post(name: Notification.Name("viva-ml-device-token-registered"), object: "00000000-0000-0000-0000-000000000000")
showStartView()
}
}
Expand Down Expand Up @@ -167,6 +178,47 @@ class CameraController: LuminaViewController {
self.textPrompt = GameTimer.getTimeElapsedString(for: cachedScoreEntry)
}
}

fileprivate func checkIfLatestModelAvailable() {
let defaults = UserDefaults.standard
guard let lastCheckDate = defaults.string(forKey: "lastModelCheckDate")?.dateFromISO8601 else {
// this has never checked - log the first date and continually check whenever we load this screen again
defaults.set(Date().iso8601, forKey: "lastModelCheckDate")
defaults.synchronize()
return
}
let interval = Date().timeIntervalSince(lastCheckDate)
if interval > TimeInterval(60 * 60 * 24 * 7) {
// its been 7 days since the last check for a fresh model, let's see if there's something new to download
defaults.set(Date().iso8601, forKey: "lastModelCheckDate")
defaults.synchronize()
VisualRecognitionUpdate.checkNewModelAvailable { available in
DispatchQueue.main.async {
if available { self.promptForLatestModel() }
}
}
}
}

private func promptForLatestModel() {
let alert = UIAlertController.init(title: "Load New Visual Model", message: "This will attempt to download a file that is between 15-20 MBs. Do you want to continue?", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
let okAction = UIAlertAction(title: "Download", style: .default) { _ in
UIApplication.shared.isNetworkActivityIndicatorVisible = true
VisualRecognitionUpdate.loadLatestModel(useCloudAPI: true, completion: { model, _ in
UIApplication.shared.isNetworkActivityIndicatorVisible = false
if let model = model {
SVProgressHUD.showSuccess(withStatus: "Latest model loaded")
self.streamingModels = [LuminaModel(model: model, type: "WatsonML")]
} else {
SVProgressHUD.showError(withStatus: "Could not download - using existing model")
}
})
}
alert.addAction(cancelAction)
alert.addAction(okAction)
present(alert, animated: true, completion: nil)
}
}

// MARK: State Check On Load
Expand Down
31 changes: 31 additions & 0 deletions iOS/rainbow/Controller/Leaderboard/Date+WatsonML.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,35 @@ extension Date {
dateFormatter.timeStyle = .medium
return dateFormatter.string(from: self)
}

var iso8601: String {
return Formatter.iso8601.string(from: self)
}
}

extension Formatter {
static let iso8601: DateFormatter = {
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .iso8601)
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX"
return formatter
}()

static let watson: DateFormatter = {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
return dateFormatter
}()
}

extension String {
var dateFromISO8601: Date? {
return Formatter.iso8601.date(from: self) // "Mar 22, 2017, 10:22 AM"
}

var dateFromWatson: Date? {
return Formatter.watson.date(from: self)
}
}
Loading

0 comments on commit 2e4b5e9

Please sign in to comment.