Fixed BufferedImage conversion issues. Model now working correctly.

This commit is contained in:
davpapp 2018-02-23 19:48:16 -05:00
parent d3e1d42623
commit 8dbc60a00f
6 changed files with 38 additions and 24 deletions

View File

@ -30,25 +30,29 @@ public class IronMiner {
public IronMiner() throws AWTException, IOException public IronMiner() throws AWTException, IOException
{ {
cursor = new Cursor(); //cursor = new Cursor();
cursorTask = new CursorTask(); //cursorTask = new CursorTask();
inventory = new Inventory(); //inventory = new Inventory();
objectDetector = new ObjectDetector(); objectDetector = new ObjectDetector();
robot = new Robot(); robot = new Robot();
randomizer = new Randomizer(); randomizer = new Randomizer();
} }
public void run() throws Exception { public void run() throws Exception {
int count = 0;
while (true) { long mineStartTime = System.currentTimeMillis();
while (System.currentTimeMillis() - 60000 < mineStartTime) {
count++;
BufferedImage screenCapture = objectDetector.captureScreenshotGameWindow(); BufferedImage screenCapture = objectDetector.captureScreenshotGameWindow();
ArrayList<DetectedObject> detectedObjects = objectDetector.getObjectsInImage(screenCapture); ArrayList<DetectedObject> detectedObjects = objectDetector.getObjectsInImage(screenCapture);
ArrayList<DetectedObject> ironOres = objectDetector.getObjectsOfClassInList(detectedObjects, "ironOre"); //ArrayList<DetectedObject> ironOres = objectDetector.getObjectsOfClassInList(detectedObjects, "ironOre");
System.out.println("Count: " + count);
DetectedObject closestIronOre = getClosestObjectToCharacter(ironOres); System.out.println(detectedObjects.size());
/*DetectedObject closestIronOre = getClosestObjectToCharacter(ironOres);
if (closestIronOre != null) { if (closestIronOre != null) {
Tracker objectTracker = TrackerKCF.create(); //Tracker objectTracker = TrackerKCF.create();
Rect2d boundingBox = closestIronOre.getBoundingRect2d(); //Rect2d boundingBox = closestIronOre.getBoundingRect2d();
objectTracker.init(getMatFromBufferedImage(screenCapture), boundingBox); objectTracker.init(getMatFromBufferedImage(screenCapture), boundingBox);
cursor.moveAndLeftClickAtCoordinatesWithRandomness(closestIronOre.getCenterForClicking(), 10, 10); cursor.moveAndLeftClickAtCoordinatesWithRandomness(closestIronOre.getCenterForClicking(), 10, 10);
@ -67,7 +71,7 @@ public class IronMiner {
} }
} }
dropInventoryIfFull(); dropInventoryIfFull();*/
} }
} }

View File

@ -14,6 +14,7 @@ limitations under the License.
==============================================================================*/ ==============================================================================*/
import java.awt.AWTException; import java.awt.AWTException;
import java.awt.Graphics2D;
import java.awt.Point; import java.awt.Point;
import java.awt.Rectangle; import java.awt.Rectangle;
import java.awt.Robot; import java.awt.Robot;
@ -110,30 +111,39 @@ public class ObjectDetector {
} }
private static Tensor<UInt8> makeImageTensor(BufferedImage image) throws IOException { private static Tensor<UInt8> makeImageTensor(BufferedImage image) throws IOException {
BufferedImage img = ImageIO.read(image); /*if (image.getType() != BufferedImage.TYPE_3BYTE_BGR) {
if (img.getType() != BufferedImage.TYPE_3BYTE_BGR) {
throw new IOException( throw new IOException(
String.format( String.format(
"Expected 3-byte BGR encoding in BufferedImage, found %d (file: %s). This code could be made more robust")); "Expected 3-byte BGR encoding in BufferedImage, found %d (file: %s). This code could be made more robust"));
} }*/
byte[] data = ((DataBufferByte) img.getData().getDataBuffer()).getData();
BufferedImage formattedImage = convertBufferedImage(image, BufferedImage.TYPE_3BYTE_BGR);
byte[] data = ((DataBufferByte) formattedImage.getData().getDataBuffer()).getData();
bgr2rgb(data);
// ImageIO.read seems to produce BGR-encoded images, but the model expects RGB. // ImageIO.read seems to produce BGR-encoded images, but the model expects RGB.
bgr2rgb(data);
final long BATCH_SIZE = 1; final long BATCH_SIZE = 1;
final long CHANNELS = 3; final long CHANNELS = 3;
long[] shape = new long[] {BATCH_SIZE, img.getHeight(), img.getWidth(), CHANNELS}; long[] shape = new long[] {BATCH_SIZE, formattedImage.getHeight(), formattedImage.getWidth(), CHANNELS};
return Tensor.create(UInt8.class, shape, ByteBuffer.wrap(data)); return Tensor.create(UInt8.class, shape, ByteBuffer.wrap(data));
} }
private static void bgr2rgb(byte[] data) { private static BufferedImage convertBufferedImage(BufferedImage sourceImage, int bufferedImageType) {
for (int i = 0; i < data.length; i += 3) { BufferedImage image = new BufferedImage(sourceImage.getWidth(), sourceImage.getHeight(), bufferedImageType);
byte tmp = data[i]; Graphics2D g2d = image.createGraphics();
data[i] = data[i + 2]; g2d.drawImage(sourceImage, 0, 0, null);
data[i + 2] = tmp; g2d.dispose();
} return image;
} }
private static void bgr2rgb(byte[] data) {
for (int i = 0; i < data.length; i += 3) {
byte tmp = data[i];
data[i] = data[i + 2];
data[i + 2] = tmp;
}
}
public BufferedImage captureScreenshotGameWindow() throws IOException, AWTException { public BufferedImage captureScreenshotGameWindow() throws IOException, AWTException {
Rectangle area = new Rectangle(Constants.GAME_WINDOW_OFFSET_X, Constants.GAME_WINDOW_OFFSET_Y, Constants.GAME_WINDOW_WIDTH, Constants.GAME_WINDOW_HEIGHT); Rectangle area = new Rectangle(Constants.GAME_WINDOW_OFFSET_X, Constants.GAME_WINDOW_OFFSET_Y, Constants.GAME_WINDOW_WIDTH, Constants.GAME_WINDOW_HEIGHT);
return robot.createScreenCapture(area); return robot.createScreenCapture(area);

View File

@ -11,5 +11,5 @@ class ObjectDetectorTest {
ObjectDetector objectDetector = new ObjectDetector(); ObjectDetector objectDetector = new ObjectDetector();
ArrayList<DetectedObject> detectedObjects1 = objectDetector.getObjectsInImage(loadimages here in bufferedimage format)); ArrayList<DetectedObject> detectedObjects1 = objectDetector.getObjectsInImage(loadimages here in bufferedimage format));
} }va
} }

Binary file not shown.

Binary file not shown.