I am collecting depth data to generate depth maps for my app. The depth data worked properly at first. However, after a few rounds of testing (while the code has not been changed), the program seems to be unable to collect proper depth data as when I print the depth data, it says
<AVDepthData: 0x2801dc610 0x0 (low/rel) (unfiltered)>
, which is clearly not a valid AVDepthData to use.
Following is the chunk of code:
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
if error != nil {
return
}
guard let imageData = photo.fileDataRepresentation() else {return}
self.picData = imageData
let depthData = photo.depthData!
DispatchQueue.main.async {
let depthPixelBuffer = depthData.depthDataMap
if !self.photoDepthConverter.isPrepared {
var depthFormatDescription: CMFormatDescription?
CMVideoFormatDescriptionCreateForImageBuffer(allocator: kCFAllocatorDefault, imageBuffer: depthPixelBuffer, formatDescriptionOut: &depthFormatDescription)
if let unwrappedDepthFormatDescription = depthFormatDescription {
self.photoDepthConverter.prepare(with: unwrappedDepthFormatDescription, outputRetainedBufferCountHint: 3)
}
}
guard let convertedDepthPixelBuffer = self.photoDepthConverter.render(pixelBuffer: depthPixelBuffer) else {
print("Unable to convert depth pixel buffer")
return
}
let greyImage = UIImage.init(pixelBuffer: convertedDepthPixelBuffer)
self.depthImage = greyImage!
}
}
Note: it is not a problem that always occurs. When I change the ios deployed version, initially, the problem seems to be solved, but when I cleaned and built again, the problem reoccurs.
I also tried to mode the declaration for depthData to be above imageData. Again, initially the problem seems to be solved, but after a feel testing trials, the problem reoccurs.