Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import net.sf.javaanpr.imageanalysis.Plate;
import net.sf.javaanpr.jar.Main;
import net.sf.javaanpr.recognizer.CharacterRecognizer;
import net.sf.javaanpr.recognizer.CharacterRecognizer.RecognizedChar;
import net.sf.javaanpr.recognizer.RecognizedChar;
import net.sf.javaanpr.recognizer.KnnPatternClassificator;
import net.sf.javaanpr.recognizer.NeuralPatternClassificator;
import org.xml.sax.SAXException;
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/net/sf/javaanpr/intelligence/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

import net.sf.javaanpr.configurator.Configurator;
import net.sf.javaanpr.jar.Main;
import net.sf.javaanpr.recognizer.CharacterRecognizer.RecognizedChar;
import net.sf.javaanpr.recognizer.RecognizedChar;
import net.sf.javaanpr.recognizer.RecognizedPattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
Expand Down Expand Up @@ -204,7 +205,7 @@ public String parse(RecognizedPlate recognizedPlate, int syntaxAnalysisMode) { /
finalPlate.requiredChanges++; // +1 for every char
for (int x = 0; x < rc.getPatterns().size(); x++) {
if (form.getPosition(j).isAllowed(rc.getPattern(x).getChar())) {
RecognizedChar.RecognizedPattern rp = rc.getPattern(x);
RecognizedPattern rp = rc.getPattern(x);
finalPlate.requiredChanges += (rp.getCost() / 100); // +x for its cost
finalPlate.addChar(rp.getChar());
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package net.sf.javaanpr.intelligence;

import net.sf.javaanpr.recognizer.CharacterRecognizer.RecognizedChar;
import net.sf.javaanpr.recognizer.RecognizedChar;

import java.util.Vector;

Expand Down
122 changes: 0 additions & 122 deletions src/main/java/net/sf/javaanpr/recognizer/CharacterRecognizer.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@

import net.sf.javaanpr.imageanalysis.Char;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;

public abstract class CharacterRecognizer {

public static final char[] ALPHABET =
Expand All @@ -45,120 +39,4 @@ public abstract class CharacterRecognizer {
};

public abstract RecognizedChar recognize(Char chr);

public class RecognizedChar {
private Vector<RecognizedPattern> patterns;
private boolean isSorted;

public RecognizedChar() {
this.patterns = new Vector<RecognizedPattern>();
this.isSorted = false;
}

public void addPattern(RecognizedPattern pattern) {
this.patterns.add(pattern);
}

public boolean isSorted() {
return this.isSorted;
}

public void sort(int direction) {
if (this.isSorted) {
return;
}
this.isSorted = true;
Collections.sort(this.patterns, new PatternComparer(direction));
}

/**
* @return null if not sorted
*/
public Vector<RecognizedPattern> getPatterns() {
if (this.isSorted) {
return this.patterns;
}
return null;
}

public RecognizedPattern getPattern(int i) {
if (this.isSorted) {
return this.patterns.elementAt(i);
}
return null;
}

public BufferedImage render() {
int width = 500;
int height = 200;
BufferedImage histogram = new BufferedImage(width + 20, height + 20, BufferedImage.TYPE_INT_RGB);
Graphics2D graphic = histogram.createGraphics();
graphic.setColor(Color.LIGHT_GRAY);
Rectangle backRect = new Rectangle(0, 0, width + 20, height + 20);
graphic.fill(backRect);
graphic.draw(backRect);
graphic.setColor(Color.BLACK);

int colWidth = width / this.patterns.size();
int left, top;
for (int ay = 0; ay <= 100; ay += 10) {
int y = 15 + (int) (((100 - ay) / 100.0f) * (height - 20));
graphic.drawString(new Integer(ay).toString(), 3, y + 11);
graphic.drawLine(25, y + 5, 35, y + 5);
}
graphic.drawLine(35, 19, 35, height);
graphic.setColor(Color.BLUE);
for (int i = 0; i < this.patterns.size(); i++) {
left = (i * colWidth) + 42;
top = height - (int) (this.patterns.elementAt(i).cost * (height - 20));
graphic.drawRect(left, top, colWidth - 2, height - top);
graphic.drawString(this.patterns.elementAt(i).chr + " ", left + 2, top - 8);
}
return histogram;
}

public final class RecognizedPattern {
private char chr;
private float cost;

public RecognizedPattern(char chr, float value) {
this.chr = chr;
this.cost = value;
}

public char getChar() {
return this.chr;
}

public float getCost() {
return this.cost;
}
}

public class PatternComparer implements Comparator<Object> {

private int direction;

public PatternComparer(int direction) {
this.direction = direction;
}

@Override
public int compare(Object o1, Object o2) {
float cost1 = ((RecognizedPattern) o1).getCost();
float cost2 = ((RecognizedPattern) o2).getCost();
int ret = 0;
if (cost1 < cost2) {
ret = -1;
}
if (cost1 > cost2) {
ret = 1;
}
if (this.direction == 1) {
ret *= -1;
}
return ret;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public RecognizedChar recognize(Char chr) {
RecognizedChar recognized = new RecognizedChar();
for (int x = 0; x < this.learnVectors.size(); x++) {
float fx = this.simplifiedEuclideanDistance(tested, this.learnVectors.elementAt(x));
recognized.addPattern(recognized.new RecognizedPattern(ALPHABET[x], fx));
recognized.addPattern(new RecognizedPattern(ALPHABET[x], fx));
}
recognized.sort(0);
recognized.sort(false);
return recognized;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,18 @@ public NeuralNetwork getNetwork() {
* Image to character.
*
* @param imgChar the Char to recognize
* @return the {@link net.sf.javaanpr.recognizer.CharacterRecognizer.RecognizedChar}
* @return the {@link net.sf.javaanpr.recognizer.RecognizedChar}
*/
@Override
public RecognizedChar recognize(Char imgChar) {
imgChar.normalize();
Vector<Double> output = this.network.test(imgChar.extractFeatures());
RecognizedChar recognized = new RecognizedChar();
for (int i = 0; i < output.size(); i++) {
recognized.addPattern(recognized.new RecognizedPattern(ALPHABET[i], output.elementAt(i).floatValue()));
recognized.addPattern(new RecognizedPattern(ALPHABET[i], output.elementAt(i).floatValue()));
}
recognized.render();
recognized.sort(1);
recognized.sort(true);
return recognized;
}

Expand Down
34 changes: 34 additions & 0 deletions src/main/java/net/sf/javaanpr/recognizer/PatternComparator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2013 JavaANPR contributors
* Copyright 2006 Ondrej Martinsky
* Licensed under the Educational Community 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.osedu.org/licenses/ECL-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 net.sf.javaanpr.recognizer;

import java.util.Comparator;

public class PatternComparator implements Comparator<RecognizedPattern> {
private boolean shouldSortDescending;

public PatternComparator(boolean shouldSortDescending) {
this.shouldSortDescending = shouldSortDescending;
}

@Override
public int compare(RecognizedPattern recognizedPattern1, RecognizedPattern recognizedPattern2) {
Float cost1 = recognizedPattern1.getCost();
Float cost2 = recognizedPattern2.getCost();
return shouldSortDescending ? -1 * cost1.compareTo(cost2) : cost1.compareTo(cost2);
}
}
94 changes: 94 additions & 0 deletions src/main/java/net/sf/javaanpr/recognizer/RecognizedChar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright 2013 JavaANPR contributors
* Copyright 2006 Ondrej Martinsky
* Licensed under the Educational Community 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.osedu.org/licenses/ECL-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 net.sf.javaanpr.recognizer;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Collections;
import java.util.Vector;

public class RecognizedChar {
private Vector<RecognizedPattern> patterns;
private boolean isSorted;

public RecognizedChar() {
this.patterns = new Vector<RecognizedPattern>();
this.isSorted = false;
}

public void addPattern(RecognizedPattern pattern) {
this.patterns.add(pattern);
}

public boolean isSorted() {
return this.isSorted;
}

public void sort(boolean shouldSortDescending) {
if (this.isSorted) {
return;
}
this.isSorted = true;
Collections.sort(this.patterns, new PatternComparator(shouldSortDescending));
}

/**
* @return null if not sorted
*/
public Vector<RecognizedPattern> getPatterns() {
if (this.isSorted) {
return this.patterns;
}
return null;
}

public RecognizedPattern getPattern(int i) {
if (this.isSorted) {
return this.patterns.elementAt(i);
}
return null;
}

public BufferedImage render() {
int width = 500;
int height = 200;
BufferedImage histogram = new BufferedImage(width + 20, height + 20, BufferedImage.TYPE_INT_RGB);
Graphics2D graphic = histogram.createGraphics();
graphic.setColor(Color.LIGHT_GRAY);
Rectangle backRect = new Rectangle(0, 0, width + 20, height + 20);
graphic.fill(backRect);
graphic.draw(backRect);
graphic.setColor(Color.BLACK);

int colWidth = width / this.patterns.size();
int left, top;
for (int ay = 0; ay <= 100; ay += 10) {
int y = 15 + (int) (((100 - ay) / 100.0f) * (height - 20));
graphic.drawString(new Integer(ay).toString(), 3, y + 11);
graphic.drawLine(25, y + 5, 35, y + 5);
}
graphic.drawLine(35, 19, 35, height);
graphic.setColor(Color.BLUE);
for (int i = 0; i < this.patterns.size(); i++) {
left = (i * colWidth) + 42;
top = height - (int) (this.patterns.elementAt(i).getCost() * (height - 20));
graphic.drawRect(left, top, colWidth - 2, height - top);
graphic.drawString(this.patterns.elementAt(i).getChar() + " ", left + 2, top - 8);
}
return histogram;
}
}
35 changes: 35 additions & 0 deletions src/main/java/net/sf/javaanpr/recognizer/RecognizedPattern.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2013 JavaANPR contributors
* Copyright 2006 Ondrej Martinsky
* Licensed under the Educational Community 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.osedu.org/licenses/ECL-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 net.sf.javaanpr.recognizer;

public final class RecognizedPattern {
private char chr;
private float cost;

public RecognizedPattern(char chr, float value) {
this.chr = chr;
this.cost = value;
}

public char getChar() {
return this.chr;
}

public float getCost() {
return this.cost;
}
}
Loading