Create a gist now

Instantly share code, notes, and snippets.

Embed
CoreML ile Görüntü Tanımlama Örneği | Swift -- http://www.bulentsiyah.com/coreml-ile-goruntu-tanimlama-ornegi-swift/
//
// ViewController.swift
// Intelligent Image
//
// Created by Bülent Siyah on 10.06.2018.
// Copyright © 2018 Bülent Siyah. All rights reserved.
//
import UIKit
import CoreML
import Vision
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var choosenImage = CIImage()
@IBOutlet weak var image: UIImageView!
@IBOutlet weak var label: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func changeClick(_ sender: Any) {
let picker = UIImagePickerController()
picker.delegate = self
picker.allowsEditing = true
picker.sourceType = .photoLibrary
self.present(picker, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
image.image = info[UIImagePickerControllerEditedImage] as?UIImage
self.dismiss(animated: true, completion: nil)
if let ciImage = CIImage(image: image.image!){
self.choosenImage = ciImage
}
recognizeImage(image: choosenImage)
}
func recognizeImage (image: CIImage){
label.text = "Finding..."
if let model = try? VNCoreMLModel(for: GoogLeNetPlaces().model){
let request = VNCoreMLRequest(model: model) { (vnrequest, error) in
if let results = vnrequest.results as? [VNClassificationObservation]{
let topResult = results.first
DispatchQueue.main.async {
let conf = (topResult?.confidence)! * 100
let rounded = Int (conf * 100) / 100
self.label.text = "\(rounded)% it's \(topResult?.identifier)"
}
}
}
let handler = VNImageRequestHandler(ciImage: image)
DispatchQueue.global(qos: .userInteractive).async {
do {
try handler.perform([request])
} catch{
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment