Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vision framework barcode detection region of interest not working

I am trying to decode barcodes that appear on a region of interest, that is 80% of the screen width and 20% of the screen height and centered on both directions (blue rectangle).

enter image description here

The camera pixel buffer is rotated right.

This is what Apple has to say about this orientation:

The (x,y) pixel coordinates of the origin point (0,0) represent the top row and rightmost column, respectively. Pixel (x,y) positions increase top-to-bottom, right-to-left. If an image is encoded with this orientation, then displayed by software unaware of orientation metadata, the image appears to be rotated 90° counter-clockwise. (That is, to present the image in its intended orientation, you must rotate it 90° clockwise.)

So, when I define the region of interest of my VNDetectBarcodesRequest I do like this:

  lazy var barcodeRequest: VNDetectBarcodesRequest = {
    let barcodeRequest = VNDetectBarcodesRequest {[weak self] request, error in
      guard error == nil else {
        print ("ERROR")
        return
      }
      self?.classification(request)
    }

    barcodeRequest.regionOfInterest = CGRect(x: 0.1,
                                             y: 0.4,
                                             width: 0.9,
                                             height: 0.6)

If the bar code is inside the blue area and at any point above that, including anywhere on the area at the top of the blue area, it will detect. If the barcode is down the blue area, it will not detect anything.

like image 544
Duck Avatar asked Oct 27 '25 06:10

Duck


2 Answers

Just making sure, if you look at regionOfInterest, the documentation says:

The rectangle is normalized to the dimensions of the processed image. Its origin is specified relative to the image's lower-left corner.

So the origin (0,0) is at the bottom left. With your current CGRect,

CGRect(x: 0.1,
       y: 0.4,
       width: 0.9,
       height: 0.6)

you are getting the expected result - "If the bar code is inside the blue area and at any point above that, including anywhere on the area at the top of the blue area, it will detect."

All you need to do is change the height from 0.6 to 0.2. You will want:

barcodeRequest.regionOfInterest = CGRect(x: 0.1,
                                         y: 0.4,
                                         width: 0.9,
                                         height: 0.2) /// your height is wrong
like image 58
aheze Avatar answered Oct 29 '25 01:10

aheze


Just to chime in here for added clarity, cuz this was also tripping me up.

The documentation for regionOfInterest says:

The default value is { { 0, 0 }, { 1, 1 } }

Which I was also confusing for 2 points (a bottom left corner and a top right corner). But that last pair is supposed to be the normalized width and height; not a normalized coordinate.

// ❌
request.regionOfInterest = CGRect(x: 0.1, y: 0.4, width: 0.9, height: 0.6)

// ✔️
request.regionOfInterest = CGRect(x: 0.1, y: 0.4, width: 0.8, height: 0.2)
like image 24
Peter Parker Avatar answered Oct 28 '25 23:10

Peter Parker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!