【问题标题】:Image Recognition Results are not printed in Swift Playgrounds图像识别结果未在 Swift Playgrounds 中打印
【发布时间】:2022-01-26 20:29:59
【问题描述】:

所以我一直在操场上工作以识别实时捕获中的对象,但是当我尝试打印结果时,结果没有打印出来。这是我的代码。我也尝试过运行我的代码,它只是在结果的守卫中执行 return。 SetupLabel func 也无法执行,因为它表示操场存在问题

class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {
// make a computed property based of the properties defined in the curly braces 
let label: UILabel = {
    let label = UILabel()
    label.textColor = .white
    label.translatesAutoresizingMaskIntoConstraints = false
    label.text = "Label"
    label.font = label.font.withSize(30)
    return label
}()



override func viewDidLoad() {
    super.viewDidLoad()
    setupCaptureSession()
    
}

func setupCaptureSession(){
    let captureSession = AVCaptureSession()
    
    // search for devices with specifications defined 
    let availableDevices = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: .video, position: .back).devices
    
    // setup capture device add input to captureSession
    do{
        if let captureDevice = availableDevices.first{
            let captureDeviceInput = try AVCaptureDeviceInput(device: captureDevice) 
            
            captureSession.addInput(captureDeviceInput)
        }
        
    }catch{
        print(error.localizedDescription)
    }
    
    // setup output and output to captureSession
    let captureOutput = AVCaptureVideoDataOutput()
    captureOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "videoQueue"))
    captureSession.addOutput(captureOutput)
    
    let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    previewLayer.frame = view.frame
    view.layer.addSublayer(previewLayer)
    
    captureSession.startRunning()
}

// called when a frame is captured 
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
    
    guard let model = try? VNCoreMLModel(for: myYolo) else {return}
    
    
    let request = VNCoreMLRequest(model: model) { (finishedRequest, error) in
        print(finishedRequest.results)
        
        
        
        guard let results = finishedRequest.results as? [VNClassificationObservation] else { return }
        guard let Observation = results.first else { return }
        
        
        DispatchQueue.main.async(execute: {
            self.label.text = "\(Observation.identifier)"
        })
    }
    guard let pixelBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }
    
    // executes request
    try? VNImageRequestHandler(cvPixelBuffer: pixelBuffer, options: [:]).perform([request])
}

func SetupLabel(){
    label.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    
    label.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -50).isActive = true
    
    view.addSubview(label)
}
}

【问题讨论】:

  • print(finishedRequest.results) 打印出什么?如果 guard let results = finishedRequest.results as? [VNClassificationObservation] else { return } 失败,则 .results 为 nil 或转换为 [VNClassificationObservation] 失败。

标签: swift swift-playground coreml vision


【解决方案1】:

您需要在真实设备上运行它。
视觉请求在游乐场/模拟器中不起作用。

【讨论】:

  • 谢谢,但是有没有办法在操场上运行它
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 2019-11-19
  • 2021-08-15
  • 2018-02-19
  • 2017-07-18
相关资源
最近更新 更多