我制作了一个使用 CoreML 的图像识别应用。从UI方面来说,我想在功能detect()
完成后选择图像并显示结果后返回主视图。但是,即使我使用DispatchQueue.main
,它也始终同时显示结果和主视图。如果我使用DispatchQueue.main
中的detect()
,它可以正常工作。但我认为它不应该在那里。
还有一个问题SVProgressHUD.show()
无效。
import UIKit
import CoreML
import Vision
import SVProgressHUD
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet weak var imageView: UIImageView!
@IBOutlet weak var cameraButton: UIBarButtonItem!
let imagePicker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
imagePicker.delegate = self
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
cameraButton.isEnabled = false
SVProgressHUD.show()
if let userPickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage{
imageView.image = userPickedImage
imagePicker.dismiss(animated: true, completion: nil)
let userPickedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
guard let ciImage = CIImage(image: userPickedImage) else {
fatalError("WRONG")
}
detect(image: ciImage)
}
}
func detect(image: CIImage){
guard let model = try? VNCoreMLModel(for: Inceptionv3().model) else {
fatalError("Loading CoreML Model Failed")
}
let request = VNCoreMLRequest(model: model) { (request, error) in
guard let results = request.results as? [VNClassificationObservation] else{
fatalError("Model failed to process image")
}
if let firstResult = results.first {
let r = firstResult.identifier.split(separator: ",")
DispatchQueue.main.async {
self.cameraButton.isEnabled = true
SVProgressHUD.dismiss()
self.navigationItem.title = r.last?.description
}
}
}
let handler = VNImageRequestHandler(ciImage: image)
do{
try handler.perform([request])
}
catch {
print(error)
}
}
@IBAction func cameraTapped(_ sender: UIBarButtonItem) {
imagePicker.sourceType = .savedPhotosAlbum
imagePicker.allowsEditing = false
present(imagePicker, animated: true, completion: nil)
}
}
答案 0 :(得分:0)
如果我理解你的问题,你可以改变如下:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
cameraButton.isEnabled = false
SVProgressHUD.show()
if let userPickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage{
imageView.image = userPickedImage
imagePicker.dismiss(animated: true) {
guard let ciImage = CIImage(image: userPickedImage) else {
fatalError("WRONG")
}
self.detect(image: ciImage)
}
}
}