mirror of
https://github.com/davpapp/PowerMiner
synced 2024-12-21 23:48:49 -05:00
Writing test bench for inventory recognition
This commit is contained in:
parent
4d6ddea2cf
commit
ebe9970ff7
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
bin/InventoryTest.class
Normal file
BIN
bin/InventoryTest.class
Normal file
Binary file not shown.
BIN
bin/main.class
BIN
bin/main.class
Binary file not shown.
@ -11,14 +11,23 @@ public class Inventory {
|
||||
public static final int INVENTORY_WIDTH = 820 - 649;// 820
|
||||
public static final int INVENTORY_HEIGHT = 350; // 530
|
||||
|
||||
Robot robot;
|
||||
Rectangle inventoryAreaToCapture;
|
||||
InventorySlot[][] inventorySlots;
|
||||
public static final int NUM_ROWS = 4;
|
||||
public static final int NUM_COLUMNS = 7;
|
||||
|
||||
public Inventory() throws AWTException {
|
||||
Robot robot;
|
||||
Rectangle inventoryRectangleToCapture;
|
||||
InventorySlot[][] inventorySlots;
|
||||
InventoryItems items;
|
||||
|
||||
public Inventory() throws AWTException, IOException {
|
||||
initializeInventoryRectangle();
|
||||
initializeInventorySlots();
|
||||
initializeItems();
|
||||
robot = new Robot();
|
||||
this.inventoryAreaToCapture = new Rectangle(INVENTORY_OFFSET_WIDTH, INVENTORY_OFFSET_HEIGHT, INVENTORY_WIDTH, INVENTORY_HEIGHT);
|
||||
}
|
||||
|
||||
private void initializeInventoryRectangle() {
|
||||
inventoryRectangleToCapture = new Rectangle(INVENTORY_OFFSET_WIDTH, INVENTORY_OFFSET_HEIGHT, INVENTORY_WIDTH, INVENTORY_HEIGHT);
|
||||
}
|
||||
|
||||
private void initializeInventorySlots() {
|
||||
@ -30,11 +39,19 @@ public class Inventory {
|
||||
}
|
||||
}
|
||||
|
||||
private void initializeItems() throws IOException {
|
||||
items = new InventoryItems("/home/dpapp/Desktop/RunescapeAIPics/Items/");
|
||||
}
|
||||
|
||||
public void update() throws IOException {
|
||||
BufferedImage image = robot.createScreenCapture(this.inventoryAreaToCapture);
|
||||
BufferedImage image = robot.createScreenCapture(this.inventoryRectangleToCapture);
|
||||
updateAllInventorySlots(image);
|
||||
}
|
||||
|
||||
|
||||
public void updateWithFakeImageForTests(BufferedImage testImage) throws IOException {
|
||||
updateAllInventorySlots(testImage);
|
||||
}
|
||||
|
||||
private void updateAllInventorySlots(BufferedImage image) throws IOException {
|
||||
for (int row = 0; row < 4; row++) {
|
||||
for (int column = 0; column < 7; column++) {
|
||||
@ -42,5 +59,19 @@ public class Inventory {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String getItemNameInInventorySlot(int row, int column) {
|
||||
return inventorySlots[row][column].getItemNameInInventorySlot(items);
|
||||
}
|
||||
|
||||
public boolean isInventoryFull() {
|
||||
for (int row = 0; row < 4; row++) {
|
||||
for (int column = 0; column < 7; column++) {
|
||||
if (!inventorySlots[row][column].isInventorySlotEmpty(items)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@ -4,12 +4,12 @@ import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
public class Item {
|
||||
public class InventoryItem {
|
||||
|
||||
private BufferedImage itemImage;
|
||||
private int minimumNumberOfMatchingPixels;
|
||||
|
||||
public Item(String itemDirectoryPath, String itemName) throws IOException {
|
||||
public InventoryItem(String itemDirectoryPath, String itemName) throws IOException {
|
||||
initializeImage(itemDirectoryPath, itemName);
|
||||
this.minimumNumberOfMatchingPixels = 100;
|
||||
}
|
@ -3,16 +3,16 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Items {
|
||||
public class InventoryItems {
|
||||
// TODO: write tests
|
||||
HashMap<String, Item> items;
|
||||
HashMap<String, InventoryItem> items;
|
||||
|
||||
public Items(String itemDirectoryPath) throws IOException {
|
||||
public InventoryItems(String itemDirectoryPath) throws IOException {
|
||||
initializeItemsFromDirectory(itemDirectoryPath);
|
||||
}
|
||||
|
||||
private void initializeItemsFromDirectory(String itemDirectoryPath) throws IOException {
|
||||
this.items = new HashMap<String, Item>();
|
||||
this.items = new HashMap<String, InventoryItem>();
|
||||
for (File itemFile : getListOfFilesFromItemDirectory(itemDirectoryPath)) {
|
||||
if (itemFile.isFile()) {
|
||||
putItemInMap(itemDirectoryPath, itemFile.getName());
|
||||
@ -26,7 +26,7 @@ public class Items {
|
||||
}
|
||||
|
||||
private void putItemInMap(String itemDirectoryPath, String itemFileName) throws IOException {
|
||||
Item item = new Item(itemDirectoryPath, itemFileName);
|
||||
InventoryItem item = new InventoryItem(itemDirectoryPath, itemFileName);
|
||||
String itemName = getItemNameFromFile(itemFileName);
|
||||
this.items.put(itemName, item);
|
||||
}
|
||||
@ -51,7 +51,7 @@ public class Items {
|
||||
return "empty";
|
||||
}
|
||||
|
||||
private Item getItemByName(String itemName) {
|
||||
private InventoryItem getItemByName(String itemName) {
|
||||
return items.get(itemName);
|
||||
}
|
||||
|
@ -11,14 +11,14 @@ import org.junit.Before;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ItemsTest {
|
||||
class InventoryItemsTest {
|
||||
|
||||
Items items;
|
||||
InventoryItems items;
|
||||
String testingItemDirectoryPath;
|
||||
|
||||
public void initialize() throws IOException {
|
||||
System.out.println("running initialize...");
|
||||
items = new Items("/home/dpapp/Desktop/RunescapeAIPics/Tests/Items/");
|
||||
items = new InventoryItems("/home/dpapp/Desktop/RunescapeAIPics/Tests/Items/");
|
||||
this.testingItemDirectoryPath = "/home/dpapp/Desktop/RunescapeAIPics/Tests/ItemNameRecognition/";
|
||||
}
|
||||
|
@ -11,9 +11,8 @@ public class InventorySlot {
|
||||
private static final int INVENTORY_SLOT_WIDTH = 171 / 4;
|
||||
private static final int INVENTORY_SLOT_HEIGHT = 254 / 7;
|
||||
|
||||
String screenshotOutputDirectory;
|
||||
Item itemInInventorySlot;
|
||||
Rectangle rectangleToCapture;
|
||||
BufferedImage inventorySlotImage;
|
||||
|
||||
public InventorySlot(int row, int column) {
|
||||
initializeRectangleToCapture(row, column);
|
||||
@ -24,62 +23,14 @@ public class InventorySlot {
|
||||
}
|
||||
|
||||
public void updateInventorySlot(BufferedImage image) throws IOException {
|
||||
BufferedImage croppedInventorySlotArea = image.getSubimage(rectangleToCapture.x, rectangleToCapture.y, rectangleToCapture.width, rectangleToCapture.height);
|
||||
setItemInInventorySlotFromImage(croppedInventorySlotArea);
|
||||
this.inventorySlotImage = image.getSubimage(rectangleToCapture.x, rectangleToCapture.y, rectangleToCapture.width, rectangleToCapture.height);
|
||||
}
|
||||
|
||||
private void setItemInInventorySlotFromImage(BufferedImage croppedInventorySlotArea) throws IOException {
|
||||
if (itemIsLog(croppedInventorySlotArea)) System.out.println("LOG!");
|
||||
public String getItemNameInInventorySlot(InventoryItems items) {
|
||||
return items.getNameOfItemFromImage(this.inventorySlotImage);
|
||||
}
|
||||
|
||||
public boolean itemIsLog(BufferedImage croppedInventorySlotArea) throws IOException {
|
||||
int matchingPixel = 0;
|
||||
//int nonMatchingPixel = 0;
|
||||
File image = new File("/home/dpapp/Desktop/RunescapeAIPics/Items/willowLogs.png");
|
||||
BufferedImage logImage = ImageIO.read(image);
|
||||
/*System.out.println(logImage.getWidth() + ", " + logImage.getHeight());
|
||||
System.out.println(INVENTORY_SLOT_WIDTH + ", " + INVENTORY_SLOT_HEIGHT);
|
||||
System.out.println("Dimension match?");*/
|
||||
for (int row = 0; row < INVENTORY_SLOT_WIDTH; row++) {
|
||||
for (int col = 0; col < INVENTORY_SLOT_HEIGHT; col++) {
|
||||
if (pixelsWithinRGBTolerance(croppedInventorySlotArea.getRGB(row, col), logImage.getRGB(row, col))) {
|
||||
matchingPixel++;
|
||||
}
|
||||
/*else {
|
||||
nonMatchingPixel++;
|
||||
}*/
|
||||
}
|
||||
}
|
||||
|
||||
if (matchingPixel > 300) {
|
||||
//System.out.println("Found log with " + matchingPixel + " matches!" + nonMatchingPixel);
|
||||
return true;
|
||||
}
|
||||
//System.out.println("No match!" + matchingPixel + ", nonmatching: " + nonMatchingPixel);
|
||||
return false;
|
||||
public boolean isInventorySlotEmpty(InventoryItems items) {
|
||||
return ("empty" == items.getNameOfItemFromImage(this.inventorySlotImage));
|
||||
}
|
||||
|
||||
private boolean pixelsWithinRGBTolerance(int rgb1, int rgb2) {
|
||||
int[] colors1 = getRGBValuesFromPixel(rgb1);
|
||||
int[] colors2 = getRGBValuesFromPixel(rgb2);
|
||||
for (int i = 0; i < 3; i++) {
|
||||
if (Math.abs(colors1[i] - colors2[i]) > 5) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
/*displayColor(colors1);
|
||||
System.out.println("vs");
|
||||
displayColor(colors2);
|
||||
System.out.println();*/
|
||||
return true;
|
||||
}
|
||||
|
||||
private void displayColor(int[] colors) {
|
||||
System.out.println(colors[0] + "," + colors[1] + "," + colors[2]);
|
||||
}
|
||||
|
||||
private int[] getRGBValuesFromPixel(int pixel) {
|
||||
int[] colors = {(pixel)&0xFF, (pixel>>8)&0xFF, (pixel>>16)&0xFF, (pixel>>24)&0xFF};
|
||||
return colors;
|
||||
}
|
||||
}
|
||||
|
@ -1,29 +0,0 @@
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class InventorySlotTest {
|
||||
|
||||
|
||||
InventorySlot inventorySlot;
|
||||
|
||||
void initialize() {
|
||||
inventorySlot = new InventorySlot(0, 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void test() throws IOException {
|
||||
initialize();
|
||||
File image = new File("/home/dpapp/Desktop/RunescapeAIPics/Tests/screenshot0_0.png");
|
||||
BufferedImage testImage = ImageIO.read(image);
|
||||
assertTrue(inventorySlot.itemIsLog(testImage));
|
||||
}
|
||||
|
||||
}
|
39
src/InventoryTest.java
Normal file
39
src/InventoryTest.java
Normal file
@ -0,0 +1,39 @@
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.awt.AWTException;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class InventoryTest {
|
||||
|
||||
Inventory inventory;
|
||||
String testingInventoryDirectoryPath;
|
||||
|
||||
public void initialize() throws AWTException, IOException {
|
||||
inventory = new Inventory();
|
||||
this.testingInventoryDirectoryPath = "/home/dpapp/Desktop/RunescapeAIPics/Tests/Inventory/";
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetNameInItemInventorySlot() throws IOException, AWTException {
|
||||
initialize();
|
||||
|
||||
// TODO: add image for uploading custom image to Inventory
|
||||
BufferedImage testImage = loadBufferedImage("inventory_0.png");
|
||||
inventory.updateWithFakeImageForTests(testImage);
|
||||
assertEquals(inventory.getItemNameInInventorySlot(0, 0), "willowLogs");
|
||||
assertEquals(inventory.getItemNameInInventorySlot(3, 6), "empty");
|
||||
}
|
||||
|
||||
public BufferedImage loadBufferedImage(String fileName) throws IOException {
|
||||
File itemFile = new File(this.testingInventoryDirectoryPath + fileName);
|
||||
BufferedImage itemImage = ImageIO.read(itemFile);
|
||||
return itemImage;
|
||||
}
|
||||
|
||||
}
|
@ -11,7 +11,8 @@ public class main {
|
||||
//Cursor cursor = new Cursor();
|
||||
//Inventory inventory = new Inventory();
|
||||
//inventory.update();
|
||||
Items items = new Items("/home/dpapp/Desktop/RunescapeAIPics/Items/");
|
||||
Inventory inventory = new Inventory();
|
||||
//Items items = new Items("/home/dpapp/Desktop/RunescapeAIPics/Items/");
|
||||
//items.displayItems();
|
||||
|
||||
System.out.println("Success!");
|
||||
|
Loading…
Reference in New Issue
Block a user