If there is no text in the selected image or document then that

ghz 8months ago ⋅ 72 views

If there is no text in the selected image or document then that image or doc should not save in the local directory

I am creating a app for scanner, when i scan the image or doc then scanned image or doc is storing in the local directory. But issue is if doc / image doesn't contain any text means should not store in the local directory. How do i check before save the scanned image?

Actually I don't have any idea to achieve this requirement. For better memory utilisation I don't want to save the doc or image which doesn't contains text. i used this var textRecognitionRequest = VNRecognizeTextRequest() But this is used for live text scanning.`

Here is saveimage function:

func SaveImage() {
    let name = "img\(Date().timeIntervalSince1970).jpg"
    let fileManager = FileManager.default
    let destPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
    
    let fullDestPath = AppConstants.LivetextImageUrl.appendingPathComponent(name)
    let fullDestPathString = fullDestPath.path
    
    //  let imageData = image.jpegData(compressionQuality: 0.8)
    let imageData = self.image.pngData()
    fileManager.createFile(atPath: fullDestPathString, contents: imageData, attributes: nil)
    if fileManager.fileExists(atPath: fullDestPathString) {
        
    }else {
        
    }
}

Thank you in advance for any help

Answers

To achieve your requirement of only saving scanned images or documents that contain text, you can use OCR (Optical Character Recognition) technology to analyze the content of the scanned image or document and determine if it contains text. There are several OCR libraries available for iOS development that you can integrate into your app to perform text recognition on images.

Here's an approach you can take to implement this in your app:

  1. Integrate an OCR library: Choose an OCR library that supports text recognition on images. Some popular OCR libraries for iOS development include Tesseract-OCR, Vision framework (available in iOS 13 and later), and Google Mobile Vision.

  2. Perform text recognition: After scanning an image or document, pass it to the OCR library to perform text recognition. The library will analyze the content of the image and extract any text present in it.

  3. Check if text exists: Once the OCR process is complete, check if any text was successfully extracted from the image. If text is detected, proceed to save the image to the local directory. Otherwise, discard the image.

Here's an example of how you can modify your SaveImage function to incorporate text recognition using the Vision framework:

import Vision

func SaveImage() {
    guard let imageData = self.image.pngData() else {
        return
    }
    
    let image = UIImage(data: imageData)!
    
    // Create a request handler for Vision requests
    let requestHandler = VNImageRequestHandler(cgImage: image.cgImage!, options: [:])
    
    // Create a text recognition request
    let textRecognitionRequest = VNRecognizeTextRequest { request, error in
        guard let observations = request.results as? [VNRecognizedTextObservation] else {
            // Text recognition failed
            return
        }
        
        // Check if any text was recognized
        if observations.isEmpty {
            // No text detected, do not save the image
            return
        }
        
        // Save the image to the local directory
        let name = "img\(Date().timeIntervalSince1970).jpg"
        let fileManager = FileManager.default
        let destPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
        let fullDestPath = URL(fileURLWithPath: destPath).appendingPathComponent(name)
        
        do {
            try imageData.write(to: fullDestPath)
            print("Image saved successfully")
        } catch {
            print("Error saving image: \(error.localizedDescription)")
        }
    }
    
    // Perform text recognition on the image
    do {
        try requestHandler.perform([textRecognitionRequest])
    } catch {
        print("Error performing text recognition: \(error.localizedDescription)")
    }
}

This modified function performs text recognition using the Vision framework on the scanned image and only saves the image to the local directory if text is detected. Otherwise, it discards the image. Make sure to handle any errors that may occur during text recognition or image saving.