Skip to content

Instantly share code, notes, and snippets.

@anupamchugh
Created November 21, 2019 20:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anupamchugh/a23c1694eb95300027e3478a8e516543 to your computer and use it in GitHub Desktop.
Save anupamchugh/a23c1694eb95300027e3478a8e516543 to your computer and use it in GitHub Desktop.
private let workQueue = DispatchQueue(label: "VisionRequest", qos: .userInitiated, attributes: [], autoreleaseFrequency: .workItem)
@objc func startVisionRequest(sender: UIButton){
var bestFrameIndex = -1
var bestThreshold : Float = 0.0
workQueue.async {
for i in 0..<self.data.count
{
guard let cgImage = self.data[i].frameImage.cgImage else {return}
let request = VNDetectFaceCaptureQualityRequest()
let requestHandler = VNImageRequestHandler(cgImage: cgImage, options: [:])
do{
try requestHandler.perform([request])
if let faceObservation = request.results?.first as? VNFaceObservation{
if let faceCaptureQuality = faceObservation.faceCaptureQuality{
self.data[i].faceQualityValue = "\(faceCaptureQuality)"
if faceCaptureQuality > bestThreshold{
bestThreshold = faceCaptureQuality
bestFrameIndex = i
}
}
}
else{
self.data[i].faceQualityValue = "0.00"
}
}catch(let error){
print(error.localizedDescription)
}
}
DispatchQueue.main.async {
self.collectionView.reloadData()
if bestFrameIndex != -1{
self.imageView?.image = self.data[bestFrameIndex].frameImage
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment