首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xcode13 13/ SwiftUI:对象检测应用程序iOS

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

Stack Overflow用户
提问于 2022-05-17 08:04:56
回答 1查看 216关注 0票数 0

我正在使用SwiftUI/Xcode构建一个对象检测应用程序,对于图像分类,我使用了Resnet50。但这是个错误。init()被否决了。我的代码是

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

// captureSession = AVCaptureSession()

代码语言:javascript
复制
    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) for{ let } (无法在作用域中找到模型)让请求=VNCoreMLRequest(模型:模型){ (finishedReq,err)在

代码语言:javascript
复制
        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])
    
    
}

}

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-17 11:39:05

现在就需要配置来初始化它。有点像

代码语言:javascript
复制
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
}

苹果公司有很好的示例代码,可以让你开始工作。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72270273

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档