Skip to content

Commit

Permalink
Added Guava, refactored NumberPlateDetectorImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
oskopek committed Apr 18, 2014
1 parent 30578c2 commit 01ec827
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 38 deletions.
5 changes: 5 additions & 0 deletions carcv-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@
<artifactId>commons-lang</artifactId>
<version>20030203.000129</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>13.0.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import javax.xml.parsers.ParserConfigurationException;

import com.google.common.collect.Multiset;
import com.google.common.collect.Multisets;
import com.google.common.collect.TreeMultiset;
import net.sf.javaanpr.imageanalysis.CarSnapshot;
import net.sf.javaanpr.intelligence.Intelligence;

Expand All @@ -36,29 +35,38 @@
import org.xml.sax.SAXException;

/**
* A Singleton implementation of <code>NumberPlateDetector</code> based on <a
* An implementation of <code>NumberPlateDetector</code> based on <a
* href="https://github.com/oskopek/javaanpr.git">JavaANPR</a>.
*
* <p>
* Make sure all images are loaded in advance!
*
*/
public class NumberPlateDetectorImpl extends NumberPlateDetector {

final private static Logger LOGGER = LoggerFactory.getLogger(NumberPlateDetectorImpl.class);

private static NumberPlateDetectorImpl detector = new NumberPlateDetectorImpl();

private NumberPlateDetectorImpl() {
private static NumberPlateDetectorImpl detector;
private Intelligence intel;

public NumberPlateDetectorImpl() throws IllegalStateException {
try {
intel = new Intelligence();
} catch (ParserConfigurationException | SAXException | IOException e) {
IllegalStateException ise = new IllegalStateException("Error occurred while initializing detector");
ise.addSuppressed(e);
throw ise;
}
}

/**
* Returns a reference to the static singleton instantiation of NumberPlateDetectorImpl
*
* @return reference to static NumberPlateDetectorImpl instance
* @return reference to static NumberPlateDetectorImpl instance, null if an error occurs during initialization
*/
public static NumberPlateDetectorImpl getInstance() {
public static NumberPlateDetectorImpl getInstance() throws IllegalStateException {
if (detector == null) {
detector = new NumberPlateDetectorImpl();
}
return detector;
}

Expand All @@ -69,22 +77,10 @@ public String detect(final List<? extends AbstractCarImage> images) {

@Override
public String detectPlateText(final List<? extends AbstractCarImage> images) {

Intelligence intel;
try {
intel = new Intelligence();
} catch (ParserConfigurationException | SAXException | IOException e) {
LOGGER.error("Error occured while detecting plate text!");
e.printStackTrace();
return null;
}

ArrayList<String> numberPlates = new ArrayList<>();

for (AbstractCarImage image : images) {
numberPlates.add(intel.recognize(new CarSnapshot(image.getImage())));
}

return getAverageNumberPlate(numberPlates);
}

Expand All @@ -94,21 +90,10 @@ public String detectPlateOrigin(final List<? extends AbstractCarImage> images) {
}

private static String getAverageNumberPlate(final List<String> numberPlates) {
HashMap<String, Integer> map = new HashMap<>();

for (String s : numberPlates) {
Integer count = map.get(s);
map.put(s, count != null ? count + 1 : 0);
}

String popular = Collections.max(map.entrySet(), new Comparator<Entry<String, Integer>>() {

@Override
public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
return o1.getValue().compareTo(o2.getValue());
}
}).getKey();

final Multiset<String> plateSet = TreeMultiset.create();
plateSet.addAll(numberPlates);
String popular = Multisets.copyHighestCountFirst(plateSet).iterator().next();
LOGGER.debug("Most popular plate is {}, occurrences {}", popular, plateSet.count(popular));
return popular;
}
}

0 comments on commit 01ec827

Please sign in to comment.