Skip to content

Instantly share code, notes, and snippets.

@bulentsiyah
Last active July 2, 2018 19:42
Show Gist options
  • Select an option

  • Save bulentsiyah/6c608c07e16e6dd06604f2128d8c365c to your computer and use it in GitHub Desktop.

Select an option

Save bulentsiyah/6c608c07e16e6dd06604f2128d8c365c to your computer and use it in GitHub Desktop.
IBM Watson Eğitilmiş Model (Core ML) ile Meyve Tanımlama | iOS -- http://www.bulentsiyah.com/ibm-watson-egitilmis-model-core-ml-ile-meyve-tanimlama-ios/
//
// IBMMeyveViewController.swift
// ML Ornekleri
//
// Created by Bülent Siyah on 2.07.2018.
// Copyright © 2018 Bülent Siyah. All rights reserved.
//
import UIKit
import CoreML
import Vision
class IBMMeyveViewController: UIViewController , UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var imageViewSelected: UIImageView!
@IBOutlet weak var labelResult: UILabel!
var imagePicker: UIImagePickerController!
var userPickedImage: UIImage?
override func viewDidLoad() {
super.viewDidLoad()
imagePicker = UIImagePickerController()
imagePicker.delegate = self
}
@IBAction func btnImageSec(_ sender: Any) {
imagePicker.allowsEditing = false
imagePicker.sourceType = .photoLibrary
present(imagePicker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
imageViewSelected.contentMode = .scaleAspectFit
imageViewSelected.image = pickedImage
self.userPickedImage = pickedImage
}
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: true, completion: nil)
}
@IBAction func btnAnalizEt(_ sender: Any) {
do {
let model = try VNCoreMLModel(for: ElmaMandalinePortakal().model)
let request = VNCoreMLRequest(model: model) { (request, error) in
if let results = request.results as? [VNClassificationObservation], let result = results.first {
DispatchQueue.main.async {
self.labelResult.text = "\(result.identifier) \(result.confidence * 100)%"
}
}
}
request.imageCropAndScaleOption = .centerCrop
DispatchQueue.global(qos: DispatchQoS.QoSClass.userInitiated).async {
let handler = VNImageRequestHandler(cgImage: self.userPickedImage!.cgImage!, options: [:])
do {
try handler.perform([request])
} catch {
print(error)
}
}
}catch {
print(error)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment