简体   繁体   English

如何在Swift中使用CoreML创建不带实时输入的简单相机应用程序?

[英]How Do I create a Simple Camera App in Swift Using CoreML that does not take Live Input?

I have been trying to create a simple camera image recognition app in xcode with swift that allows a user to take a picture. 我一直在尝试使用xcode快速创建一个简单的相机图像识别应用程序,以允许用户拍照。 The photo is then input into an already trained coreML model and the output with the predicted accuracy is output to a label. 然后将照片输入到已经训练好的coreML模型中,并将具有预测精度的输出输出到标签。

I have searched multiple websites, and all I can find is tutorials such as 我搜索了多个网站,而我所能找到的只是诸如

https://medium.freecodecamp.org/ios-coreml-vision-image-recognition-3619cf319d0b https://medium.freecodecamp.org/ios-coreml-vision-image-recognition-3619cf319d0b

that allow for the real time recognition of images. 可以实时识别图像。 I do not want it to be real time but rather just allow someone to take a picture. 我不希望它是实时的,而只是允许某人拍照。 I would like to know how to possibly transform this code in a way where it is not live input: 我想知道如何以一种不是实时输入的方式来转换此代码:

  import UIKit
  import AVFoundation
  import Vision

class ViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {
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()

    // establish the capture session and add the label
    setupCaptureSession()
    view.addSubview(label)
    setupLabel()
    // Do any additional setup after loading the view, typically from a nib.
}
func setupCaptureSession() {
    // create a new capture session
    let captureSession = AVCaptureSession()

    // find the available cameras
    let availableDevices = AVCaptureDevice.DiscoverySession(deviceTypes: [.builtInWideAngleCamera], mediaType: AVMediaType.video, position: .back).devices

    do {
        // select a camera
        if let captureDevice = availableDevices.first {
            captureSession.addInput(try AVCaptureDeviceInput(device: captureDevice))
        }
    } catch {
        // print an error if the camera is not available
        print(error.localizedDescription)
    }

    // setup the video output to the screen and add output to our capture session
    let captureOutput = AVCaptureVideoDataOutput()
    captureSession.addOutput(captureOutput)
    let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
    previewLayer.frame = view.frame
    view.layer.addSublayer(previewLayer)

    // buffer the video and start the capture session
    captureOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "videoQueue"))
    captureSession.startRunning()
}

func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
    // load our CoreML Pokedex model
    guard let model = try? VNCoreMLModel(for: aslModel().model) else { return }

    // run an inference with CoreML
    let request = VNCoreMLRequest(model: model) { (finishedRequest, error) in

        // grab the inference results
        guard let results = finishedRequest.results as? [VNClassificationObservation] else { return }

        // grab the highest confidence result
        guard let Observation = results.first else { return }

        // create the label text components
        let predclass = "\(Observation.identifier)"
        let predconfidence = String(format: "%.02f%", Observation.confidence * 100)

        // set the label text
        DispatchQueue.main.async(execute: {
            self.label.text = "\(predclass) \(predconfidence)"
        })
    }


    // create a Core Video pixel buffer which is an image buffer that holds pixels in main memory
    // Applications generating frames, compressing or decompressing video, or using Core Image
    // can all make use of Core Video pixel buffers
    guard let pixelBuffer: CVPixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }

    // execute the request
    try? VNImageRequestHandler(cvPixelBuffer: pixelBuffer, options: [:]).perform([request])
}
func setupLabel() {
    // constrain the label in the center
    label.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true

    // constrain the the label to 50 pixels from the bottom
    label.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -50).isActive = true
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

} }

Right now like stated before it takes in live image input. 现在就像在输入实时图像输入之前所述。

I wrote a post on Medium about that, but it is in Portuguese. 我在Medium上写了一篇文章,但它是葡萄牙语。 See if auto translation of Medium allows you to understand the post. 查看是否自动翻译Medium可以使您理解帖子。

Swift + Core ML Swift +核心ML

I hope this can help you. 希望对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Swift、UIkit 和 CoreML 在 iOS 应用程序中访问图像分类器 ML 模型的预测结果 - How to access prediction results of an image classifier ML model in an iOS app using Swift, UIkit, and CoreML 如何使用trained.tfilte model在实时摄像头output上创建蒙版? - How do I use trained .tfilte model, to create mask on live-camera output? 如何使用 Swift 在 iOS 上为 4D 数据创建 CoreML MLMultiArray 进行预测? - How to create CoreML MLMultiArray for a 4D data for prediction on iOS with Swift? 如何在Swift 4中使用相机? - How do I access the camera in Swift 4? 有没有一种方法可以在不要求相机权限的情况下创建一个简单的 iOS 相机实例? (斯威夫特 5.1) - Is there a way I can create a simple iOS camera instance without asking for camera permissions? (Swift 5.1) 在使用Swift的iOS中,如何创建始终在整个应用程序顶部的可拖动和浮动UIView - In iOS using swift, how do I create a draggable and floating UIView that is always on the top of throughout the entire app 如何将后置摄像头实时视频提要显示到UIImageView? - How do I display the back camera live video feed to an UIImageView? 如何使用 Swift 4 在 ARKit 中实现实时相机滤镜? - How to implement Live camera filter in ARKit with Swift 4? 我如何在swift中为iphone应用程序创建全局服务 - how do i create global service for iphone app in swift 使用 AVAssetWriter 和 CoreML 的相机上的 FPS 不一致 - FPS not consistent on Camera using AVAssetWriter and CoreML
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM