Add Rectangles Todo: fix rectangle high

This commit is contained in:
Simon Rieger 2023-12-15 20:58:20 +01:00
parent f4871473c4
commit b4ccf6315b

View file

@ -13,28 +13,46 @@ struct ContentView: View {
@State private var recognizedText: String = "" @State private var recognizedText: String = ""
@State private var isShowingPopup = false @State private var isShowingPopup = false
@State private var matchedLines: [String] = [] @State private var matchedLines: [String] = []
@State private var textRectangles: [RectangleData] = []
var body: some View { var body: some View {
VStack { ZStack {
CameraView(recognizedText: $recognizedText, isShowingPopup: $isShowingPopup, matchedLines: $matchedLines) CameraView(recognizedText: $recognizedText, isShowingPopup: $isShowingPopup, matchedLines: $matchedLines, textRectangles: $textRectangles)
.edgesIgnoringSafeArea(.all) .edgesIgnoringSafeArea(.all)
}
.alert(isPresented: $isShowingPopup) { // Overlay für die Rechtecke
Alert( ForEach(textRectangles) { rectangleData in
title: Text("Text Match Found"), Rectangle()
message: Text("Found matching text: \(matchedLines.joined(separator: "\n"))"), .stroke(Color.green, lineWidth: 2)
dismissButton: .default(Text("OK")) { .frame(width: rectangleData.rect.width, height: rectangleData.rect.height)
// Hier kannst du zusätzlichen Code für das Schließen des Popups hinzufügen .position(x: rectangleData.rect.midX, y: rectangleData.rect.midY)
} .rotationEffect(Angle(degrees: 270))
)
} }
//Text("Recognized Text: \(recognizedText)")
// .padding()
} }
.alert(isPresented: $isShowingPopup) {
Alert(
title: Text("Text Match Found"),
message: Text("Found matching text: \(matchedLines.joined(separator: "\n"))"),
dismissButton: .default(Text("OK")) {
// Hier kannst du zusätzlichen Code für das Schließen des Popups hinzufügen
}
)
}
}
}
struct RectangleData: Identifiable {
var id: UUID
var rect: CGRect
} }
struct CameraView: UIViewControllerRepresentable { struct CameraView: UIViewControllerRepresentable {
@Binding var recognizedText: String @Binding var recognizedText: String
@Binding var isShowingPopup: Bool @Binding var isShowingPopup: Bool
@Binding var matchedLines: [String] @Binding var matchedLines: [String]
@Binding var textRectangles: [RectangleData]
class Coordinator: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate { class Coordinator: NSObject, AVCaptureVideoDataOutputSampleBufferDelegate {
var parent: CameraView var parent: CameraView
@ -57,8 +75,15 @@ struct CameraView: UIViewControllerRepresentable {
observation.topCandidates(1).first?.string observation.topCandidates(1).first?.string
}.joined(separator: "\n") }.joined(separator: "\n")
let rectangles = results.map { observation in
let boundingBox = observation.boundingBox
let transformedRect = self.parent.transformRect(boundingBox)
return RectangleData(id: UUID(), rect: transformedRect)
}
DispatchQueue.main.async { DispatchQueue.main.async {
self.parent.recognizedText = text self.parent.recognizedText = text
self.parent.textRectangles = rectangles
print(text) print(text)
self.parent.checkForRegexMatch(text) self.parent.checkForRegexMatch(text)
} }
@ -123,12 +148,12 @@ struct CameraView: UIViewControllerRepresentable {
isShowingPopup = !matchedLines.isEmpty isShowingPopup = !matchedLines.isEmpty
} }
func percentageMatch(text: String, pattern: String) -> Double? { func percentageMatch(text: String, pattern: String) -> Double? {
guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else { guard let regex = try? NSRegularExpression(pattern: pattern, options: []) else {
return nil return nil
} }
let range = NSRange(location: 0, length: text.utf16.count) let range = NSRange(location: 0, length: text.utf16.count)
if let match = regex.firstMatch(in: text, options: [], range: range) { if let match = regex.firstMatch(in: text, options: [], range: range) {
let matchLength = match.range.length let matchLength = match.range.length
@ -137,7 +162,15 @@ struct CameraView: UIViewControllerRepresentable {
print("\(text) Match Percentage: \(matchPercentage)%") print("\(text) Match Percentage: \(matchPercentage)%")
return matchPercentage return matchPercentage
} }
return 0.0 return 0.0
} }
func transformRect(_ rect: CGRect) -> CGRect {
let previewSize = UIScreen.main.bounds.size
var transformedRect = VNImageRectForNormalizedRect(rect, Int(previewSize.width), Int(previewSize.height))
transformedRect.origin.x = previewSize.width - transformedRect.origin.x - transformedRect.size.width
return transformedRect
}
} }