This file contains hidden or 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 Foundation | |
/// An abstract class that makes building simple asynchronous operations easy. | |
/// Subclasses must implement `execute()` to perform any work and call | |
/// `finish()` when they are done. All `NSOperation` work will be handled | |
/// automatically. | |
open class AsynchronousOperation: Operation { | |
// MARK: - Properties |
This file contains hidden or 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 ARKit | |
class ARViewController: UIViewController { | |
// MARK: - Variables | |
let sceneView = ARSCNView() | |
// MARK: - Lifecycle | |
override func loadView() { |
This file contains hidden or 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 ARKit | |
class ARViewController: UIViewController, ARSessionDelegate { | |
// MARK: - Variables | |
let sceneView = ARSCNView() | |
// MARK: - Lifecycle | |
override func loadView() { |
This file contains hidden or 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
// MARK: - ARSessionDelegate | |
var currentBuffer: CVPixelBuffer? | |
func session(_: ARSession, didUpdate frame: ARFrame) { | |
// We return early if currentBuffer is not nil or the tracking state of camera is not normal | |
guard currentBuffer == nil, case .normal = frame.camera.trackingState else { | |
return | |
} |
This file contains hidden or 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
private lazy var predictionRequest: VNCoreMLRequest = { | |
// Load the ML model through its generated class and create a Vision request for it. | |
do { | |
let model = try VNCoreMLModel(for: HandModel().model) | |
let request = VNCoreMLRequest(model: model) | |
// This setting determines if images are scaled or cropped to fit our 224x224 input size. Here we try scaleFill so we don't cut part of the image. | |
request.imageCropAndScaleOption = VNImageCropAndScaleOption.scaleFill | |
return request | |
} catch { |
This file contains hidden or 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
let visionQueue = DispatchQueue(label: "com.viseo.ARML.visionqueue") | |
private func startDetection() { | |
// To avoid force unwrap in VNImageRequestHandler | |
guard let buffer = currentBuffer else { return } | |
// Right orientation because the pixel data for image captured by an iOS device is encoded in the camera sensor's native landscape orientation | |
let requestHandler = VNImageRequestHandler(cvPixelBuffer: buffer, orientation: .right) | |
// We perform our CoreML Requests asynchronously. |
This file contains hidden or 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 CoreML | |
import Vision | |
public class HandDetector { | |
// MARK: - Variables | |
private let visionQueue = DispatchQueue(label: "com.viseo.ARML.visionqueue") | |
private lazy var predictionRequest: VNCoreMLRequest = { | |
// Load the ML model through its generated class and create a Vision request for it. |
This file contains hidden or 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
var previewView = UIImageView() | |
// MARK: - Lifecycle | |
override func loadView() { | |
super.loadView() | |
view = sceneView | |
// Create a session configuration |
This file contains hidden or 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
// MARK: - Private functions | |
let handDetector = HandDetector() | |
private func startDetection() { | |
// To avoid force unwrap in VNImageRequestHandler | |
guard let buffer = currentBuffer else { return } | |
handDetector.performDetection(inputBuffer: buffer) { outputBuffer, _ in | |
// Here we are on a background thread |
This file contains hidden or 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
// Create a session configuration | |
let configuration = ARWorldTrackingConfiguration() | |
// Enable Horizontal plane detection | |
configuration.planeDetection = .horizontal | |
// We want to receive the frames from the video | |
sceneView.session.delegate = self | |
// Run the session with the configuration |
OlderNewer