Skip to content

Commit

Permalink
Tracking rectangle
Browse files Browse the repository at this point in the history
  • Loading branch information
xzzz9097 committed Jun 27, 2017
1 parent 66f8519 commit c766c84
Showing 1 changed file with 26 additions and 2 deletions.
28 changes: 26 additions & 2 deletions Machine/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class ViewController: NSViewController, AVCaptureVideoDataOutputSampleBufferDele

var gradientLayer = CAGradientLayer()

var faceRect = NSView(frame: NSRect())

@IBOutlet weak var cameraView: NSView!

override func viewDidLoad() {
Expand All @@ -33,12 +35,18 @@ class ViewController: NSViewController, AVCaptureVideoDataOutputSampleBufferDele
override func viewDidLayout() {
super.viewDidLayout()

previewLayer.frame = self.cameraView.bounds;
previewLayer.frame = self.cameraView.bounds;
gradientLayer.frame = self.cameraView.bounds;
}

func prepareViews() {
cameraView.wantsLayer = true
faceRect.wantsLayer = true

faceRect.layer?.borderColor = NSColor.yellow.cgColor
faceRect.layer?.borderWidth = 1

cameraView.addSubview(faceRect)
}

func loadCaptureSession() {
Expand Down Expand Up @@ -114,7 +122,14 @@ class ViewController: NSViewController, AVCaptureVideoDataOutputSampleBufferDele
.flatMap({$0 as? VNFaceObservation})
.map({$0.boundingBox})

print(results)
for result in results {
DispatchQueue.main.async {
self.faceRect.frame = result.scaled(
width: self.cameraView.bounds.width,
height: self.cameraView.bounds.height
)
}
}
}

override var representedObject: Any? {
Expand All @@ -125,3 +140,12 @@ class ViewController: NSViewController, AVCaptureVideoDataOutputSampleBufferDele

}

extension CGRect {
func scaled(width: CGFloat, height: CGFloat) -> CGRect {
return CGRect(x: self.minX * width,
y: self.minY * height,
width: self.width * width,
height: self.height * height)
}
}

0 comments on commit c766c84

Please sign in to comment.