Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

res = cv2.matchTemplate #12

Closed
Shikamaru5 opened this issue Oct 27, 2021 · 0 comments
Closed

res = cv2.matchTemplate #12

Shikamaru5 opened this issue Oct 27, 2021 · 0 comments

Comments

@Shikamaru5
Copy link

hey there, I'm having difficulty figuring out how to get my template to match the game recording for my code. This is the test code that I'm trying to run, and it just doesn't seem to work. I do realize I haven't run the exact code that you put on your videos, but that is because I can't seem to get it to window/video capture for the game I'm playing, (which is a little frustrating).
Nevertheless, it works for still image and it works fine videocapturing when I don't try to matchTemplate but the two don't seem to want to merge.

import numpy as np
from PIL import ImageGrab
import cv2
import time
import threading

def screen_record(): 
     last_time = time.time()
     while(True):
         # 800x600 windowed mode for GTA 5, at the top left position of your main screen.
         # 40 px accounts for title bar. 
         printscreen = np.array(ImageGrab.grab(bbox=(0,40,800,640)))
         print('loop took {} seconds'.format(time.time()-last_time))
         last_time = time.time()
         img_gray = cv2.imshow('window',cv2.cvtColor(printscreen, cv2.COLOR_BGR2GRAY))
         if cv2.waitKey(25) & 0xFF == ord('q'):
             cv2.destroyAllWindows()
             break

    eyes_hungry = cv2.imshow('hungry2.jpg',0)
    last_time = time.time()
    res = cv2.matchTemplate(img_gray, eyes_hungry, cv2.TM_CCOEFF_NORMED)       
    threshhold = 0.8
    loc = np.where(res >= threshhold)
    for pt in zip(*loc[::-1]):
        w, h = template.shape[::-1]
        cv2.rectangle(img_gray, pt, (pt[0]+w, pt[1]+h), (0,255,255), 1)


screen_record()

this is the error or a variation on the same error I get when I try running it:

  F:\Desktop\Ne\neat-python>grabscreen.py                                                    
loop took 0.0657804012298584 seconds                                                       
Traceback (most recent call last):                                                         
  File "F:\Desktop\Ne\neat-python\grabscreen.py", line 29, in <module>                     
    screen_record()                                                                        
  File "F:\Desktop\Ne\neat-python\grabscreen.py", line 22, in screen_record                
    res = cv2.matchTemplate(img_gray, eyes_hungry, cv2.TM_CCOEFF_NORMED)                   
cv2.error: OpenCV(4.5.3) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-sn_xpupm\opencv\modules\imgproc\src\templmatch.cpp:588: error: (-215:Assertion failed) corr.rows <= img.rows + templ.rows - 1 && corr.cols <= img.cols + templ.cols - 1 in function 'cv::crossCorr'

if you or anyone might be able to help, I'd really appreciate it, I've been banging my head on the wall for a few days now.

Thank you for reading.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant