1
0
mirror of https://github.com/davpapp/PowerMiner synced 2024-12-22 07:58:48 -05:00

Writing test bench for item recognition

This commit is contained in:
davpapp 2018-02-02 00:03:14 -05:00
parent 7610163b13
commit 4d6ddea2cf
8 changed files with 82 additions and 15 deletions

Binary file not shown.

Binary file not shown.

BIN
bin/ItemsTest.class Normal file

Binary file not shown.

Binary file not shown.

View File

@ -7,9 +7,11 @@ import javax.imageio.ImageIO;
public class Item {
private BufferedImage itemImage;
private int minimumNumberOfMatchingPixels;
public Item(String itemDirectoryPath, String itemName) throws IOException {
initializeImage(itemDirectoryPath, itemName);
this.minimumNumberOfMatchingPixels = 100;
}
private void initializeImage(String itemDirectoryPath, String itemName) throws IOException {
@ -23,7 +25,7 @@ public class Item {
public boolean itemMatchesImage(BufferedImage itemImageToCompare) {
if (imagesAreTheSameSize(itemImageToCompare)) {
return imagesMatch(itemImageToCompare);
return imagesMatch(itemImageToCompare, 5);
}
return false;
}
@ -32,20 +34,17 @@ public class Item {
return (itemImage.getWidth() == itemImageToCompare.getWidth()) && (itemImage.getHeight() == itemImageToCompare.getHeight());
}
private boolean imagesMatch(BufferedImage itemImageToCompare) {
return false;
}
private boolean imagesMatch(BufferedImage itemImageToCompare, int pixelTolerance) {
int matchingPixel = 0;
int numberOfMatchingPixels = 0;
for (int row = 0; row < itemImageToCompare.getWidth(); row++) {
for (int col = 0; col < itemImageToCompare.getHeight(); col++) {
if (pixelsAreWithinRGBTolerance(itemImage.getRGB(row, col), itemImageToCompare.getRGB(row, col))) {
matchingPixel++;
numberOfMatchingPixels++;
}
}
}
return true;
return (numberOfMatchingPixels > this.minimumNumberOfMatchingPixels);
}
private boolean pixelsAreWithinRGBTolerance(int rgb1, int rgb2) {

View File

@ -7,8 +7,8 @@ public class Items {
// TODO: write tests
HashMap<String, Item> items;
public Items() throws IOException {
initializeItemsFromDirectory("/home/dpapp/Desktop/RunescapeAIPics/Items/");
public Items(String itemDirectoryPath) throws IOException {
initializeItemsFromDirectory(itemDirectoryPath);
}
private void initializeItemsFromDirectory(String itemDirectoryPath) throws IOException {
@ -20,7 +20,7 @@ public class Items {
}
}
private File[] getListOfFilesFromItemDirectory(String itemDirectoryPath) {
public File[] getListOfFilesFromItemDirectory(String itemDirectoryPath) {
File itemDirectory = new File(itemDirectoryPath);
return itemDirectory.listFiles();
}
@ -31,21 +31,32 @@ public class Items {
this.items.put(itemName, item);
}
private String getItemNameFromFile(String fileName) {
public String getItemNameFromFile(String fileName) {
return fileName.substring(0, fileName.indexOf('.'));
}
public boolean isInstanceOf(BufferedImage itemImage, String itemName) {
public boolean isImageThisItem(BufferedImage itemImage, String itemName) {
if (items.containsKey(itemName)) {
return getItem(itemName).itemMatchesImage(itemImage);
return getItemByName(itemName).itemMatchesImage(itemImage);
}
return false;
}
private Item getItem(String itemName) {
public String getNameOfItemFromImage(BufferedImage inputImage) {
for (String itemName : items.keySet()) {
if (getItemByName(itemName).itemMatchesImage(inputImage)) {
return itemName;
}
}
return "empty";
}
private Item getItemByName(String itemName) {
return items.get(itemName);
}
/*public void displayItems() {
for (HashMap.Entry<String, Item> entry : items.entrySet()) {
String itemName = entry.getKey();

57
src/ItemsTest.java Normal file
View File

@ -0,0 +1,57 @@
import static org.junit.jupiter.api.Assertions.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import javax.imageio.ImageIO;
import org.junit.Before;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
class ItemsTest {
Items items;
String testingItemDirectoryPath;
public void initialize() throws IOException {
System.out.println("running initialize...");
items = new Items("/home/dpapp/Desktop/RunescapeAIPics/Tests/Items/");
this.testingItemDirectoryPath = "/home/dpapp/Desktop/RunescapeAIPics/Tests/ItemNameRecognition/";
}
@Test
public void testGetNameOfItemFromImage() throws IOException {
initialize();
for (File itemFile : items.getListOfFilesFromItemDirectory(this.testingItemDirectoryPath)) {
if (itemFile.isFile()) {
BufferedImage itemImage = ImageIO.read(itemFile);
String expectedItemName = getItemNameForTest(itemFile.getName());
assertEquals(expectedItemName, items.getNameOfItemFromImage(itemImage));
System.out.println("Successfully recongized " + itemFile.getName());
}
}
}
@Test
public void testIsImageThisItem() throws IOException {
initialize();
for (File itemFile : items.getListOfFilesFromItemDirectory(this.testingItemDirectoryPath)) {
if (itemFile.isFile()) {
BufferedImage itemImage = ImageIO.read(itemFile);
String expectedItemName = getItemNameForTest(itemFile.getName());
if (expectedItemName == "empty") continue;
assertTrue(items.isImageThisItem(itemImage, expectedItemName));
System.out.println("Successfully recongized " + itemFile.getName());
}
}
}
private String getItemNameForTest(String fileName) {
return fileName.substring(0, fileName.indexOf('_'));
}
}

View File

@ -11,7 +11,7 @@ public class main {
//Cursor cursor = new Cursor();
//Inventory inventory = new Inventory();
//inventory.update();
Items items = new Items();
Items items = new Items("/home/dpapp/Desktop/RunescapeAIPics/Items/");
//items.displayItems();
System.out.println("Success!");