Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

superscape

macrumors 6502a
Original poster
Feb 12, 2008
937
223
East Riding of Yorkshire, UK
Hi,

I wonder if anyone here has experience of using the Vision Framework?

I'm hoping to be able to grab one or more barcodes out of a image. It seems to work fine for 2D codes like QR codes where I get two results if there are two QR codes (as I'd expect).

However, for 1D codes such as ITF14, UPC etc I get multiple results per code. It appears that for each code I'm getting an unpredictable number of multiple results. Each result is a thin rectangle through the code*. So, I am unable to say how many barcodes are in an image, or precisely where they are.

Has anyone here any experience of this? Any suggestions?

Thanks
r.



* See attached image. The semi-transparent red rectangles are the detected rectangles.



ps. Here's a copy of my code. Be nice - it's my first experiment with both Swift and Vision. ;-)

Code:
       let theImage=NSImage.init(named: NSImage.Name(rawValue: "ITF14"))
       imageView.image=theImage //show the image in an NSImageView
       let rootLayer=imageView.layer //the top level layer

        // Create a barcode detection-request
       let barcodeRequest = VNDetectBarcodesRequest(completionHandler: { request, error in
       guard let results = request.results else { return }

           // Loop through the results
           for result in results {

                if let barcode = result as? VNBarcodeObservation {

                   //make a new CALayer
                   let newLayer=CALayer()

                    newLayer.frame=NSMakeRect(
                       barcode.bottomLeft.x * (theImage?.size.width)!,
                       barcode.bottomLeft.y * (theImage?.size.height)!,
                       (barcode.bottomRight.x - barcode.bottomLeft.x) * (theImage?.size.width)!,
                       (barcode.topLeft.y - barcode.bottomLeft.y) * (theImage?.size.width)!
                   )

                    newLayer.backgroundColor=NSColor.init(red: 1, green: 0, blue: 0, alpha: 0.3).cgColor
                    rootLayer?.addSublayer(newLayer)
               }

            }

        })

       
       let cgImage = theImage?.cgImage(forProposedRect: nil, context: nil, hints: nil)
       let handler = VNImageRequestHandler(cgImage: cgImage!, options: [:])

        // Perform the barcode-request.
       guard let _ = try? handler.perform([barcodeRequest]) else {
           return print("Could not perform barcode-request!")
       }
 

Attachments

  • Screen Shot 2018-04-04 at 11.00.56.png
    Screen Shot 2018-04-04 at 11.00.56.png
    175.8 KB · Views: 87
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.