Skip to content

Instantly share code, notes, and snippets.

@sturdysturge
Last active May 9, 2022 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sturdysturge/56577c39e8dc944132da5bed892c6803 to your computer and use it in GitHub Desktop.
Save sturdysturge/56577c39e8dc944132da5bed892c6803 to your computer and use it in GitHub Desktop.
import SwiftUI
import AVFoundation.AVCaptureSession
import Vision.VNRequest
class DataModel: NSObject, ObservableObject, AVCaptureVideoDataOutputSampleBufferDelegate {
static let shared = DataModel(debugString: "")
@Published var session = AVCaptureSession()
@Published var debugString = ""
@Published var previewView = UIView(frame: CGRect(x: 0, y: 0, width: 375, height: 667))
var requests = [VNRequest]()
convenience init(debugString: String) {
self.init()
requests = [.fromFile(named: "MobileNet") { debugString in
self.debugString = debugString }]
session.setup(delegate: self)
session.startRunning()
}
func captureOutput(_ output: AVCaptureOutput,
didOutput sampleBuffer: CMSampleBuffer,
from connection: AVCaptureConnection) {
do {
if let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) {
try VNImageRequestHandler(
cvPixelBuffer: pixelBuffer,
orientation: .up,
options: [:])
.perform(DataModel.shared.requests)
}
} catch {
fatalError(error.localizedDescription)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment