Created a library of images which do not contain barcodes, or contain barcodes we don't yet support. Wrote a new unit test designed to identify false positives, and set a benchmark which we can improve over time. Out of these 26 images, tested in all four rotations, we currently find 44/104 false positives. The Code 39 decoder in particular seems to be too lenient.
git-svn-id: https://zxing.googlecode.com/svn/trunk@436 59b500cc-1b3d-0410-9834-0bbf25fbcc57
BIN
core/test/data/blackbox/falsepositives/01.jpg
Normal file
After Width: | Height: | Size: 103 KiB |
BIN
core/test/data/blackbox/falsepositives/02.jpg
Normal file
After Width: | Height: | Size: 193 KiB |
BIN
core/test/data/blackbox/falsepositives/03.jpg
Normal file
After Width: | Height: | Size: 201 KiB |
BIN
core/test/data/blackbox/falsepositives/04.jpg
Normal file
After Width: | Height: | Size: 189 KiB |
BIN
core/test/data/blackbox/falsepositives/05.jpg
Normal file
After Width: | Height: | Size: 188 KiB |
BIN
core/test/data/blackbox/falsepositives/06.jpg
Normal file
After Width: | Height: | Size: 131 KiB |
BIN
core/test/data/blackbox/falsepositives/07.jpg
Normal file
After Width: | Height: | Size: 100 KiB |
BIN
core/test/data/blackbox/falsepositives/08.jpg
Normal file
After Width: | Height: | Size: 73 KiB |
BIN
core/test/data/blackbox/falsepositives/09.jpg
Normal file
After Width: | Height: | Size: 243 KiB |
BIN
core/test/data/blackbox/falsepositives/10.jpg
Normal file
After Width: | Height: | Size: 210 KiB |
BIN
core/test/data/blackbox/falsepositives/11.jpg
Normal file
After Width: | Height: | Size: 164 KiB |
BIN
core/test/data/blackbox/falsepositives/12.jpg
Normal file
After Width: | Height: | Size: 175 KiB |
BIN
core/test/data/blackbox/falsepositives/13.jpg
Normal file
After Width: | Height: | Size: 105 KiB |
BIN
core/test/data/blackbox/falsepositives/14.jpg
Normal file
After Width: | Height: | Size: 132 KiB |
BIN
core/test/data/blackbox/falsepositives/15.jpg
Normal file
After Width: | Height: | Size: 181 KiB |
BIN
core/test/data/blackbox/falsepositives/16.jpg
Normal file
After Width: | Height: | Size: 202 KiB |
BIN
core/test/data/blackbox/falsepositives/17.jpg
Normal file
After Width: | Height: | Size: 158 KiB |
BIN
core/test/data/blackbox/falsepositives/18.jpg
Normal file
After Width: | Height: | Size: 138 KiB |
BIN
core/test/data/blackbox/falsepositives/19.jpg
Normal file
After Width: | Height: | Size: 207 KiB |
BIN
core/test/data/blackbox/falsepositives/20.jpg
Normal file
After Width: | Height: | Size: 201 KiB |
BIN
core/test/data/blackbox/falsepositives/21.jpg
Normal file
After Width: | Height: | Size: 142 KiB |
BIN
core/test/data/blackbox/falsepositives/22.jpg
Normal file
After Width: | Height: | Size: 126 KiB |
BIN
core/test/data/blackbox/falsepositives/23.jpg
Normal file
After Width: | Height: | Size: 134 KiB |
BIN
core/test/data/blackbox/falsepositives/24.jpg
Normal file
After Width: | Height: | Size: 104 KiB |
BIN
core/test/data/blackbox/falsepositives/25.jpg
Normal file
After Width: | Height: | Size: 139 KiB |
0
core/test/data/blackbox/falsepositives/26.jpg
Normal file
|
@ -44,7 +44,7 @@ import java.util.Vector;
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractBlackBoxTestCase extends TestCase {
|
public abstract class AbstractBlackBoxTestCase extends TestCase {
|
||||||
|
|
||||||
private static final Hashtable<DecodeHintType, Object> TRY_HARDER_HINT;
|
protected static final Hashtable<DecodeHintType, Object> TRY_HARDER_HINT;
|
||||||
static {
|
static {
|
||||||
TRY_HARDER_HINT = new Hashtable<DecodeHintType, Object>();
|
TRY_HARDER_HINT = new Hashtable<DecodeHintType, Object>();
|
||||||
TRY_HARDER_HINT.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
|
TRY_HARDER_HINT.put(DecodeHintType.TRY_HARDER, Boolean.TRUE);
|
||||||
|
@ -97,11 +97,19 @@ public abstract class AbstractBlackBoxTestCase extends TestCase {
|
||||||
testResults.add(new TestResult(mustPassCount, rotation));
|
testResults.add(new TestResult(mustPassCount, rotation));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected File[] getImageFiles() {
|
||||||
|
assertTrue("Please run from the 'core' directory", testBase.exists());
|
||||||
|
return testBase.listFiles(IMAGE_NAME_FILTER);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Reader getReader() {
|
||||||
|
return barcodeReader;
|
||||||
|
}
|
||||||
|
|
||||||
public void testBlackBox() throws IOException {
|
public void testBlackBox() throws IOException {
|
||||||
assertFalse(testResults.isEmpty());
|
assertFalse(testResults.isEmpty());
|
||||||
assertTrue("Please run from the 'core' directory", testBase.exists());
|
|
||||||
|
|
||||||
File[] imageFiles = testBase.listFiles(IMAGE_NAME_FILTER);
|
File[] imageFiles = getImageFiles();
|
||||||
int[] passedCounts = new int[testResults.size()];
|
int[] passedCounts = new int[testResults.size()];
|
||||||
for (File testImage : imageFiles) {
|
for (File testImage : imageFiles) {
|
||||||
System.out.println("Starting " + testImage.getAbsolutePath());
|
System.out.println("Starting " + testImage.getAbsolutePath());
|
||||||
|
@ -185,7 +193,7 @@ public abstract class AbstractBlackBoxTestCase extends TestCase {
|
||||||
return result.toString();
|
return result.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static BufferedImage rotateImage(BufferedImage original, float degrees) {
|
protected static BufferedImage rotateImage(BufferedImage original, float degrees) {
|
||||||
if (degrees == 0.0f) {
|
if (degrees == 0.0f) {
|
||||||
return original;
|
return original;
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -0,0 +1,96 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2008 Google Inc.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.google.zxing.common;
|
||||||
|
|
||||||
|
import com.google.zxing.MonochromeBitmapSource;
|
||||||
|
import com.google.zxing.MultiFormatReader;
|
||||||
|
import com.google.zxing.ReaderException;
|
||||||
|
import com.google.zxing.Result;
|
||||||
|
import com.google.zxing.client.j2se.BufferedImageMonochromeBitmapSource;
|
||||||
|
|
||||||
|
import javax.imageio.ImageIO;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This test ensures that random, noisy, or unsupported barcode images do not decode.
|
||||||
|
*
|
||||||
|
* @author dswitkin@google.com (Daniel Switkin)
|
||||||
|
*/
|
||||||
|
public final class FalsePositivesBlackBoxTestCase extends AbstractBlackBoxTestCase {
|
||||||
|
|
||||||
|
// This number should be reduced as we get better at rejecting false positives.
|
||||||
|
private static final int FALSE_POSITIVES_ALLOWED = 44;
|
||||||
|
|
||||||
|
// Use the multiformat reader to evaluate all decoders in the system.
|
||||||
|
public FalsePositivesBlackBoxTestCase() {
|
||||||
|
super(new File("test/data/blackbox/falsepositives"), new MultiFormatReader(), null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testBlackBox() throws IOException {
|
||||||
|
File[] imageFiles = getImageFiles();
|
||||||
|
int falsePositives = 0;
|
||||||
|
for (File testImage : imageFiles) {
|
||||||
|
System.out.println("Starting " + testImage.getAbsolutePath());
|
||||||
|
|
||||||
|
// Try all four rotations, since many of the test images don't have a notion of up, and we
|
||||||
|
// want to be as robust as possible.
|
||||||
|
BufferedImage image = ImageIO.read(testImage);
|
||||||
|
for (int x = 0; x < 4; x++) {
|
||||||
|
if (!checkForFalsePositives(image, x * 90.0f)) {
|
||||||
|
falsePositives++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("Found " + falsePositives + " false positives (" + FALSE_POSITIVES_ALLOWED +
|
||||||
|
" max)");
|
||||||
|
assertTrue("Too many false positives found", falsePositives <= FALSE_POSITIVES_ALLOWED);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Make sure ZXing does NOT find a barcode in the image.
|
||||||
|
*
|
||||||
|
* @param image The image to test
|
||||||
|
* @param rotationInDegrees The amount of rotation to apply
|
||||||
|
* @return true if nothing found, false if a non-existant barcode was detected
|
||||||
|
*/
|
||||||
|
private boolean checkForFalsePositives(BufferedImage image, float rotationInDegrees) {
|
||||||
|
BufferedImage rotatedImage = rotateImage(image, rotationInDegrees);
|
||||||
|
MonochromeBitmapSource source = new BufferedImageMonochromeBitmapSource(rotatedImage);
|
||||||
|
Result result;
|
||||||
|
try {
|
||||||
|
result = getReader().decode(source);
|
||||||
|
System.out.println("Found false positive: '" + result.getText() + "' with format '" +
|
||||||
|
result.getBarcodeFormat() + "' (rotation: " + rotationInDegrees + ')');
|
||||||
|
return false;
|
||||||
|
} catch (ReaderException re) {
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try "try harder" mode
|
||||||
|
try {
|
||||||
|
result = getReader().decode(source, TRY_HARDER_HINT);
|
||||||
|
System.out.println("Try harder found false positive: '" + result.getText() + "' with format '" +
|
||||||
|
result.getBarcodeFormat() + "' (rotation: " + rotationInDegrees + ')');
|
||||||
|
return false;
|
||||||
|
} catch (ReaderException re) {
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|