Started work on adding Gaussian translation to CursorPath

master
davpapp 2018-02-04 12:18:34 -05:00
parent 88b16aeb5f
commit c24f8588de
20 changed files with 383 additions and 9 deletions

View File

@ -6,6 +6,11 @@
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="lib" path="/home/dpapp/lib/Jama-1.0.3.jar" sourcepath="/home/dpapp/lib/Jama">
<attributes>
<attribute name="module" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/>
<classpathentry kind="output" path="bin"/>
</classpath>

View File

@ -1,5 +1,6 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate
org.eclipse.jdt.core.compiler.codegen.targetPlatform=9
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=9

View File

@ -0,0 +1,17 @@
import numpy as np
import cv2
willowCascade = cv2.CascadeClassifier('/home/dpapp/open/opencv-haar-classifier-training/classifier/stage9.xml')
img = cv2.imread('/home/dpapp/Desktop/RunescapeAIPics/CascadeTraining/Testing/screenshot0.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
willows = willowCascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in willows:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
print("Found willow!")
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

View File

@ -0,0 +1,17 @@
import numpy as np
import cv2
willowCascade = cv2.CascadeClassifier('/home/dpapp/open/opencv-haar-classifier-training/classifier/stage19.xml')
img = cv2.imread('/home/dpapp/Desktop/RunescapeAIPics/CascadeTraining/Testing/screenshot0.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
willows = willowCascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in willows:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
print("Found willow!")
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,130 @@
import java.awt.AWTException;
import java.awt.Color;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import javax.imageio.ImageIO;
public class ColorAnalyzer {
String inputImageDirectory;
public ColorAnalyzer() {
inputImageDirectory = "/home/dpapp/Desktop/RunescapeAIPics/colorDetection/";
}
public BufferedImage readImageFromFile(String imageFileName) throws IOException {
File imageFile = new File(getImagePath(imageFileName));
return ImageIO.read(imageFile);
}
private String getImagePath(String imageFileName) {
return this.inputImageDirectory + imageFileName;
}
public void showColorDistribution(String imageName) throws IOException {
HashMap<Integer, Integer> colorFrequencies = new HashMap<Integer, Integer>();
BufferedImage image = readImageFromFile(imageName);
for (int row = 0; row < image.getWidth(); row++) {
for (int col = 0; col < image.getHeight(); col++) {
int color = image.getRGB(row, col); //getRGBValuesFromPixel(image.getRGB(row, col));
int count = colorFrequencies.containsKey(color) ? colorFrequencies.get(color) : 0;
colorFrequencies.put(color, count + 1);
}
}
System.out.println(image.getWidth() + ", " + image.getHeight());
displayColorDistribution(colorFrequencies);
}
public void displayColorDistribution(HashMap<Integer, Integer> colorFrequencies) {
int numDifferentColors = 0;
for (HashMap.Entry<Integer, Integer> entry: colorFrequencies.entrySet()) {
numDifferentColors++;
int[] color = getRGBValuesFromPixel(entry.getKey());
int count = entry.getValue();
System.out.println("(" + color[0] + "," + color[1] + "," + color[2] + "): " + count);
}
System.out.println("Number of different colors: " + numDifferentColors);
}
private int[] getRGBValuesFromPixel(int pixel) {
int[] colors = {(pixel)&0xFF, (pixel>>8)&0xFF, (pixel>>16)&0xFF, (pixel>>24)&0xFF};
return colors;
}
private boolean pixelsAreWithinRGBTolerance(int rgb1, int rgb2, int tolerance) {
int[] colors1 = getRGBValuesFromPixel(rgb1);
int[] colors2 = getRGBValuesFromPixel(rgb2);
for (int i = 0; i < 3; i++) {
if (Math.abs(colors1[i] - colors2[i]) > tolerance) {
return false;
}
}
return true;
}
public void printCursorColor() throws IOException, InterruptedException, AWTException {
BufferedImage image;
Robot robot = new Robot();
while (true) {
image = robot.createScreenCapture(new Rectangle(0, 0, 1920, 1080));
Point cursorPosition = getCurrentCursorPoint();
int[] colors = getRGBValuesFromPixel(image.getRGB(cursorPosition.x, cursorPosition.y));
//System.out.println(colors[0] + "," + colors[1] + "," + colors[2]);
System.out.println("(" + cursorPosition.x + "," + cursorPosition.y + "), Color: (" + colors[0] + "," + colors[1] + "," + colors[2]);
Thread.sleep(100);
}
}
private boolean isColorInRange(int rgb1) {
int colors[] = {makeRGBIntFromComponents(47,88,81),
makeRGBIntFromComponents(46,88,81)};
for (int color : colors) {
if (pixelsAreWithinRGBTolerance(rgb1, color, 10)) {
return true;
}
}
return false;
}
public void colorImage() throws IOException {
BufferedImage image = readImageFromFile("screenshot0.png");
for (int row = 0; row < image.getWidth(); row++) {
for (int col = 0; col < image.getHeight(); col++) {
if (isColorInRange(image.getRGB(row, col))) {
System.out.println("match!");
image.setRGB(row, col, makeRGBIntFromComponents(255, 0, 0));
}
}
}
ImageIO.write(image, "png", new File(getImageName("filtered")));
}
private int makeRGBIntFromComponents(int red, int green, int blue) {
return red*65536 + green*256 + blue;
}
private String getImageName(String imageName) {
return inputImageDirectory + imageName + ".png";
}
public Point getCurrentCursorPoint() {
return MouseInfo.getPointerInfo().getLocation();
}
public static void main(String[] args) throws Exception
{
ColorAnalyzer colorAnalyzer = new ColorAnalyzer();
//colorAnalyzer.showColorDistribution("screenshot21.jpg");
//colorAnalyzer.printCursorColor();
colorAnalyzer.colorImage();
}
}

View File

@ -146,15 +146,35 @@ public class Cursor {
}
private CursorPath chooseCursorPathToFollowBasedOnDistance(int distanceToMoveCursor) {
ArrayList<CursorPath> cursorPathsWithSameDistance = cursorPathsByDistance.get(distanceToMoveCursor);
// TODO: Error check if path of this size exists
if (cursorPathsWithSameDistance.size() == 0) {
System.out.println("No movement required! Returning empty list of CursorPoints.");
if (distanceToMoveCursor == 0) {
return new CursorPath(new ArrayList<CursorPoint>());
}
return cursorPathsWithSameDistance.get(new Random().nextInt(cursorPathsWithSameDistance.size()));
int newDistanceToMoveCursor = findNearestPathLengthThatExists(distanceToMoveCursor);
double scaleFactor = getScaleFactor(newDistanceToMoveCursor, distanceToMoveCursor);
ArrayList<CursorPath> cursorPathsWithSameDistance = cursorPathsByDistance.get(newDistanceToMoveCursor);
CursorPath scaledCursorPath = cursorPathsWithSameDistance.get(random.nextInt(cursorPathsWithSameDistance.size())).getScaledCopyOfCursorPath(1.0);
return scaledCursorPath;
}
private int findNearestPathLengthThatExists(int distanceToMoveCursor) {
int offset = 1;
while (cursorPathsByDistance.get(distanceToMoveCursor + offset).size() == 0) {
if (offset > 0) {
offset = -(offset + 1);
}
else {
offset = -offset + 1;
}
}
return distanceToMoveCursor + offset;
}
private double getScaleFactor(int newDistanceToMoveCursor, int distanceToMoveCursor) {
return (1.0 * newDistanceToMoveCursor / distanceToMoveCursor);
}
private int calculateDistanceBetweenPoints(Point a, Point b) {
return (int) (Math.hypot(a.x - b.x, a.y - b.y));
}

View File

@ -100,6 +100,14 @@ public class CursorPath {
return pathTheta;
}
public CursorPath getScaledCopyOfCursorPath(double scaleFactor) {
ArrayList<CursorPoint> scaledCursorPath = new ArrayList<CursorPoint>();
for (CursorPoint cursorPoint : this.pathCursorPoints) {
scaledCursorPath.add(new CursorPoint((int) (cursorPoint.x * scaleFactor), (int) (cursorPoint.y * scaleFactor), (int) (cursorPoint.postMillisecondDelay * scaleFactor)));
}
return new CursorPath(scaledCursorPath);
}
public void displayCursorPoints() {
for (CursorPoint p : pathCursorPoints) {
p.display();

View File

@ -59,10 +59,6 @@ public class Inventory {
updateAllInventorySlots(image);
}
private String getImageName() {
return ("/home/dpapp/Desktop/RunescapeAIPics/Tests/Inventory/inventory.png");
}
// For testing only
public void updateWithFakeImageForTests(BufferedImage testImage) throws IOException {
updateAllInventorySlots(testImage);

View File

@ -0,0 +1,41 @@
import java.awt.Point;
import java.util.Random;
import Jama.Matrix;
public class Randomizer {
Random random;
public Randomizer() {
random = new Random();
}
public int nextGaussianWithinThreeSTDs(int mean, int STD) {
int result = (int) (random.nextGaussian() * STD + mean);
while (result > (mean + 3 * STD) || result < (mean - 3 * STD)) {
result = (int) random.nextGaussian() * STD + mean;
}
return result;
}
public Point generatePeakForTransformationParabola(int pathDistance) {
double maxTransformationScale = 0.2;
int peakX = nextGaussianWithinThreeSTDs(pathDistance / 2, pathDistance / 6);
int peakY = (int) (random.nextGaussian() * maxTransformationScale);
return new Point(peakX, peakY);
}
public double[] generateParabolaEquation(int pathDistance, Point peakPoint) {
double[][] lhsMatrix = {{0, 0, 1}, {peakPoint.x * peakPoint.x, peakPoint.x, 1}, {pathDistance * pathDistance, pathDistance, 1}};
double[][] rhsMatrix = {{0, peakPoint.y, 0}};
Matrix lhs = new Matrix(lhsMatrix);
Matrix rhs = new Matrix(rhsMatrix);
Matrix ans = lhs.solve(rhs);
double[] result = {ans.get(0, 0), ans.get(1, 0), ans.get(2, 0)};
return result;
}
//public Point transformPoint
}

View File

@ -0,0 +1,29 @@
import static org.junit.jupiter.api.Assertions.*;
import java.awt.Point;
import org.junit.jupiter.api.Test;
class RandomizerTest {
@Test
void test() {
Randomizer randomizer = new Randomizer();
double[] parabolaEquation1 = randomizer.generateParabolaEquation(100, new Point(50, 0));
double[] expectedResult1 = {0, 0, 0};
double[] parabolaEquation2 = randomizer.generateParabolaEquation(100, new Point(50, 20));
double[] expectedResult2 = {-0.008, 0.008, 0};
double[] parabolaEquation3 = randomizer.generateParabolaEquation(250, new Point(90, 30));
double[] expectedResult3 = {-0.002083, 0.52083, 0.0};
checkVariables(expectedResult1, parabolaEquation1);
checkVariables(expectedResult2, parabolaEquation2);
checkVariables(expectedResult3, parabolaEquation3);
}
void checkVariables(double[] expected, double[] actual) {
assertEquals(0, 0, 0);
}
}

View File

@ -0,0 +1,37 @@
import java.awt.AWTException;
import java.io.IOException;
public class WillowChopper {
Cursor cursor;
CursorTask cursorTask;
Inventory inventory;
public WillowChopper() throws AWTException, IOException
{
cursor = new Cursor();
cursorTask = new CursorTask();
inventory = new Inventory();
}
public void run() throws IOException, InterruptedException {
while (true) {
/*
if (character.isCharacterEngaged()) {
// DO NOTHING
// do things like checking the inventory
}
else {
find closest willow tree
chop willow tree
}
*/
inventory.update();
if (inventory.isInventoryFull()) {
cursorTask.dropAllItemsInInventory(cursor, inventory);
}
}
}
}

View File

@ -0,0 +1,73 @@
import java.awt.AWTException;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class cascadeTrainingImageCollector {
public String imageOutputDirectory;
public Robot robot;
public int imageDimension;
public cascadeTrainingImageCollector(String imageOutputDirectory) throws AWTException {
this.imageOutputDirectory = imageOutputDirectory;
this.robot = new Robot();
this.imageDimension = 75;
}
public void captureEveryNSeconds(int n) throws IOException, InterruptedException {
for (int i = 0; i < 60; i++) {
captureScreenshotAroundMouse(i);
System.out.println(i);
Thread.sleep(n * 1000);
}
}
public void captureWindowEveryNMilliseconds(int n) throws InterruptedException, IOException {
for (int i = 0; i < 1000; i++) {
captureScreenshotGameWindow(i);
System.out.println(i);
//System.out.println("Created image: " + getImageName(i));
Thread.sleep(n * 5000);
}
}
private void captureScreenshotGameWindow(int counter) throws IOException {
Rectangle area = new Rectangle(103, 85, 510, 330);
BufferedImage image = robot.createScreenCapture(area);
ImageIO.write(image, "jpg", new File(getImageName(counter)));
}
private void captureScreenshotAroundMouse(int counter) throws IOException {
BufferedImage image = robot.createScreenCapture(getRectangleAroundCursor());
ImageIO.write(image, "jpg", new File(getImageName(counter)));
}
private Rectangle getRectangleAroundCursor() {
Point cursorPoint = getCurrentCursorPoint();
return new Rectangle(cursorPoint.x - imageDimension / 2, cursorPoint.y - imageDimension / 2, imageDimension, imageDimension);
}
private Point getCurrentCursorPoint() {
return MouseInfo.getPointerInfo().getLocation();
}
private String getImageName(int counter) {
return imageOutputDirectory + "screenshot" + counter + ".jpg";
}
public static void main(String[] args) throws Exception
{
System.out.println("Starting image collection...");
//cascadeTrainingImageCollector imageCollector = new cascadeTrainingImageCollector("/home/dpapp/Desktop/RunescapeAIPics/CascadeTraining/PositiveSamples/");
//imageCollector.captureEveryNSeconds(30);
cascadeTrainingImageCollector imageCollector = new cascadeTrainingImageCollector("/home/dpapp/Desktop/RunescapeAIPics/CascadeTraining/Testing/");
imageCollector.captureWindowEveryNMilliseconds(1);
}
}