Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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/
//
// 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
You can’t perform that action at this time.