From ebe9970ff795724e9440ef27b8cd9094620fd020 Mon Sep 17 00:00:00 2001 From: davpapp Date: Fri, 2 Feb 2018 00:49:19 -0500 Subject: [PATCH] Writing test bench for inventory recognition --- bin/Inventory.class | Bin 1611 -> 2630 bytes bin/{Item.class => InventoryItem.class} | Bin 2605 -> 2632 bytes bin/{Items.class => InventoryItems.class} | Bin 2788 -> 2851 bytes ...emsTest.class => InventoryItemsTest.class} | Bin 2670 -> 2715 bytes bin/InventorySlot.class | Bin 3076 -> 1578 bytes bin/InventorySlotTest.class | Bin 1117 -> 0 bytes bin/InventoryTest.class | Bin 0 -> 1875 bytes bin/main.class | Bin 731 -> 688 bytes src/Inventory.java | 47 +++++++++++--- src/{Item.java => InventoryItem.java} | 4 +- src/{Items.java => InventoryItems.java} | 12 ++-- ...ItemsTest.java => InventoryItemsTest.java} | 6 +- src/InventorySlot.java | 61 ++---------------- src/InventorySlotTest.java | 29 --------- src/InventoryTest.java | 39 +++++++++++ src/main.java | 3 +- 16 files changed, 97 insertions(+), 104 deletions(-) rename bin/{Item.class => InventoryItem.class} (86%) rename bin/{Items.class => InventoryItems.class} (80%) rename bin/{ItemsTest.class => InventoryItemsTest.class} (86%) delete mode 100644 bin/InventorySlotTest.class create mode 100644 bin/InventoryTest.class rename src/{Item.java => InventoryItem.java} (94%) rename src/{Items.java => InventoryItems.java} (83%) rename src/{ItemsTest.java => InventoryItemsTest.java} (92%) delete mode 100644 src/InventorySlotTest.java create mode 100644 src/InventoryTest.java diff --git a/bin/Inventory.class b/bin/Inventory.class index c5560f1dd901fc4033aca6987a1f553eadcd4684..9c8d680c43b3c7b45bdb221e61b60fd893828a91 100644 GIT binary patch literal 2630 zcmaJ?-%}G;7(F)$EW{N8C{T#M5N!yS2-S*$AVMOk5G;vMw3RMlg_R_m2^+0__TAS` zr!Vb`5Bku~6q%_red|mAgU|V3alz3u_O|yd%(=&MYM}xrN1*)rC|lo6M~) zr)P4r0wOJNc-k&)J4VS_G742Q2n3!AbRF0+n@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3g^3A%UhP|SsI9(BI6lP7t St!Q`+7le+0i$YQLSo#mWSqFLm literal 1611 zcmah}O>Yxd6g_Xo$q+L!{s>7LJCF}TY=>f!K%q@Y3Be(5O(J2-C_(~FoCge@cp@`~ zw(IQKcH3^UU{$HXQjyqo!*5{IWjFl+6>59l*csa@Qf0e5WzBQz-x&xAM4wvEt(;@k9_O}pp315xFkINIWXC%EXR?Ij|A~0$Mv2G#O~iO z1_CG_gZh{4n(eI!=;=&FAe{fUD)&8`bsG3Y;B>%Z^d`mSan<3ZD+`PVpE5c9Tk=g45>s5CK4Dj zaG6c+VI9UIK2yk7*&8A&k@ULzg3480r?FS#fs$uczqw=Ww^8~V`@5DW4O|yU__f$> zuCNuTr)vJv#0{)+zsgp6vcyH!f`(pBf2^aKj?><=9?RU##-m5FE_VycT2eh&GjR)f z&SZ18PUakGTca4` zlO&ShI+txC$!k3O1{2#&OdVmGk2Bjb?PW|%#qcU79yBp~gzV1@*KmRNVUh)5LL8VA zgh>!0OYf)jnys|Bh@2nK_kkV-`4b7NVy|V5sPP*vX-AmXmk%+cUq8f1_7}`27gBGq zxSdQjaiw&iiB#11@t;2qgh+CIukdvr|BNq_l4OiUCyAcId9FFySNR4i9uqCBHjhE_ zyXFfP$2dOclZG!C6+!3&qT&O;go(dci@;KkSj#^1O-5^sj+5jxuk35A6tiG{{^2%ww1Tdg9rhS8DABRdXD<4=p9x1 Pd{rVXRla6^4aI)}I)_8Z diff --git a/bin/Item.class b/bin/InventoryItem.class similarity index 86% rename from bin/Item.class rename to bin/InventoryItem.class index 92f038208287de707d3f36c61bcff571878af647..e950b6d2c301b02eefaa26d8af8b37502f91e006 100644 GIT binary patch delta 67 zcmZ20azaG%)W2Q(7#J8#8Oqrim>3y&J@d*^^GfoIDmMxkGII0#!1hvQ|Xk)W2Q(7#J8#8Oqrim>3yYHcA>YGO_t=4rNSZVdUI=l(m@!0PnX7c>n+a diff --git a/bin/Items.class b/bin/InventoryItems.class similarity index 80% rename from bin/Items.class rename to bin/InventoryItems.class index 99a863603c4e2bab6c7e0ac7af355b48a0067bc1..32d37ffe102fe813e222c482f411e36a888394d3 100644 GIT binary patch delta 134 zcmaDNx>!u|)W2Q(7#J8#89Lb+m>3!OJoCy@^GfoIDkloKGn!3|Q0IUP)a~OI@PW&3 vwqtz2$i)liPnKui#m$e9pM0D7=j4NoqFe}}&97MgGja*TwQPRH_KO(+X`V6d delta 74 zcmV-Q0JZ<47UUHW%Ko~(0000r0Eq_x0s#O8kr_t;Es3y^JoCy@^GfoIDklnPatrt%xYljlB1qiB54iZ? SlAD(>2D5UBBlthq%9sHi-Xa|U delta 50 zcmbO&`c6dP)W2Q(7#J8#8CuyHm>3y2CrWBEvHMK)ZDZp0nRxF3BkSf9jKQpoe4AC- G%a{R!F%Crl diff --git a/bin/InventorySlot.class b/bin/InventorySlot.class index 06d7b9125a7a8428f63003a001ccd681038ab437..136e4f4f4f7239bf89ad100c3ba14c45fc3d9b0f 100644 GIT binary patch literal 1578 zcmah|Yi|=r6g}fdj5nM35eROQrUabUSz6rEcN1t!aLBgCZn5PGqzY}EiM`a`wKiiy z`h)t353K}5YNh@FepJ;nYqN0@1X^i!?wxzjIrrX~m;e3!FM!YSQwlMG8QXg-J>>_7 zjh?SkNC;#fIgg!k&+$6t`pzTSRs!ekTB}lP*6-ba*r?W<4}Y<5H@5|ZO@K|`+gFaK zT25~$x^|L<=K6w z!`S8HSXX<>w}yLrGLXCBIkToVT+dZE1(GG(UTX;?HvL^`AdWc`8Jri0m)2Sa1m;a# zL{?x)C%8_}{ar@%6Y4Tm*ORqje@6yQXQxLE`KsS`dMzh#wLNwwRM*`X$W%`>m4f1d ze?VDnzc=iADZI!0v*j9C#){T=Ss+E>T6&rqizcRF3d{`$yN;44umx61Z|;UJ->5uk z%Ykw^sT9_T6q37s*{(+?X|i86@gb%-*C*P#W}+NghuZqY#C2^Y58PeV)t=8x-XeOY zy3*}*m4PqubqZgxnNe-88IOr?z>Z9Jq-qRz!T^>yD)-H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.class deleted file mode 100644 index a355f15507881689c9a141ea9bba903ffa0072f3..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1117 zcmaJ=YflqF6g@)=3+qy#mGTsd&$eI(-;bgRzLHI<7$HsIiy7LX47R(Q-6^8~%0z)k z;t%jg8SiY1&_w;v*_nIio^$S<)1SY-{{V0gPb~B?jJe*v^t2y-T5tM#Lq^&{l3{dD z>D}GHht0Gk9+zlr6u8J5kz4%5NaoC1JiKhB2Eu=|Rj|dxMxMff<#TuKPljRYUE9;DC95h@q;1vnd@bT4 zhS`cAHuzqfq|h2DEkiB>#bb>&G0r;}#h`@+2Imz0m1@dCTwy5lUB4xHJrF^_U&!c# z_5)6AGOCF{uDWkkEsCKc9@RoAy=d3h%kP&CSxJH3y4WqFIDe)EP=$49XjQW& z&roba1JZ<&Ifs<`gEUD<2~5$QB;ibBmUf2u5`7JRi?T-UeDM%-C&+E4FCXD*W$_p{ zUKhV2yL5<~$GCMym?c5L1i?)b$rRB}V-7Phd9DN1B~OtB9^fI7WvIbNcuZ2s(^TDu FmEY?S{WJgo diff --git a/bin/InventoryTest.class b/bin/InventoryTest.class new file mode 100644 index 0000000000000000000000000000000000000000..effbe697f9fe7088c3521d8cec65f43a4bf6ab36 GIT binary patch literal 1875 zcmah~YggMw5WQ;yGC~ky2q6$45K6!ah@?qhq)Bjq)Qtl)P*eI!7ke>FWJ#6Q5c;dy za~i`b=ky2kNA+}862@t1`$5t^X71d%JG1)Z&)&h49w7+GEHT&(StiK$hgx z@ld)_Jz_{t&y*P2o_ZBwB8_efoiG_L6{Ray>$M#bZ1NpPXc(`|oe~eEo+pcKYEOpR zx*1&OK=`JHm)wy*k*m4h#x9@aci@=XW%MBU$iAXyRg=>agFkC zMldmm?oKdVx6qD`3`QC5+k0M3*cG4qzP%*E*UIzlmvvW!W$uf`!n!Pnwx$-^O&nWe z+^}FE!!W8vd?wT?uZekf+i*P6{~DnwSra80O3HuHbv?zSl!!>t3##^hol>T^ zFO>*v?#n3Pf=N-{vyg?E!7Y+Xo}J^X`8nUM>g^c|IoxKr?0CGAukY@PKvW7fUM21B zP5<-wX5u9+pww3OJ3bU~5VIEMU^5KY{R&rNORBvU{#rz!D_#&u!7!eH{~OXwNn_o$ za1ZY@3{{2F$|M?{kh>s0?M9N>R*X0w(y~n7V-a1(KP_89 z%>N8U--5a`QP3N@+3DRz4_$M#-$f&v zzKp)@xno$r&>)2#dUr&N7B11)iVe_<%QU0QkepNKi>y-U$3O(MMBy}0Hg}4_?PCnR z#c(b=@)l#~s67#-EZO%FR(}h6wuwGY=;{J8O z#oVtLzTLnb{Y|GEn9trn!3TQQ(c7V?4{3T%WGGT(khB;k$m-(fiI5n<%>HfD6P)^ wOm?xvu^1B+`6i_}8O6=TI7wVW#J6;2^qZrz`wrie=L9{kh>I5chX{-R1sJu(3jhEB literal 0 HcmV?d00001 diff --git a/bin/main.class b/bin/main.class index b021deaa939449f0a221cd9bca5160172bc036b6..88f95e184fedc20fe7e90a8b5e8e7709e6505e57 100644 GIT binary patch delta 254 zcmcc3x`CDJ)W2Q(7#J8#8FVLd{nF?3%qvUHE6FdaGtn+utV;6`7-jr2v- zg@S?);3MdpxDco9Rt_Ba&-u>v(fl^)=kMDGfIaL!(4T#?r9>ocOcONNMG^^ip7Jzh zheBSdBxT2WEM&k_anNdqfn)=mQMj0OV4~W)CzZP*=CNpDfnfZSUvt*wafkUgQnz+c z!xBLu$rZtrH%NyGYiD7se3c0vHEpaA*nS=aLdqJ&8tN9-3CjP#4mMCFu+n~gHzpY2 zS*I>Fd^YSo9IbS^(>(YB+t0rm%OBT$e4 diff --git a/src/Inventory.java b/src/Inventory.java index 5a92901..f508248 100644 --- a/src/Inventory.java +++ b/src/Inventory.java @@ -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; + } } diff --git a/src/Item.java b/src/InventoryItem.java similarity index 94% rename from src/Item.java rename to src/InventoryItem.java index 2fa529c..af0f61b 100644 --- a/src/Item.java +++ b/src/InventoryItem.java @@ -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; } diff --git a/src/Items.java b/src/InventoryItems.java similarity index 83% rename from src/Items.java rename to src/InventoryItems.java index 0810c75..da8483a 100644 --- a/src/Items.java +++ b/src/InventoryItems.java @@ -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 items; + HashMap 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(); + this.items = new HashMap(); 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); } diff --git a/src/ItemsTest.java b/src/InventoryItemsTest.java similarity index 92% rename from src/ItemsTest.java rename to src/InventoryItemsTest.java index 5458a7c..f182752 100644 --- a/src/ItemsTest.java +++ b/src/InventoryItemsTest.java @@ -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/"; } diff --git a/src/InventorySlot.java b/src/InventorySlot.java index 8e56fa9..834eb3b 100644 --- a/src/InventorySlot.java +++ b/src/InventorySlot.java @@ -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; - } } diff --git a/src/InventorySlotTest.java b/src/InventorySlotTest.java deleted file mode 100644 index f749035..0000000 --- a/src/InventorySlotTest.java +++ /dev/null @@ -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)); - } - -} diff --git a/src/InventoryTest.java b/src/InventoryTest.java new file mode 100644 index 0000000..c3fe3db --- /dev/null +++ b/src/InventoryTest.java @@ -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; + } + +} diff --git a/src/main.java b/src/main.java index 31beb20..6ca5b39 100644 --- a/src/main.java +++ b/src/main.java @@ -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!");