Created
November 21, 2019 20:19
-
-
Save anupamchugh/a23c1694eb95300027e3478a8e516543 to your computer and use it in GitHub Desktop.
This file contains 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 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