Advertisement
DeEskalator

Untitled

Jun 19th, 2025
424
0
18 hours
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.45 KB | None | 0 0
  1. /// Process an image and return the first table detected
  2.     func extractTable(from image: Data) async throws -> DocumentObservation.Container.Table {
  3.        
  4.         // The Vision request.
  5.         let request = RecognizeDocumentsRequest()
  6.        
  7.         // Perform the request on the image data and return the results.
  8.         let observations = try await request.perform(on: image)
  9.  
  10.         // Get the first observation from the array.
  11.         guard let document = observations.first?.document else {
  12.             throw AppError.noDocument
  13.         }
  14.        
  15.         // Extract the first table detected.
  16.         guard let table = document.tables.first else {
  17.             throw AppError.noTable
  18.         }
  19.        
  20.         return table
  21.     }
  22.    
  23.     /// Extract name, email addresses, and phone number from a table into a list of contacts.
  24.     private func parseTable(_ table: DocumentObservation.Container.Table) {
  25.         var foundItems = [Contact]()
  26.        
  27.         // Iterate over each row in the table.
  28.         for row in table.rows {
  29.             // The contact name will be taken from the first column.
  30.             guard let firstCell = row.first else {
  31.                 continue
  32.             }
  33.             // Extract the text content from the transcript.
  34.             let name = firstCell.content.text.transcript
  35.            
  36.             // Look for emails and phone numbers in the remaining cells.
  37.             var detectedPhone: String? = nil
  38.             var detectedEmail: String? = nil
  39.            
  40.             for cell in row.dropFirst() {
  41.                 // Get all detected data in the cell, then match emails and phone numbers.
  42.                 let allDetectedData = cell.content.text.detectedData
  43.                 for data in allDetectedData {
  44.                     switch data.match.details {
  45.                     case .emailAddress(let email):
  46.                         detectedEmail = email.emailAddress
  47.                     case .phoneNumber(let phoneNumber):
  48.                         detectedPhone = phoneNumber.phoneNumber
  49.                     default:
  50.                         break
  51.                     }
  52.                 }
  53.             }
  54.             // Create a contact if an email was detected.
  55.             if let email = detectedEmail {
  56.                 let contact = Contact(name: name, email: email, phoneNumber: detectedPhone)
  57.                 foundItems.append(contact)
  58.             }
  59.         }
  60.         contacts = foundItems
  61.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement