Skip to content

Instantly share code, notes, and snippets.

@noppefoxwolf
Created June 22, 2020 22:58
Show Gist options
  • Save noppefoxwolf/54f03f38d8258a0b36f47c0ff6069771 to your computer and use it in GitHub Desktop.
Save noppefoxwolf/54f03f38d8258a0b36f47c0ff6069771 to your computer and use it in GitHub Desktop.
VNDetectHumanHandPoseRequest
import UIKit
import AVFoundation
import Vision
class ViewController: UIViewController {
lazy var device = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .front)!
lazy var input = try! AVCaptureDeviceInput(device: device)
lazy var output = AVCaptureVideoDataOutput()
lazy var session = AVCaptureSession()
lazy var request: VNRequest = VNDetectHumanHandPoseRequest { (result, error) in
if let results = result.results as? [VNRecognizedPointsObservation] {
let points: [VNRecognizedPointKey : VNRecognizedPoint]? = try! results.first?.recognizedPoints(forGroupKey: .handLandmarkRegionKeyLittleFinger)
print(points![.handLandmarkKeyLittleMCP])
}
}
let displayLayer = AVSampleBufferDisplayLayer()
override func loadView() {
super.loadView()
view.layer.addSublayer(displayLayer)
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
displayLayer.frame = view.bounds
}
override func viewDidLoad() {
super.viewDidLoad()
output.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String : kCVPixelFormatType_32BGRA]
session.addInput(input)
session.addOutput(output)
output.setSampleBufferDelegate(self, queue: .main)
session.startRunning()
}
}
extension ViewController: AVCaptureVideoDataOutputSampleBufferDelegate {
func captureOutput(_ output: AVCaptureOutput, didDrop sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
}
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
try! VNImageRequestHandler(cmSampleBuffer: sampleBuffer, options: [:]).perform([request])
displayLayer.enqueue(sampleBuffer)
}
}
@noppefoxwolf
Copy link
Author

2Dだった~~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment