Refactoring cursor code
parent
e6ab945729
commit
90c8cbb33b
@ -1,17 +1,18 @@
|
||||
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)
|
||||
willowCascade = cv2.CascadeClassifier('/home/dpapp/open/opencv-haar-classifier-training/classifier/stage8.xml')
|
||||
img = cv2.imread('/home/dpapp/Desktop/RunescapeAIPics/CascadeTraining/Testing/screenshot1.jpg')
|
||||
#gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
||||
|
||||
willows = willowCascade.detectMultiScale(gray, 1.3, 5)
|
||||
willows = willowCascade.detectMultiScale(img, 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.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,85 @@
|
||||
import java.awt.AWTException;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.Robot;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.FilenameFilter;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
public class ImageCollector {
|
||||
|
||||
public String screenshotOutputDirectory;
|
||||
public Rectangle gameWindowRectangle;
|
||||
|
||||
/*
|
||||
* Methods needed:
|
||||
*
|
||||
* - Capture screen window every N seconds
|
||||
*
|
||||
* initialize with: output directory, file name
|
||||
*
|
||||
* detect last file name
|
||||
*/
|
||||
|
||||
public ImageCollector(String screenshotOutputDirectory) {
|
||||
initializeGameWindowRectangle();
|
||||
this.screenshotOutputDirectory = screenshotOutputDirectory;
|
||||
}
|
||||
|
||||
private void initializeGameWindowRectangle() {
|
||||
this.gameWindowRectangle = new Rectangle(103, 85, 510, 330);
|
||||
}
|
||||
|
||||
public void collectImages(String itemName) throws IOException, InterruptedException, AWTException {
|
||||
int itemCounter = getItemCounter(itemName);
|
||||
int numImagesToCapture = 50;
|
||||
for (int counter = itemCounter + 1; counter < itemCounter + numImagesToCapture + 1; counter++) {
|
||||
captureAndSaveGameWindow(itemName, counter);
|
||||
Thread.sleep(2000);
|
||||
}
|
||||
}
|
||||
|
||||
private int getItemCounter(String itemName) {
|
||||
File[] listOfFiles = getFilesFromFolderThatStartWith(itemName);
|
||||
int counter = 0;
|
||||
for (File file : listOfFiles) {
|
||||
counter = Math.max(counter, getItemNumberFromFile(file.getName()));
|
||||
}
|
||||
return counter;
|
||||
}
|
||||
|
||||
private File[] getFilesFromFolderThatStartWith(String itemName) {
|
||||
File folder = new File(screenshotOutputDirectory);
|
||||
File[] listOfFiles = folder.listFiles(new FilenameFilter() {
|
||||
public boolean accept(File file, String name) {
|
||||
return name.startsWith(itemName);
|
||||
}
|
||||
});
|
||||
return listOfFiles;
|
||||
}
|
||||
|
||||
private int getItemNumberFromFile(String fileName) {
|
||||
String itemNumber = fileName.substring(fileName.indexOf("_") + 1, fileName.indexOf("."));
|
||||
return Integer.parseInt(itemNumber);
|
||||
}
|
||||
|
||||
private void captureAndSaveGameWindow(String itemName, int fileCounter) throws IOException, InterruptedException, AWTException {
|
||||
Robot robot = new Robot();
|
||||
BufferedImage imageCaptured = robot.createScreenCapture(gameWindowRectangle);
|
||||
String fileName = getFileName(itemName, fileCounter);
|
||||
ImageIO.write(imageCaptured, "jpg", new File(fileName));
|
||||
System.out.println("Wrote file: " + fileName);
|
||||
}
|
||||
|
||||
private String getFileName(String itemName, int counter) {
|
||||
return screenshotOutputDirectory + itemName + "_" + counter + ".jpg";
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
ImageCollector imageCollector = new ImageCollector("/home/dpapp/Desktop/RunescapeAI/TensorFlow/IronOre/");
|
||||
imageCollector.collectImages("ironOre");
|
||||
}
|
||||
}
|
@ -1,56 +0,0 @@
|
||||
import java.awt.AWTException;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.Robot;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Scanner;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
public class ScreenshotAutomator {
|
||||
|
||||
public String screenshotOutputDirectory;
|
||||
public Robot robot;
|
||||
public Rectangle screenAreaToCapture;
|
||||
|
||||
public ScreenshotAutomator(String screenshotOutputDirectory) throws AWTException {
|
||||
this.screenshotOutputDirectory = screenshotOutputDirectory;
|
||||
this.screenAreaToCapture = new Rectangle(0, 0, 1920 / 2, 1080);
|
||||
this.robot = new Robot();
|
||||
}
|
||||
|
||||
public void captureEveryNSeconds(int n) throws IOException, InterruptedException {
|
||||
for (int i = 0; i < 20; i++) {
|
||||
captureScreenshot(i);
|
||||
System.out.println("Created image: " + getImageName(i));
|
||||
Thread.sleep(n * 1000);
|
||||
}
|
||||
}
|
||||
|
||||
public void captureOnKeyboardInput() throws IOException, InterruptedException {
|
||||
int counter = 0;
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
while (true) {
|
||||
captureScreenshot(counter);
|
||||
scanner.nextLine();
|
||||
System.out.println("Created image: " + getImageName(counter));
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
|
||||
private void captureScreenshot(int counter) throws IOException {
|
||||
BufferedImage image = robot.createScreenCapture(this.screenAreaToCapture);
|
||||
ImageIO.write(image, "png", new File(getImageName(counter)));
|
||||
}
|
||||
|
||||
private String getImageName(int counter) {
|
||||
return screenshotOutputDirectory + "screenshot" + counter + ".png";
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception
|
||||
{
|
||||
ScreenshotAutomator screenshotAutomator = new ScreenshotAutomator("/home/dpapp/Desktop/RunescapeAIPics/");
|
||||
screenshotAutomator.captureOnKeyboardInput();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue