Skip to content

Instantly share code, notes, and snippets.

@bulentsiyah
Last active June 10, 2018 20:51
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save bulentsiyah/b761521c781d24dcf7ccf376959ea930 to your computer and use it in GitHub Desktop.
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