So, I have to scan different barcodes with various colours. For example, a yellow barcode on black background or yellow barcode on white background.
I don't have any issues with them being recognized by traditional linear and CCD barcode scanners. I have tried using Apple Vision framework but it doesn't work on them. They work perfectly fine on black barcodes with white background.
My barcodes are all Code 128 so I use this code for it:
var barcodeObservations: [String : VNBarcodeObservation] = [:]
for barcode in barcodes {
if let detectedBarcode = barcode as? VNBarcodeObservation {
if detectedBarcode.symbology == .code128 {
barcodeObservations[detectedBarcode.payloadStringValue!] = detectedBarcode
}
}
}
And in 'captureOutput' function under AVCaptureVideoDataOutputSampleBufferDelegate, I use this to filter my live feed as black and white which helps in the recognition of the golden barcode on silver background (The first image):
let context = CIContext(options: nil)
let currentFilter = CIFilter(name: "CIPhotoEffectMono")
currentFilter!.setValue(CIImage(cvImageBuffer: pixelBuffer), forKey: kCIInputImageKey)
let output = currentFilter!.outputImage!
context.render(output, to: pixelBuffer)
How can I make the Vision Framework detect barcodes with invert colors?
The 'CIColorInvert' filter doesn't work.
Edit: These are the barcodes:
Yes, but with an important note. Barcodes are typically printed with black bars on a white background. These colors can be changed, but caution should be taken to ensure the barcode is still readable by a barcode scanner.
There are many reasons barcodes might not scan and most of them can be boiled down to one of three things—your equipment isn't suited to your barcodes, your scanner isn't being operated properly or your barcode labels aren't suited to your application or environment.
How do I know which colours to use for my barcode? Does my barcode have to be black and white? While black bars on a white background are a very good combination, your barcode does not have to be black and white. The colour chosen must appear black under red light, so blue and green are possible options.
Not all barcode scanners can read all barcodes. Laser barcode scanners and linear imagers read only 1D barcode scanners. Imager (camera-based) 2D barcode scanners read all common 1D and 2D barcodes.
By default, Apple Vision, CoreML, ARKit and RealityKit frameworks are designed to detect barcodes that must be seen through a camera as a high-contrast black-and-white
images (with a predictable weights for channels: r=30%
, g=59%
, b=11%
). In your case a yellow barcode on a white background has the lowest contrast, so no one real barcode scanner can read it, including rgb camera feed for Vision.
Let's see what Best and Worst Colors for Barcode Labels article tells us:
One reason why barcodes can be hard to scan is
color
, or more specifically, the lack of contrast in colors. If there isn’t enough contrast between the background and bar colors, barcode scanners will have a hard time reading it.
Avoid the following colours' combination because they are in low-contrast greyscale spectre:
Although if you wanna successfully detect a chromatic barcode on a chromatic background using Vision, you definitely need to apply a grayscale filter to color CVPixelBuffer stream before Vision starts recognizing barcodes. For this use AVFoundation
and CoreImage
frameworks.
Please read these three posts to find out how you can do it:
Barcodes printed with metallic paints (gold, silver, copper, etc) are the worst instances for barcode readers. This is due to the fact that metallic paint catches reflections and it has environment lights' speculars. So barcodes printed with metallic paints are barely read.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With