Swiftpack.co - labs-nomad/iOSRawCamera as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by labs-nomad.
labs-nomad/iOSRawCamera 1.8.5
Provides a simple API over AVFoundation to stream CVPixelBuffer's for Computer Vision tasks.
⭐️ 3
🕓 3 years ago
iOS
.package(url: "https://github.com/labs-nomad/iOSRawCamera.git", from: "1.8.5")

Install

If you dont have cocoapods installed on your maching

sudo gem install cocoapods

Once you have cocoapods installed cd into your project. If you don't have cocoapods initalized you can init like this

pod init

Now, open the generated podfile and add this project

pod 'iOSRawCamera'

Finally, install or update the dependencies.

pod install

Now use the workspace file to open xCode.

Basic Usage

Check camera authorization like this:

let authorizationController = CameraAuthorizationController()

guard authorizationController.isCameraAuthorized() == true else {
    return
}

Request camera authorization like this:

let cameraAuthorizationController = CameraAuthorizationController()
cameraAuthorizationController.requestCameraPermission { (didAuthorize) in
    switch didAuthorize {
    case true:
        break
    case false:
        break
    }
}

Once authorized, you can set up the camera synchronously or asynchronously

Sync:

let cameraController: CameraController = CameraController()
do{
    try cameraController.setUp()
}catch{

}

Async:

let cameraController: CameraController = CameraController()
let asyncController = CameraAsyncController()
asyncController.setUpAsync(cameraController: cameraController, finished: { (p_error) in
    if let error = p_error {
        callback?(error)
    }else {
        callback?(nil)
    }
})

Once the camera is set up you can start it like this:

cameraController.startRunning()

Then, all your objects that want to get CVPixelBuffer's can listen for the notification like this:

NotificationCenter.default.addObserver(self, selector: #selector(self.newBuffer(_:)), name: NewCameraBufferNotification, object: nil)
@objc func newBuffer(_ notification: Notification) {
    let buffer = notification.object as! CVPixelBuffer
    let w = Int32(CVPixelBufferGetWidth(buffer))
    let h = Int32(CVPixelBufferGetHeight(buffer))
    let opaque = UnsafeMutableRawPointer(Unmanaged.passRetained(buffer).toOpaque())
    CVPixelBufferLockBaseAddress(buffer, .readOnly)
    //Do inference.
    CVPixelBufferUnlockBaseAddress(buffer, .readOnly)
}

GitHub

link
Stars: 3
Last commit: 3 years ago
Advertisement: IndiePitcher.com - Cold Email Software for Startups

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics