mirror of
https://github.com/davpapp/PowerMiner
synced 2025-01-08 12:28:06 -05:00
Fixed BufferedImage conversion issues. Model now working correctly.
This commit is contained in:
parent
d3e1d42623
commit
8dbc60a00f
@ -30,25 +30,29 @@ public class IronMiner {
|
||||
|
||||
public IronMiner() throws AWTException, IOException
|
||||
{
|
||||
cursor = new Cursor();
|
||||
cursorTask = new CursorTask();
|
||||
inventory = new Inventory();
|
||||
//cursor = new Cursor();
|
||||
//cursorTask = new CursorTask();
|
||||
//inventory = new Inventory();
|
||||
objectDetector = new ObjectDetector();
|
||||
robot = new Robot();
|
||||
randomizer = new Randomizer();
|
||||
}
|
||||
|
||||
public void run() throws Exception {
|
||||
|
||||
while (true) {
|
||||
int count = 0;
|
||||
long mineStartTime = System.currentTimeMillis();
|
||||
|
||||
while (System.currentTimeMillis() - 60000 < mineStartTime) {
|
||||
count++;
|
||||
BufferedImage screenCapture = objectDetector.captureScreenshotGameWindow();
|
||||
ArrayList<DetectedObject> detectedObjects = objectDetector.getObjectsInImage(screenCapture);
|
||||
ArrayList<DetectedObject> ironOres = objectDetector.getObjectsOfClassInList(detectedObjects, "ironOre");
|
||||
|
||||
DetectedObject closestIronOre = getClosestObjectToCharacter(ironOres);
|
||||
//ArrayList<DetectedObject> ironOres = objectDetector.getObjectsOfClassInList(detectedObjects, "ironOre");
|
||||
System.out.println("Count: " + count);
|
||||
System.out.println(detectedObjects.size());
|
||||
/*DetectedObject closestIronOre = getClosestObjectToCharacter(ironOres);
|
||||
if (closestIronOre != null) {
|
||||
Tracker objectTracker = TrackerKCF.create();
|
||||
Rect2d boundingBox = closestIronOre.getBoundingRect2d();
|
||||
//Tracker objectTracker = TrackerKCF.create();
|
||||
//Rect2d boundingBox = closestIronOre.getBoundingRect2d();
|
||||
objectTracker.init(getMatFromBufferedImage(screenCapture), boundingBox);
|
||||
|
||||
cursor.moveAndLeftClickAtCoordinatesWithRandomness(closestIronOre.getCenterForClicking(), 10, 10);
|
||||
@ -67,7 +71,7 @@ public class IronMiner {
|
||||
}
|
||||
}
|
||||
|
||||
dropInventoryIfFull();
|
||||
dropInventoryIfFull();*/
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,6 +14,7 @@ limitations under the License.
|
||||
==============================================================================*/
|
||||
|
||||
import java.awt.AWTException;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Point;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.Robot;
|
||||
@ -110,30 +111,39 @@ public class ObjectDetector {
|
||||
}
|
||||
|
||||
private static Tensor<UInt8> makeImageTensor(BufferedImage image) throws IOException {
|
||||
BufferedImage img = ImageIO.read(image);
|
||||
if (img.getType() != BufferedImage.TYPE_3BYTE_BGR) {
|
||||
/*if (image.getType() != BufferedImage.TYPE_3BYTE_BGR) {
|
||||
throw new IOException(
|
||||
String.format(
|
||||
"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.
|
||||
bgr2rgb(data);
|
||||
final long BATCH_SIZE = 1;
|
||||
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));
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
private static BufferedImage convertBufferedImage(BufferedImage sourceImage, int bufferedImageType) {
|
||||
BufferedImage image = new BufferedImage(sourceImage.getWidth(), sourceImage.getHeight(), bufferedImageType);
|
||||
Graphics2D g2d = image.createGraphics();
|
||||
g2d.drawImage(sourceImage, 0, 0, null);
|
||||
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 {
|
||||
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);
|
||||
|
@ -11,5 +11,5 @@ class ObjectDetectorTest {
|
||||
ObjectDetector objectDetector = new ObjectDetector();
|
||||
ArrayList<DetectedObject> detectedObjects1 = objectDetector.getObjectsInImage(loadimages here in bufferedimage format));
|
||||
|
||||
}
|
||||
}va
|
||||
}
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user