Skip to content

add more image for each charector #72

Open
@parsibox

Description

@parsibox

hi
i change your code to add more than one image for each charector
please change it in your project
change in class KnnPatternClassifier

    public KnnPatternClassifier() {
        String path = Configurator.getConfigurator().getPathProperty("char_learnAlphabetPath");
        learnLists = new ArrayList<>(36);
        davari_learnLists_char = new ArrayList<Character>(1000);

        final String alphaString = "0123456789abcdefghijklmnopqrstuvwxyz";
        String directory = "alphabet_10x15";

        File files[] = new File(directory).listFiles();
        System.out.println("directory : " + directory);
        List<String> filenames = new ArrayList<>();
        for (int i = 0; i < alphaString.length(); i++) {

            for (File f : files) {
                if ( f.getName().startsWith(alphaString.charAt(i)+"_") ){

                 //   System.out.println("shoro ba "+alphaString.charAt(i)+" ==> " + f.getName());
                    String s = directory + File.separator +f.getName();
                    if (Configurator.getConfigurator().getResourceAsStream(s) != null) {
                        Char imgChar;
                        try (InputStream is = Configurator.getConfigurator().getResourceAsStream(s)) {
                            imgChar = new Char(is);
                        } catch (IOException e) {
                            throw new IllegalStateException("Failed to load Char: " + s, e);
                        }
                        imgChar.normalize();
                        learnLists.add(imgChar.extractFeatures());
                        davari_learnLists_char.add(alphaString.charAt(i));
                    }
                }
            }

        }

        /*
        List<String> filenames = Char.getAlphabetList(path);
        for (String fileName : filenames) {
            Char imgChar;
            try (InputStream is = Configurator.getConfigurator().getResourceAsStream(fileName)) {
                imgChar = new Char(is);
            } catch (IOException e) {
                throw new IllegalStateException("Failed to load Char: " + fileName, e);
            }
            imgChar.normalize();
            learnLists.add(imgChar.extractFeatures());
        }
        */
        // check vector elements
        for (List<Double> learnList : learnLists) {
            if (learnList == null) {
                logger.warn("Alphabet in {} is not complete", path);
            }
        }
    }

    @Override
    public RecognizedChar recognize(Char chr) {
        //recognized.setImage(chr.getImage());
        List<Double> tested = chr.extractFeatures();
        RecognizedChar recognized = new RecognizedChar(chr);
        for (int x = 0; x < learnLists.size(); x++) {

            float fx = simplifiedEuclideanDistance(tested, learnLists.get(x));
            recognized.addPattern(new RecognizedPattern(davari_learnLists_char.get(x), fx));
        }
        recognized.sort(false);
        return recognized;
    }

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions