Last active
November 30, 2017 21:15
-
-
Save ShawonAshraf/3ccb294d02f37f5f8d383988331caf20 to your computer and use it in GitHub Desktop.
ClassifierGUISwift.md
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
// | |
// ViewController.swift | |
// macOS_GUI | |
// | |
// Created by Shawon Ashraf on 7/8/17. | |
// Copyright © 2017 Shawon Ashraf. All rights reserved. | |
// | |
import Cocoa | |
class ViewController: NSViewController { | |
@IBOutlet var imageView: NSImageView! | |
@IBOutlet var loadImageButton: NSButton! | |
@IBOutlet var recognizeButton: NSButton! | |
@IBOutlet var resultLabel: NSTextField! | |
var imagePath: String! | |
var labelPath: String! | |
var graphPath: String! | |
let scriptPath = "/path/to/python/script" | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view. | |
} | |
override var representedObject: Any? { | |
didSet { | |
// Update the view, if already loaded. | |
} | |
} | |
@IBAction func loadImage(_ sender: Any) { | |
// create the file open dialog | |
let fileOpenDialog = NSOpenPanel() | |
fileOpenDialog.title = "Select the image file" | |
fileOpenDialog.showsResizeIndicator = true | |
fileOpenDialog.canChooseFiles = true | |
fileOpenDialog.canChooseDirectories = true | |
fileOpenDialog.canCreateDirectories = true | |
fileOpenDialog.allowsMultipleSelection = false | |
fileOpenDialog.allowedFileTypes = ["png", "jpg", "jpeg"] | |
// now call the dialog | |
if (fileOpenDialog.runModal() == NSModalResponseOK) { | |
let result = fileOpenDialog.url | |
if (result != nil) { | |
imagePath = result!.path | |
// set image to image viewer | |
let image = NSImage(byReferencingFile: imagePath!) | |
imageView.image = image | |
} | |
} | |
} | |
@IBAction func recognizeFromImage(_ sender: Any) { | |
// set label and graph path | |
labelPath = "path/to/labels" | |
graphPath = "path/to/tf_graph" | |
// initiate task for running script | |
let task = Process() | |
task.launchPath = "path/to/python" | |
task.arguments = [scriptPath, imagePath!, labelPath!, graphPath!] | |
// create the pipe | |
let pipe = Pipe() | |
task.standardOutput = pipe | |
// launch task | |
task.launch() | |
let data = pipe.fileHandleForReading.readDataToEndOfFile() | |
let output = NSString(data: data, encoding: String.Encoding.utf8.rawValue) | |
resultLabel.stringValue = output! as String | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment