Posts

Post marked as solved
2 Replies
1.7k Views
Hello, I am quite new to Swift, and trying to understand, how i can programmatically create an UI Button, that can execute code after the button has been pressed, and stop the code when the button is pressed again (with if else statements). I have studied the documentation by Apple, but can't quite get how you can achieve this.
Posted
by Ricards97.
Last updated
.
Post not yet marked as solved
1 Replies
1.8k Views
Hello, I am currently working on figuring out how to export the TrueDepth camera Depth data from the sample code found here - https://developer.apple.com/documentation/avfoundation/cameras_and_media_capture/streaming_depth_data_from_the_truedepth_camera. I want to convert AVDepthData object to a list of real world XYZ coordinate, that i could export to a .txt file. After searching the developer forum, I stumbled upon this arcticle - https://developer.apple.com/forums/thread/106438?answerId=345004022#345004022. The following code: privatefunc getPoints(avDepthData: AVDepthData)->Array{ &#9;&#9;&#9;&#9;let depthData = avDepthData.converting(toDepthDataType: kCVPixelFormatType_DepthFloat32) &#9;&#9;&#9;&#9;guard let intrinsicMatrix = avDepthData.cameraCalibrationData?.intrinsicMatrix, &#9;&#9;&#9;&#9;} &#9;&#9;&#9; &#9;&#9;&#9;&#9;CVPixelBufferLockBaseAddress(depthDataMap, CVPixelBufferLockFlags(rawValue: 0)) &#9;&#9;&#9; &#9;&#9;&#9;&#9;let width = CVPixelBufferGetWidth(depthDataMap) &#9;&#9;&#9;&#9;let height = CVPixelBufferGetHeight(depthDataMap) &#9;&#9;&#9; &#9;&#9;&#9;&#9;var points = Array() &#9;&#9;&#9;&#9;let focalX = Float(width) * (intrinsicMatrix[0][0] / PHOTO_WIDTH) &#9;&#9;&#9;&#9;let focalY = Float(height) * ( intrinsicMatrix[1][1] / PHOTO_HEIGHT) &#9;&#9;&#9;&#9;let principalPointX = Float(width) * (intrinsicMatrix[2][0] / PHOTO_WIDTH) &#9;&#9;&#9;&#9;let principalPointY = Float(height) * (intrinsicMatrix[2][1] / PHOTO_HEIGHT) &#9;&#9;&#9;&#9;for y in 0 ..< height{ &#9;&#9;&#9;&#9;&#9;&#9;for x in 0 ..< width{ &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;guard let Z = getDistance(at: CGPoint(x: x, y: y) , depthMap: depthDataMap, depthWidth: width, depthHeight: height) else { &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;continue &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;&#9;&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;let X = (Float(x) - principalPointX) * Z / focalX &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;let Y = (Float(y) - principalPointY) * Z / focalY &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;points.append(PointXYZ(x: X, y: Y, z: Z)) &#9;&#9;&#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;} &#9;&#9;&#9;&#9;CVPixelBufferUnlockBaseAddress(depthDataMap, CVPixelBufferLockFlags(rawValue: 0)) &#9;&#9;&#9; &#9;&#9;&#9;&#9;return points &#9;&#9;} The problem with this is, that the sample code doesn't implement PHOTOWIDTH and PHOTOHEIGHT. Also, the rectification of the depth map was not posted, in my scenario it is not needed at the moment. Maybe someone can help to manipulate this code in order to get the values ?
Posted
by Ricards97.
Last updated
.