Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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