Context
A SwiftUI app that uses the phone's camera to detect what hand sign I am doing in front of it and update a Text view in SwiftUI. The detection part is done in a UIViewController (primarily with Vision) and then that view is used in the main ContentView in SwiftUI. (UIViewControllerRepresentable)
Problem
I am able to print what hand sign I am doing in the front of the screen in the UIViewController, but not able to send that value to update the text label in SwiftUI.
Here are my three main code files that pertain to this issue:
ContentView.swift
struct ContentView: View {
@State var text = ""
var body: some View {
ZStack {
Color(hex: "3E065F")
.ignoresSafeArea()
VStack {
Text(text)
.foregroundColor(Color.white)
.font(.largeTitle)
MyCameraView(value: $text)
}
}
}
}
MyCameraView.swift
struct MyCameraView: UIViewControllerRepresentable {
@Binding var value: String
func makeUIViewController(context: Context) -> CameraViewController {
let cvc = CameraViewController()
return cvc
}
func updateUIViewController(
_ uiViewController: CameraViewController,
context: Context
) {
value = uiViewController.currText // Only Called Once!
}
}
CameraViewController.swift
final class CameraViewController: UIViewController,
AVCaptureVideoDataOutputSampleBufferDelegate {
var currText = "A"
...
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
...
guard let handSignsModel = try? VNCoreMLModel(for: SavedModel().model) else { print("Fail"); return }
let request = VNCoreMLRequest(model: handSignsModel) { (finishedRequest, err) in
guard let results = finishedRequest.results as? [VNClassificationObservation] else { return }
//print(results.first?.identifier)
self.currText = results.first!.identifier
print(self.currText)
}
try? VNImageRequestHandler(cvPixelBuffer: pixelBuffer, options: [:]).perform([request])
}
}
Please look at the print(self.currText)
statement in the last file. I want to pass that value (every frame) to update the text in the ContentView.
I tried to use updateUIViewController method in MyCameraView.swift, but it does not get called to update the text label every frame, only the first time it loads.