Advertisement
Guest User

Untitled

a guest
Dec 11th, 2024
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.64 KB | None | 0 0
  1. import SwiftUI
  2. import AVFoundation
  3. import Vision
  4.  
  5. class CameraViewModel: NSObject, ObservableObject {
  6.     @Published var detectedRects: [CGRect] = []
  7.     private let session = AVCaptureSession()
  8.     private var videoOutput = AVCaptureVideoDataOutput()
  9.     private var visionRequest = VNDetectRectanglesRequest()
  10.  
  11.     override init() {
  12.         super.init()
  13.         setupSession()
  14.         setupVision()
  15.     }
  16.  
  17.     func setupSession() {
  18.         guard let captureDevice = AVCaptureDevice.default(for: .video) else { return }
  19.         session.beginConfiguration()
  20.  
  21.         // Set up input
  22.         if let input = try? AVCaptureDeviceInput(device: captureDevice), session.canAddInput(input) {
  23.             session.addInput(input)
  24.         }
  25.  
  26.         // Set up output
  27.         videoOutput.setSampleBufferDelegate(self, queue: DispatchQueue(label: "camera.frame.processing"))
  28.         if session.canAddOutput(videoOutput) {
  29.             session.addOutput(videoOutput)
  30.         }
  31.  
  32.         session.commitConfiguration()
  33.     }
  34.  
  35.     func setupVision() {
  36.         visionRequest.minimumAspectRatio = 1.58 / 1.0
  37.         visionRequest.maximumAspectRatio = 1.6 / 1.0
  38.         visionRequest.minimumSize = 0.2 // Minimum size as a fraction of the image
  39.         visionRequest.maximumObservations = 1
  40.         visionRequest.quadratureTolerance = 45.0
  41.     }
  42.  
  43.     func startSession() {
  44.         session.startRunning()
  45.     }
  46.  
  47.     func stopSession() {
  48.         session.stopRunning()
  49.     }
  50. }
  51.  
  52. extension CameraViewModel: AVCaptureVideoDataOutputSampleBufferDelegate {
  53.     func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
  54.         guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }
  55.         let requestHandler = VNImageRequestHandler(cvPixelBuffer: pixelBuffer, options: [:])
  56.  
  57.         do {
  58.             try requestHandler.perform([visionRequest])
  59.             if let results = visionRequest.results as? [VNRectangleObservation] {
  60.                 DispatchQueue.main.async {
  61.                     self.detectedRects = results.map { observation in
  62.                         CGRect(
  63.                             x: observation.boundingBox.origin.x,
  64.                             y: 1 - observation.boundingBox.origin.y - observation.boundingBox.size.height,
  65.                             width: observation.boundingBox.size.width,
  66.                             height: observation.boundingBox.size.height
  67.                         )
  68.                     }
  69.                 }
  70.             }
  71.         } catch {
  72.             print("Error performing vision request: \(error)")
  73.         }
  74.     }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement