Last active
July 2, 2018 20:35
-
-
Save bulentsiyah/e8a9e68e8693c49c7f237fa662046f47 to your computer and use it in GitHub Desktop.
IBM Watson Eğitilmiş Model (Core ML) ile Trafik İşaretleri Tanımlama | iOS --- http://www.bulentsiyah.com/ibm-watson-egitilmis-model-core-ml-ile-trafik-isaretleri-tanimlama-ios/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// IBMCustomMeyveTrafikViewController.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 IBMCustomMeyveTrafikViewController: UIViewController , UIImagePickerControllerDelegate, UINavigationControllerDelegate { | |
var meyveTrueTrafikFalse = true | |
@IBOutlet weak var image: UIImageView! | |
@IBOutlet weak var result: UILabel! | |
var imagePicker: UIImagePickerController! | |
var userPickedImage: UIImage? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
imagePicker = UIImagePickerController() | |
imagePicker.delegate = self | |
} | |
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) { | |
if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage { | |
image.contentMode = .scaleAspectFit | |
image.image = pickedImage | |
self.userPickedImage = pickedImage | |
} | |
dismiss(animated: true, completion: nil) | |
} | |
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { | |
dismiss(animated: true, completion: nil) | |
} | |
@IBAction func btnResimSec(_ sender: Any) { | |
imagePicker.allowsEditing = false | |
imagePicker.sourceType = .photoLibrary | |
present(imagePicker, animated: true, completion: nil) | |
} | |
@IBOutlet var btnAnalizEt: UIView! | |
@IBAction func btnAnalizYap(_ sender: Any) { | |
do { | |
var model : VNCoreMLModel | |
if(meyveTrueTrafikFalse){ | |
model = try VNCoreMLModel(for: ElmaMandalinePortakal().model) | |
}else{ | |
model = try VNCoreMLModel(for: Trafikisaretleri().model) | |
} | |
let request = VNCoreMLRequest(model: model) { (request, error) in | |
if let results = request.results as? [VNClassificationObservation], let result = results.first { | |
DispatchQueue.main.async { | |
self.result.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