Wowxel/core/Monitor.py
2024-09-03 00:23:14 -04:00

88 lines
2.6 KiB
Python

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
class Monitor:
def __init__(self):
self.logger = Logger("Monitor")
self.monitor = None
self.screenThread = None
self.screenshot = None
self.fps = None
self.getMonitor()
def getMonitor(self):
for monitor in get_monitors():
if (monitor.is_primary):
self.monitor = monitor
def updateScreen(self):
self.logger.log("Monitor: Starting computer vision screen update...")
self.logger.log("Monitor: Detected display resolution: " + str(self.monitor.width) + " x " + str(self.monitor.height))
loopTime = time.time()
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)
self.screenshot = newScreenshot
currTime = time.time()
if currTime - fpsPrintTime >= FPS_REPORT_DELAY:
self.fps = 1 / (currTime - loopTime)
fpsPrintTime = currTime
loopTime = currTime
def startScreenCaptureThread(self):
self.screenThread = Thread(target=self.updateScreen, name="Update screen thread", daemon=True)
self.screenThread.start()
self.logger.log("Main Agent: Thread started")
def findMatch(self, img, 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)
w, h = template.shape[::-1]
min_val, max_val, min_loc, max_loc = cv.minMaxLoc(res)
# If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
if method in [cv.TM_SQDIFF, cv.TM_SQDIFF_NORMED]:
top_left = min_loc
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()
def stopScreenCaptureThread(self):
if (self.screenThread):
self.screenThread.terminate()
self.logger.log("Main Agent: Thread terminated")