Xcode13/ SwiftUI:对象检测应用程序 iOS

Xcode13/ SwiftUI: Object detection app iOS

我正在使用 SwiftUI/Xcode 构建对象检测应用程序,对于图像分类,我使用了 Resnet50。但是有一个错误。 [init() 已弃用][1]。我的代码是

如何解决这个问题。我是初学者,请简单点

//相机 让 captureSession = AVCaptureSession()

    guard let captureDevice = AVCaptureDevice.default(for: .video) else { return }
    guard let input = try? AVCaptureDeviceInput(device: captureDevice) else { return }
    captureSession.addInput(input)
    
    captureSession.startRunning()
    
    let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    view.layer.addSublayer(previewLayer)
    previewLayer.frame = view.frame
    
    
    // The camera is now created!
    
    view.addSubview(belowView)
    
    belowView.clipsToBounds = true
    belowView.layer.cornerRadius = 15.0
    belowView.layer.maskedCorners = [.layerMaxXMinYCorner, .layerMinXMinYCorner]
    
    
    let  dataOutput = AVCaptureVideoDataOutput()
    dataOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "videoQueue"))
    captureSession.addOutput(dataOutput)
}
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
    guard let pixelBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }

守卫让模型=试试? VNCoreMLModel(for: model) else { return } (在范围内找不到模型) 让 request = VNCoreMLRequest(model: model) { (finishedReq, err) in

        guard let results = finishedReq.results as? [VNClassificationObservation] else {return}
        guard let firstObservation = results.first else {return}
        
        let name: String = firstObservation.identifier
        let acc: Int = Int(firstObservation.confidence * 100)
        
        DispatchQueue.main.async {
            self.objectNameLabel.text = name
            self.accuracyLabel.text = "Accuracy: \(acc)%"
            
        }
    }
    
    try? VNImageRequestHandler(cvPixelBuffer: pixelBuffer, options: [:]).perform([request])
    
    
}

}

您现在需要配置来初始化它。像

static func createImageClassifier() -> VNCoreMLModel {
    // Use a default model configuration.
    let defaultConfig = MLModelConfiguration()

    // Create an instance of the image classifier's wrapper class.
    let imageClassifierWrapper = try? Resnet50(configuration: defaultConfig)

    guard let imageClassifier = imageClassifierWrapper else {
        fatalError("App failed to create an image classifier model instance.")
    }

    // Get the underlying model instance.
    let imageClassifierModel = imageClassifier.model

    // Create a Vision instance using the image classifier's model instance.
    guard let imageClassifierVisionModel = try? VNCoreMLModel(for: imageClassifierModel) else {
        fatalError("App failed to create a `VNCoreMLModel` instance.")
    }

    return imageClassifierVisionModel
}

Apple 有非常好的示例代码可以帮助您入门。

https://developer.apple.com/documentation/vision/classifying_images_with_vision_and_core_ml