Fix match template

This commit is contained in:
srose 2024-09-05 00:47:18 -04:00
parent 05c04e3bac
commit 369fe8d316
15 changed files with 18 additions and 45 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.1 MiB

BIN
assets/screenshot.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 MiB

View File

@ -53,7 +53,8 @@ class FishingAgent:
while True: while True:
time.sleep(1) time.sleep(1)
self.logger.log("###") self.logger.log("###")
self.monitor.findMatch(self.fishingBobberImg) point = self.monitor.findMatch(self.fishingBobberImg)
pyautogui.moveTo(point[0], point[1])
def run(self): def run(self):
if self.monitor.screenshot is None: if self.monitor.screenshot is None:

View File

@ -8,6 +8,6 @@ class MainAgent:
def startFishBot(self): def startFishBot(self):
self.monitor.startScreenCaptureThread() self.monitor.startScreenCaptureThread()
self.fishingBot.test() self.fishingBot.run()

View File

@ -1,11 +1,9 @@
import time import time
import os
from threading import Thread from threading import Thread
from screeninfo import get_monitors from screeninfo import get_monitors
import pyautogui import pyautogui
import numpy as np import numpy as np
import cv2 as cv import cv2 as cv
from matplotlib import pyplot as plt
from core.Logger import Logger from core.Logger import Logger
FPS_REPORT_DELAY = 3 FPS_REPORT_DELAY = 3
@ -32,11 +30,9 @@ class Monitor:
fpsPrintTime = time.time() fpsPrintTime = time.time()
while True: while True:
newScreenshot = pyautogui.screenshot() newScreenshot = pyautogui.screenshot()
newScreenshot = cv.cvtColor(np.array(newScreenshot), cv.COLOR_RGB2BGR) newScreenshot = cv.cvtColor(np.array(newScreenshot), cv.IMREAD_GRAYSCALE)
cv.imwrite("in_memory_to_disk.png", newScreenshot)
self.screenshot = newScreenshot self.screenshot = newScreenshot
cv.imwrite("assets/screenshot.jpg", self.screenshot)
currTime = time.time() currTime = time.time()
if currTime - fpsPrintTime >= FPS_REPORT_DELAY: if currTime - fpsPrintTime >= FPS_REPORT_DELAY:
self.fps = 1 / (currTime - loopTime) self.fps = 1 / (currTime - loopTime)
@ -48,13 +44,11 @@ class Monitor:
self.screenThread.start() self.screenThread.start()
self.logger.log("Main Agent: Thread started") self.logger.log("Main Agent: Thread started")
def findMatch(self, img, method = "TM_CCOEFF_NORMED"): def findMatch(self, template, method = "TM_CCOEFF_NORMED"):
methodInt = getattr(cv, method) methodInt = getattr(cv, method)
screen = cv.imread("assets/screenshot.jpg", cv.IMREAD_GRAYSCALE)
herePath = os.path.dirname(os.path.realpath(__file__)) assert template is not None, "file could not be read, check with os.path.exists()"
template = cv.imread(os.path.join(herePath, "assets", "FishTest3.png"), cv.IMREAD_GRAYSCALE) res = cv.matchTemplate(screen, template, methodInt)
res = cv.matchTemplate(img, template, methodInt)
w, h = template.shape[::-1] w, h = template.shape[::-1]
min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res) min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res)
@ -64,21 +58,8 @@ class Monitor:
else: else:
top_left = max_loc top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h) bottom_right = (top_left[0] + w, top_left[1] + h)
cv.rectangle(img,top_left, bottom_right, 255, 2) return (top_left[0] + bottom_right[0]) / 2, (top_left[1] + bottom_right[1]) / 2
plt.subplot(121)
plt.imshow(res,cmap = 'gray')
plt.title('Matching Result')
plt.xticks([])
plt.yticks([])
plt.subplot(122)
plt.imshow(img,cmap = 'gray')
plt.title('Detected Point')
plt.xticks([])
plt.yticks([])
plt.suptitle(method)
plt.show()
def stopScreenCaptureThread(self): def stopScreenCaptureThread(self):
if (self.screenThread): if (self.screenThread):

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.1 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1.5 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 1010 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 7.7 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.1 MiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 4.0 MiB

BIN
tests/bobber.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.2 KiB

BIN
tests/screen.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 815 KiB

View File

@ -1,14 +1,11 @@
import os
import cv2 as cv import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt from matplotlib import pyplot as plt
# interpolate here_path to get the path to the fishing target image img = cv.imread('screen.jpg', cv.IMREAD_GRAYSCALE)
herePath = os.path.dirname(os.path.realpath(__file__))
img = cv.imread(os.path.join(herePath, "assets", "Bobber2.png"), cv.IMREAD_GRAYSCALE)
assert img is not None, "file could not be read, check with os.path.exists()" assert img is not None, "file could not be read, check with os.path.exists()"
img2 = img.copy() img2 = img.copy()
template = cv.imread(os.path.join(herePath, "assets", "FishTest3.png"), cv.IMREAD_GRAYSCALE) template = cv.imread('bobber.jpg', cv.IMREAD_GRAYSCALE)
assert template is not None, "file could not be read, check with os.path.exists()" assert template is not None, "file could not be read, check with os.path.exists()"
w, h = template.shape[::-1] w, h = template.shape[::-1]
@ -33,16 +30,10 @@ for meth in methods:
cv.rectangle(img,top_left, bottom_right, 255, 2) cv.rectangle(img,top_left, bottom_right, 255, 2)
plt.subplot(121) plt.subplot(121),plt.imshow(res,cmap = 'gray')
plt.imshow(res,cmap = 'gray') plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
plt.title('Matching Result') plt.subplot(122),plt.imshow(img,cmap = 'gray')
plt.xticks([]) plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
plt.yticks([])
plt.subplot(122)
plt.imshow(img,cmap = 'gray')
plt.title('Detected Point')
plt.xticks([])
plt.yticks([])
plt.suptitle(meth) plt.suptitle(meth)
plt.show() plt.show()