I am working with the code below to detect multiple images on the screen using multiple source images to detect different object on screen. found this link with solution but when applied my fps drops down drastically
import cv2 as cv
import numpy as np
import os
from time import time
import mss
os.chdir(os.path.dirname(os.path.abspath(__file__)))
imgs = ['car.png', 'bear.png', 'computer.png', 'log.png', 'utensils.png',
'bench.png', 'river.png', 'keepdistance.png', 'waste.png', 'player.png']
def findClickPostion(needle_img_path, haystack_img):
# haystack_img = cv.imread('/Users/amanrai/Desktop/conquer2020/main.png', cv.IMREAD_UNCHANGED)
needle_img = cv.imread(needle_img_path, cv.IMREAD_UNCHANGED)
result = cv.matchTemplate(haystack_img, needle_img, cv.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv.minMaxLoc(result)
threshold = 0.8
if max_val >= threshold:
needle_w = needle_img.shape[1]
needle_h = needle_img.shape[0]
top_left = max_loc
bottom_left = (top_left[0] + needle_w, top_left[1] + needle_h)
print('Found')
cv.rectangle(haystack_img, top_left, bottom_left, color=(
0, 255, 0), thickness=2, lineType=cv.LINE_4)
cv.imshow('result', haystack_img)
# cv.waitKey()
else:
print('Not Found')
loop_time = time()
with mss.mss() as sct:
monitor = {"top": 450, "left": 0, "width": 1438,
"height": 450} # "width": 1500, "height": 860
while (True):
screenshot = np.array(sct.grab(monitor))
#screenshot = cv.cvtColor(screenshot, cv.COLOR_RGB2BGR)
#cv.imshow('Computer Vision', screenshot)
for ik in imgs:
findClickPostion(ik, screenshot)
print('FPS {}'.format(1 / (time() - loop_time)))
loop_time = time()
if cv.waitKey(1) == ord('q'):
cv.destroyAllWindows()
break
print('done')
Is there a faster way to achieve it as speed up the process so that it detects multiple objects on screen without losing any fps.