Unable to scan barcode from an Image using vision

Hello, I have been working to try to create a scanner to scan a PDF417 barcode from your photos library for a few days now and have come to a dead end. Every time that I run my function on the photo, my array of observations always returns as []. This example is me trying to use it with an automatic generated image because I think that if it works with this, it will work with a real screenshot. That being said, I have already tried with all sorts of images that aren't pre-generated, and they, still, have failed to work. Code below:

Calling the function

createVisionRequest(image: generatePDF417Barcode(from: "71238-12481248-128035-40239431")!)

Creating the Barcode:

static func generatePDF417Barcode(from key: String) -> UIImage? {
    let data = key.data(using: .utf8)!
    let filter = CIFilter.pdf417BarcodeGenerator()
    filter.message = data
    filter.rows = 7
    let transform = CGAffineTransform(scaleX: 3, y: 4)

    if let outputImage = filter.outputImage?.transformed(by: transform) {
            let context = CIContext()
            if let cgImage = context.createCGImage(outputImage, from: outputImage.extent) {
                return UIImage(cgImage: cgImage)
            }
        }
    
    return nil   
}

Main function for scanning the barcode:

static func desynthesizeIDScreenShot(from image: UIImage, completion: @escaping (String?) -> Void) {
        guard let ciImage = CIImage(image: image) else {
            print("Empty image")
            return
        }
        let imageRequestHandler = VNImageRequestHandler(ciImage: ciImage, orientation: .up)
        
        let request = VNDetectBarcodesRequest { (request,error) in
            guard error == nil else {
                completion(nil)
                return
            }
            
            guard let observations = request.results as? [VNDetectedObjectObservation] else {
                completion(nil)
                return
            }
            request.revision = VNDetectBarcodesRequestRevision2
            let result = (observations.first as? VNBarcodeObservation)?.payloadStringValue
            print("Observations", observations)
            if let result {
            completion(result)
                print()
                print(result)
            } else {
                print(error?.localizedDescription) //returns nil
                completion(nil)
                print()
                print(result)
                print()
            }
            
            
        }
        request.symbologies = [VNBarcodeSymbology.pdf417]
        
        try? imageRequestHandler.perform([request])   
    }

Thanks!

Replies

I would start by modifying an existing sample app: https://developer.apple.com/documentation/vision/detecting_objects_in_still_images