Skip to content

Instantly share code, notes, and snippets.

@kunal732
Created January 20, 2018 17:18
Show Gist options
  • Save kunal732/9bfd8458ab4f0e4c4992d2f7ed6bc111 to your computer and use it in GitHub Desktop.
Save kunal732/9bfd8458ab4f0e4c4992d2f7ed6bc111 to your computer and use it in GitHub Desktop.
Just looking at the top result for Predictions through the Clarifai Apple SDK - Partial AppDelegate.Swift file.
//
// AppDelegate.swift
// third
//
// Created by Kunal Batra on 1/16/18.
// Copyright © 2018 Kunal Batra. All rights reserved.
//
import UIKit
import Clarifai_Apple_SDK
import CoreData
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var model: Model!
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//Initialize with API Key
Clarifai.sharedInstance().start(apiKey:"your_api_key_goes_here")
// Create Image(s)
let image = Image(url: URL(string: "https://englishlanguagethoughts.files.wordpress.com/2017/09/gr1.jpg"))
self.model = Clarifai.sharedInstance().generalModel
// Create Data Asset(s)
// A Data Asset is a container for the asset in question, plus metadata
// related to it
let dataAsset = DataAsset.init(image: image)
// Create Input(s) and Input(s) Array
// An input object contains the data asset, temporal information, and
// is a fundamental component to be used by models to train on or predict.
let input = Input.init(dataAsset:dataAsset)
let inputs = [input]
/// Use the model you want to predict on. This can be a custom trained or one of our public models.
self.model.predict(inputs, completionHandler: {(outputs: [Output]?,error: Error?) -> Void in
// Iterate through outputs to learn about what has been predicted
//NSLog(String([outputs].count))
for output in outputs! {
// Do something with your outputs
//Just look at the first image in the input array
for (img_index, concept) in output.dataAsset.concepts!.enumerated() {
//Do something with the top result
if img_index == 0 {
NSLog(concept.name)
}
}
}
})
// Override point for customization after application launch.
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment