-
-
Save sturdysturge/56577c39e8dc944132da5bed892c6803 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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