<commit_before>//
//  ViewController.swift
//  SwiftOpenCV
//
//  Created by <NAME> on 10/28/14.
//  Copyright (c) 2014 WhitneyLand. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    enum Segue: String {
        case ShowRecognition
    }
    
    @IBOutlet weak var imageView: UIImageView!
    
    var selectedImage : UIImage?
    
    // MARK: Segue
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let vc =  segue.destinationViewController as! DetailViewController
        vc.recognizedText = sender as! String
    }
    
    // MARK: Actions
    
    @IBAction func onTakePictureTapped(sender: AnyObject) {
        
        let sheet: UIActionSheet = UIActionSheet();
        let title: String = "Please choose an option";
        sheet.title  = title;
        sheet.delegate = self;
        sheet.addButtonWithTitle("Choose Picture");
        sheet.addButtonWithTitle("Take Picture");
        sheet.addButtonWithTitle("Cancel");
        sheet.cancelButtonIndex = 2;
        sheet.showInView(self.view);
    }
    
    @IBAction func onRecognizeTapped(sender: AnyObject) {
        
        guard let selectedImage = selectedImage else {
            
            let alert = UIAlertController(title: "SwiftOCR", message: "Please select image", preferredStyle: .Alert)
            let cancel = UIAlertAction(title: "Ok", style: .Cancel, handler: nil)
            presentViewController(alert, animated: true, completion: nil)
            return
        }
        
        let progressHud = MBProgressHUD.showHUDAddedTo(view, animated: true)
        progressHud.labelText = "Detecting..."
        progressHud.mode = MBProgressHUDModeIndeterminate
        
        dispatch_async(dispatch_get_global_queue(0, 0)) {
            let ocr = SwiftOCR(fromImage: self.selectedImage)
            ocr.recognize()
            
            dispatch_sync(dispatch_get_main_queue()) {
                self.imageView.image = ocr.groupedImage
                
                progressHud.hide(true);
                
                let dprogressHud = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
                dprogressHud.labelText = "Recognizing..."
                dprogressHud.mode = MBProgressHUDModeIndeterminate
                
                let text = ocr.recognizedText
                
                self.performSegueWithIdentifier(Segue.ShowRecognition.rawValue, sender: text)
                
                dprogressHud.hide(true)
            }
        }
    }
}

// MARK: - UIActionSheetDelegate

extension ViewController: UIActionSheetDelegate {
    
    func actionSheet(sheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        
        switch buttonIndex{
            
        case 0:
            imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
            imagePicker.allowsEditing = false
            imagePicker.delegate = self
            self.presentViewController(imagePicker, animated: true, completion: nil)
            break;
        case 1:
            imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
            imagePicker.allowsEditing = false
            imagePicker.delegate = self
            self.presentViewController(imagePicker, animated: true, completion: nil)
            break;
        default:
            break;
        }
    }
}

// MARK: - UIImagePickerControllerDelegate

extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        
        guard let choosenImage = info[UIImagePickerControllerOriginalImage] as? UIImage else { return }
        selectedImage = choosenImage
        picker.dismissViewControllerAnimated(true, completion: nil)
        imageView.image = choosenImage
    }
    
    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        picker.dismissViewControllerAnimated(true, completion: nil)
    }<commit_msg>Using UIAlertController instead of UIActionSheet<commit_after>//
//  ViewController.swift
//  SwiftOpenCV
//
//  Created by <NAME> on 10/28/14.
//  Copyright (c) 2014 WhitneyLand. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
    
    enum Segue: String {
        case ShowRecognition
    }
    
    @IBOutlet weak var imageView: UIImageView!
    
    var selectedImage : UIImage?
    
    // MARK: Segue
    
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let vc =  segue.destinationViewController as! DetailViewController
        vc.recognizedText = sender as! String
    }
    
    // MARK: Actions
    
    @IBAction func onTakePictureTapped(sender: AnyObject) {
        
        let alert = UIAlertController(title: "Please choose an option", message: nil, preferredStyle: .ActionSheet)
        let cameraAction = UIAlertAction(title: "Take Picture", style: .Default) { [weak self] action in
            
            let imagePicker = UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
            imagePicker.allowsEditing = false
            self?.presentViewController(imagePicker, animated: true, completion: nil)
        }
        let photoLibraryAction = UIAlertAction(title: "Choose Picture", style: .Default) { [weak self] action in
            
            let imagePicker = UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
            imagePicker.allowsEditing = false
            self?.presentViewController(imagePicker, animated: true, completion: nil)
        }
        let cancelAction = UIAlertAction(title: "Cancel", style: .Cancel, handler: nil)
        
        alert.addAction(cameraAction)
        alert.addAction(photoLibraryAction)
        alert.addAction(cancelAction)
        presentViewController(alert, animated: true, completion: nil)
    }
    
    @IBAction func onRecognizeTapped(sender: AnyObject) {
        
        guard let selectedImage = selectedImage else {
            
            let alert = UIAlertController(title: "SwiftOCR", message: "Please select image", preferredStyle: .Alert)
            let cancel = UIAlertAction(title: "Ok", style: .Cancel, handler: nil)
            alert.addAction(cancel)
            presentViewController(alert, animated: true, completion: nil)
            return
        }
        
        let progressHud = MBProgressHUD.showHUDAddedTo(view, animated: true)
        progressHud.labelText = "Detecting..."
        progressHud.mode = MBProgressHUDModeIndeterminate
        
        dispatch_async(dispatch_get_global_queue(0, 0)) {
            let ocr = SwiftOCR(fromImage: selectedImage)
            ocr.recognize()
            
            dispatch_sync(dispatch_get_main_queue()) {
                self.imageView.image = ocr.groupedImage
                
                progressHud.hide(true);
                
                let dprogressHud = MBProgressHUD.showHUDAddedTo(self.view, animated: true)
                dprogressHud.labelText = "Recognizing..."
                dprogressHud.mode = MBProgressHUDModeIndeterminate
                
                let text = ocr.recognizedText
                
                self.performSegueWithIdentifier(Segue.ShowRecognition.rawValue, sender: text)
                
                dprogressHud.hide(true)
            }
        }
    }
}

// MARK: - UIImagePickerControllerDelegate

extension ViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        
        guard let choosenImage = info[UIImagePickerControllerOriginalImage] as? UIImage else { return }
        selectedImage = choosenImage
        picker.dismissViewControllerAnimated(true, completion: nil)
        imageView.image = choosenImage
    }
    
    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        picker.dismissViewControllerAnimated(true, completion: nil)
    }
{'fraction_non_alphanumeric': 0.07477744807121661, 'fraction_numerical': 0.003204747774480712, 'mean_word_length': 2.2965571205007826, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '796286', 'n_tokens_mistral': 2244, 'n_tokens_neox': 2130, 'n_words': 471}