Started writing tests for object detection

This commit is contained in:
davpapp 2018-02-23 17:04:28 -05:00
parent 168f42cc86
commit d38d4afe54
8 changed files with 34 additions and 8 deletions

View File

@ -41,7 +41,7 @@ public class DetectedObject {
return boundingBox;
}
private Rect2d getBoundingRect2d() {
public Rect2d getBoundingRect2d() {
return new Rect2d(boundingBox.x, boundingBox.y, boundingBox.x + boundingBox.width, boundingBox.y + boundingBox.height);
}

View File

@ -3,12 +3,14 @@ import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import org.opencv.core.Mat;
import org.opencv.core.Rect2d;
import org.opencv.tracking.Tracker;
import org.opencv.tracking.TrackerKCF;
@ -46,18 +48,19 @@ public class IronMiner {
DetectedObject closestIronOre = getClosestObjectToCharacter(ironOres);
if (closestIronOre != null) {
Tracker objectTracker = TrackerKCF.create();
objectTracker.init(screenCapture, closestIronOre.getBoundingRect2d());
Rect2d boundingBox = closestIronOre.getBoundingRect2d();
objectTracker.init(getMatFromBufferedImage(screenCapture), boundingBox);
cursor.moveAndLeftClickAtCoordinatesWithRandomness(closestIronOre.getCenterForClicking(), 10, 10);
long mineStartTime = System.currentTimeMillis();
int maxTimeToMine = randomizer.nextGaussianWithinRange(3500, 5000);
// track until either we lose the object or too much time passes
while ((System.currentTimeMillis() - mineStartTime) < maxTimeToMine) {
screenCapture = objectDetector.captureScreenshotGameWindow();
objectTracker.update(screenCapture, boundingBox);
Rectangle newBounds = new Rectangle();
if (!objectDetector.isObjectPresentInBoundingBoxInImage(screenCapture, newBounds, "ironOre")) {
boolean ok = objectTracker.update(getMatFromBufferedImage(screenCapture), boundingBox);
if (!ok || !objectDetector.isObjectPresentInBoundingBoxInImage(screenCapture, boundingBox, "ironOre")) {
System.out.println("Lost track! Finding new ore.");
break;
}
@ -101,6 +104,13 @@ public class IronMiner {
return null;
}
public Mat getMatFromBufferedImage(BufferedImage image) {
Mat matImage = new Mat();
byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
matImage.put(0, 0, pixels);
return matImage;
}
public int getDistanceBetweenPoints(Point startingPoint, Point goalPoint) {
return (int) (Math.hypot(goalPoint.x - startingPoint.x, goalPoint.y - startingPoint.y));
}

View File

@ -32,6 +32,7 @@ import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
import org.opencv.core.Rect2d;
import org.tensorflow.SavedModelBundle;
import org.tensorflow.Tensor;
import org.tensorflow.types.UInt8;
@ -92,8 +93,8 @@ public class ObjectDetector {
return detectedObjectsInImage;
}
public boolean isObjectPresentInBoundingBoxInImage(BufferedImage image, Rectangle boundingBox, String objectClass) throws Exception {
BufferedImage subImage = image.getSubimage(boundingBox.x, boundingBox.y, boundingBox.width, boundingBox.height);
public boolean isObjectPresentInBoundingBoxInImage(BufferedImage image, Rect2d boundingBox, String objectClass) throws Exception {
BufferedImage subImage = image.getSubimage((int) boundingBox.x, (int) boundingBox.y, (int) boundingBox.width, (int) boundingBox.height);
ArrayList<DetectedObject> detectedObjectsInSubImage = getObjectsInImage(subImage);
return (getObjectsOfClassInList(detectedObjectsInSubImage, objectClass).size() != 0);
}

View File

@ -0,0 +1,15 @@
import static org.junit.jupiter.api.Assertions.*;
import java.awt.AWTException;
import org.junit.jupiter.api.Test;
class ObjectDetectorTest {
@Test
void testObjectDetection() throws AWTException {
ObjectDetector objectDetector = new ObjectDetector();
ArrayList<DetectedObject> detectedObjects1 = objectDetector.getObjectsInImage(loadimages here in bufferedimage format));
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.