swiftswift5apple-vision

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.


Solution

  • 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