Search in sources :

Example 1 with RelativePixelAnalyser

use of net.hearthstats.game.imageanalysis.RelativePixelAnalyser in project HearthStats.net-Uploader by HearthStats.

the class RelativePixelAnalyserMain method testFindRelativePixel.

public void testFindRelativePixel() throws Exception {
    ScreenAnalyser analyser = new ScreenAnalyser();
    RelativePixelAnalyser relativePixelAnalyser = new RelativePixelAnalyser();
    File imageFolder = new File(IMAGE_PATH);
    File[] imageArray = imageFolder.listFiles();
    Assert.assertNotNull("No files found in " + IMAGE_PATH + ". Please make sure you've set the path to a folder that contains screenshots from Hearthstone", imageArray);
    List<File> images = new ArrayList<>(imageArray.length);
    for (File image : imageArray) {
        if (image.getName().endsWith(".png")) {
            // Determine if this is a match end image
            BufferedImage bufferedImage = ImageIO.read(image);
            Screen screen = analyser.identifyScreen(bufferedImage, null);
            if (screen == Screen.MATCH_NAXXRAMAS_END || screen == Screen.MATCH_ORGRIMMAR_END || screen == Screen.MATCH_PANDARIA_END || screen == Screen.MATCH_STORMWIND_END || screen == Screen.MATCH_STRANGLETHORN_END) {
                // This is a match end screen, so it is suitable for testing with the RelativePixelAnalyser
                images.add(image);
            }
            bufferedImage.flush();
        }
    }
    Assert.assertFalse("No match end images found in " + IMAGE_PATH + ". Please make sure you've set the path to a folder that contains screenshots from Hearthstone", images.size() == 0);
    int page = 0;
    int pageCount = (images.size() / PAGE_SIZE) + 1;
    while (page < pageCount) {
        page++;
        String filename = IMAGE_PATH + "/relative-test-" + page + ".html";
        try (BufferedWriter output = new BufferedWriter(new FileWriter(filename))) {
            writeHtmlHeader(output, page, pageCount);
            List<Coordinate> coordinates = new ArrayList<>();
            for (int i = (page - 1) * PAGE_SIZE; i < images.size() && i < page * PAGE_SIZE; i++) {
                File image = images.get(i);
                output.write("<tr>" + "<td colspan=\"3\" class=\"filename\"><h2>");
                output.write(image.getName());
                output.write("</h2></td>" + "</tr>" + "<tr>" + "<td><div><img src=\"");
                output.write(image.getName());
                output.write("\" id=\"img_");
                output.write(String.valueOf(i - ((page - 1) * PAGE_SIZE)));
                output.write("\" alt=\"");
                output.write(image.getName());
                output.write("\" width=\"400\"></div></td>");
                output.write("<td><canvas id=\"canvas_");
                output.write(String.valueOf(i - ((page - 1) * PAGE_SIZE)));
                output.write("\" width=\"300\" height=\"300\"></td>");
                try {
                    log.debug("***** Testing Image {} *****", image.getName());
                    BufferedImage bufferedImage = ImageIO.read(image);
                    Coordinate coordinate = relativePixelAnalyser.findRelativePixel(bufferedImage, UniquePixel.VICTORY_DEFEAT_REFBOX_TL, UniquePixel.VICTORY_DEFEAT_REFBOX_BR, 8, 11);
                    coordinates.add(coordinate);
                    output.write("<td class=\"");
                    if (coordinate == null) {
                        output.write("matchzero");
                    } else {
                        output.write("matchone");
                    }
                    output.write("\">");
                    if (coordinate != null) {
                        output.write("<div>Reference Pixel = ");
                        output.write(String.valueOf(coordinate.x()));
                        output.write(", ");
                        output.write(String.valueOf(coordinate.y()));
                        output.write("</div>");
                        int victory1Matches = relativePixelAnalyser.countMatchingRelativePixels(bufferedImage, coordinate, new UniquePixel[] { UniquePixel.VICTORY_REL_1A, UniquePixel.VICTORY_REL_1B });
                        int victory2Matches = relativePixelAnalyser.countMatchingRelativePixels(bufferedImage, coordinate, new UniquePixel[] { UniquePixel.VICTORY_REL_2A, UniquePixel.VICTORY_REL_2B, UniquePixel.VICTORY_REL_2C });
                        int defeat1Matches = relativePixelAnalyser.countMatchingRelativePixels(bufferedImage, coordinate, new UniquePixel[] { UniquePixel.DEFEAT_REL_1A, UniquePixel.DEFEAT_REL_1B, UniquePixel.DEFEAT_REL_1C, UniquePixel.DEFEAT_REL_1D, UniquePixel.DEFEAT_REL_1E });
                        int defeat2Matches = relativePixelAnalyser.countMatchingRelativePixels(bufferedImage, coordinate, new UniquePixel[] { UniquePixel.DEFEAT_REL_2A });
                        output.write("<div>Count of V1 matches: ");
                        output.write(String.valueOf(victory1Matches));
                        output.write("</div>");
                        output.write("<div>Count of V2 matches: ");
                        output.write(String.valueOf(victory2Matches));
                        output.write("</div>");
                        output.write("<div>Count of D1 matches: ");
                        output.write(String.valueOf(defeat1Matches));
                        output.write("</div>");
                        output.write("<div>Count of D2 matches: ");
                        output.write(String.valueOf(defeat2Matches));
                        output.write("</div>");
                        if (victory1Matches > 0 && victory2Matches == 3 && defeat1Matches == 0 && defeat2Matches == 0) {
                            output.write("<div><b>MATCHED VICTORY</b></div>");
                        }
                        if (victory1Matches == 0 && victory2Matches == 0 && defeat1Matches > 0 && defeat2Matches == 1) {
                            output.write("<div><b>MATCHED DEFEAT</b></div>");
                        }
                    }
                    output.write("</td>");
                } catch (IOException e) {
                    log.warn("Cannot handle image " + image.getName() + " due to exception", e);
                    output.write("<b>Exception</b></td></tr>");
                }
                output.write("</tr>");
            }
            writeCanvasJavascript(output, coordinates);
            writeHtmlFooter(output, page, pageCount);
        } catch (IOException e) {
            Assert.fail("IOException writing file " + filename);
            throw e;
        }
    }
}
Also used : Screen(net.hearthstats.game.Screen) ArrayList(java.util.ArrayList) RelativePixelAnalyser(net.hearthstats.game.imageanalysis.RelativePixelAnalyser) BufferedImage(java.awt.image.BufferedImage) Coordinate(net.hearthstats.util.Coordinate) ScreenAnalyser(net.hearthstats.game.imageanalysis.ScreenAnalyser)

Aggregations

BufferedImage (java.awt.image.BufferedImage)1 ArrayList (java.util.ArrayList)1 Screen (net.hearthstats.game.Screen)1 RelativePixelAnalyser (net.hearthstats.game.imageanalysis.RelativePixelAnalyser)1 ScreenAnalyser (net.hearthstats.game.imageanalysis.ScreenAnalyser)1 Coordinate (net.hearthstats.util.Coordinate)1