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:
time.sleep(1)
self.logger.log("###")
self.monitor.findMatch(self.fishingBobberImg)
point = self.monitor.findMatch(self.fishingBobberImg)
pyautogui.moveTo(point[0], point[1])
def run(self):
if self.monitor.screenshot is None:

View File

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

View File

@ -1,11 +1,9 @@
import time
import os
from threading import Thread
from screeninfo import get_monitors
import pyautogui
import numpy as np
import cv2 as cv
from matplotlib import pyplot as plt
from core.Logger import Logger
FPS_REPORT_DELAY = 3
@ -32,11 +30,9 @@ class Monitor:
fpsPrintTime = time.time()
while True:
newScreenshot = pyautogui.screenshot()
newScreenshot = cv.cvtColor(np.array(newScreenshot), cv.COLOR_RGB2BGR)
cv.imwrite("in_memory_to_disk.png", newScreenshot)
newScreenshot = cv.cvtColor(np.array(newScreenshot), cv.IMREAD_GRAYSCALE)
self.screenshot = newScreenshot
cv.imwrite("assets/screenshot.jpg", self.screenshot)
currTime = time.time()
if currTime - fpsPrintTime >= FPS_REPORT_DELAY:
self.fps = 1 / (currTime - loopTime)
@ -48,13 +44,11 @@ class Monitor:
self.screenThread.start()
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)
herePath = os.path.dirname(os.path.realpath(__file__))
template = cv.imread(os.path.join(herePath, "assets", "FishTest3.png"), cv.IMREAD_GRAYSCALE)
res = cv.matchTemplate(img, template, methodInt)
screen = cv.imread("assets/screenshot.jpg", cv.IMREAD_GRAYSCALE)
assert template is not None, "file could not be read, check with os.path.exists()"
res = cv.matchTemplate(screen, template, methodInt)
w, h = template.shape[::-1]
min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res)
@ -64,21 +58,8 @@ class Monitor:
else:
top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
cv.rectangle(img,top_left, bottom_right, 255, 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()
return (top_left[0] + bottom_right[0]) / 2, (top_left[1] + bottom_right[1]) / 2
def stopScreenCaptureThread(self):
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 numpy as np
from matplotlib import pyplot as plt
# interpolate here_path to get the path to the fishing target image
herePath = os.path.dirname(os.path.realpath(__file__))
img = cv.imread(os.path.join(herePath, "assets", "Bobber2.png"), cv.IMREAD_GRAYSCALE)
img = cv.imread('screen.jpg', cv.IMREAD_GRAYSCALE)
assert img is not None, "file could not be read, check with os.path.exists()"
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()"
w, h = template.shape[::-1]
@ -33,16 +30,10 @@ for meth in methods:
cv.rectangle(img,top_left, bottom_right, 255, 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.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(meth)
plt.show()