Created
June 22, 2020 22:58
-
-
Save noppefoxwolf/54f03f38d8258a0b36f47c0ff6069771 to your computer and use it in GitHub Desktop.
VNDetectHumanHandPoseRequest
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 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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
2Dだった~~