i want to make this script to detect if an image on a region change image it press key
if pyautogui.locateOnScreen(image=r'C:\Users\ether\Desktop\New folder (4)\saveimage.png')(region=(800,460,90,90), grayscale=True, confidence=0.5):
elif pyautogui.locateOnScreen('yellow.png', confidence=0.8) !=None:
keyboard.press_and_release('d')
elif pyautogui.locateOnScreen('blue.png', confidence=0.8) !=None:
keyboard.press_and_release('a')
Related
I have a python program using the cv2 library, that chooses a random US state, pulls the image of the state from a folder and displays it. It was working fine, so I saved it, and suddenly, instead of showing the image, it shows a blank grey square and crashes. Why is it doing this, and how can I fix it? Heres the sample images:
and heres the code:
import cv2
import random
import time
#makes a list of the states
def states():
states = ['alabama','alaska','arizona','arkansas','california',
'colorado','connecticut','deleware','florida','georgia','hawaii',
'idaho','illinois','indiana','iowa','kansas','kentucky','louisiana',
'maine','maryland','massachussets','michigan','minnesota',
'mississipi','missouri','montana','nebraska','nevada',
'new_hampshire','new_jersey','new_mexico','new_york',
'north_carolina','north_dakota','ohio','oklahoma','oregon',
'pennsylvania','rhode_island','south_carolina','south_dakota',
'tennessee','texas','utah','vermont','virginia','washington',
'west_virginia','wisconsin','wyoming']
while True:
#choose a random state from the states list
state = random.choice(states)
#take a picture from the states folder, and display it
img = cv2.imread('picture_files/states/' + state + '.png')
cv2.imshow('guess the state!', img)
#checks if you typed the right state,
#and gives an appropriate response
guess = input("guess the state! (type stop to stop!)\n")
if guess.lower() == state:
print("Correct!")
time.sleep(2)
print("Lets do it again!")
elif guess.lower() == "stop":
break
else:
print("Nope! It was " + state + ". Keep trying!")
time.sleep(2)
if __name__ == '__main__':
states()
Basically you are missing some cv2.waitKey() function to show the image (see also here).
This is a possible example for a solution.
def pick_a_state():
states = ['a','b'] # replace with your list
return random.choice(states)
def show_state(state):
img = cv2.imread(state + '.png', cv2.IMREAD_UNCHANGED)
cv2.imshow('guess the state!', img)
cv2.waitKey(1000)
def get_the_answer(state):
guess = raw_input("guess the state! (type stop to stop!)\n")
if guess.lower() == state:
print(state)
print("Correct!")
time.sleep(2)
print("Lets do it again!")
return 1
elif guess.lower() == "stop":
return 0
else:
print("Nope! It was " + state + ". Keep trying!")
time.sleep(2)
return 1
if __name__ == '__main__':
while True:
state = pick_a_state()
show_state(state)
if get_the_answer(state) == 0:
cv2.destroyAllWindows()
break
Please analyze the code to understand how it works. Hope can help.
I've had an error like this before where opencv would not open and have been successful in a few ways to solve it.
Add cv2.waitKey(0) at the end of your function and then add
cv2.destroyAllWindows() underneath your calling of states()
like this:
if __name__ == '__main__':
states()
cv2.destroyAllWindows()
Install opencv-contrib-python.
Try installing it with the command pip3 install opencv-contrib-python==4.4.0.46.
I wanted to capture an image directly from my camera by keyboard input. For example, when I press 'r', the camera takes a picture of what it sees. Here's my code:
import cv2 as cv
import time
cap = cv.VideoCapture(0)
while True:
_, img = cap.read()
cv.imshow('original', img)
if cv.waitKey(1) == ord('q'):
break
elif cv.waitKey(1) == ord('r'):
cv.imwrite('data_{}.jpg'.format(time.time()), img)
print('saving an image')
continue
else:
continue
cap.release()
cv.destroyAllWindows()
This code is actually working but not that good. Sometimes, when I press 'r' it not saving any image into my directory. How could I improve this code so whenever I press 'r' it's certainly saving an image?
You're supposed to capture a key and then check its value.
while True:
key = cv.waitKey(1)
if key == ord('q'):
break
elif key == ord('r'):
# do your stuff here
pass
The way you've done it transverses the conditional blocks with a new key press at a time. Let's say you first press 'r'. The loop is currently at if cv.waitKey(1) == ord('q'), which evaluates false, so it continues to the next check. By this time you're not pressing anything, so cv.waitKey(1) will return -1 and your check for 'r' will be false. And on and on you go.
Hello my goal is to be able to make my bot click the thing of my choosing on any screen size because I think that is the main issue. Ive tried to decrease the confidence level but it just ends up clicking something else with the same general color. ive tested it with an EXACT image and it clicks the correct spot so its not like the coordinates are off or anything its just the image recognition.
These are the images to go by
(X1, NextLesson, Arrow)
from pyautogui import *
import pyautogui
import time
import keyboard
import random
def NextLesson():
keepGoing = True
while keepGoing == True:
counter = 0
nl_coordinates = pyautogui.locateOnScreen('images/nextLesson.png', confidence=0.4)
print(nl_coordinates) # This will print out where it is
if nl_coordinates:
print(f"I can see it at {nl_coordinates}")
pyautogui.click(nl_coordinates)
keepGoing = False
else:
print("I cannot see it.")
def Arrow():
keepGoing = True
while keepGoing == True:
counter = 0
arrow_coordinates = pyautogui.locateOnScreen('images/arrow.png', confidence=0.4)
print(arrow_coordinates) # This will print out where it is
if arrow_coordinates:
print(f"I can see it at {arrow_coordinates}")
pyautogui.click(arrow_coordinates)
keepGoing = False
else:
print("I cannot see it.")
def X1():
keepGoing = True
while keepGoing == True:
counter = 0
x1_coordinates = pyautogui.locateOnScreen('images/x1.png', confidence=0.4)
print(x1_coordinates) # This will print out where it is
if x1_coordinates:
print(f"I can see it at {x1_coordinates}")
pyautogui.click(x1_coordinates)
keepGoing = False
else:
print("I cannot see it.")
while True:
counter = 0
counter2 = 0
true = True
time.sleep(2)
X1()#
time.sleep(8)
NextLesson()#
time.sleep(10)
Arrow()#
print("calibration complete ")
time.sleep(5)
cords = pyautogui.position()
while counter != 1800:
time.sleep(60)
pyautogui.click(cords) #clicking where ouse is at
print("clicked")
counter += 60
print(counter)
if counter == 1800:
time.sleep(5) #stops code for 5 secs
X1() #clicks mouse to x button
print("clicked x")
time.sleep(5) #stops code for 5 secs
NextLesson() #clicks mouse to the assignment button
print("clicked assignemnt")
time.sleep(15) #stops code for 2 secs
Arrow() #clicks mouse to the second assignment button
print("clicked 2nd assignment button ")
time.sleep(5) #waits 5secs to put cursor at position
cords = pyautogui.position() #grabs position
print("grabbed position")
We can use opencv-python to perform multiscale template matching. The idea is to scale the template image and attempt to locate the resized template in the screenshot. The code below (adapted from here) loops over 50 scaling parameters in the range [0.25,2] and selects the one the that provides the best match. You might want to decrease the range or number of scaling parameters for efficiency. It is also better to save the correct scaling parameter and reuse it for multiple images.
import cv2
import pyscreeze
import numpy as np
import imutils
import pyautogui
def template_match_with_scaling(image,gs=True,confidence=0.8):
"""
Locate an image and return a pyscreeze box surrounding it.
Template matching is done by default in grayscale (gs=True)
Detect image if normalized correlation coefficient is > confidence (0.8 is default)
"""
templateim = pyscreeze._load_cv2(image,grayscale=gs) # template image
(tH, tW) = templateim.shape[:2]
screenim_color = pyautogui.screenshot() # screenshot of image
screenim_color = cv2.cvtColor(np.array(screenim_color),cv2.COLOR_RGB2BGR)
if gs is True:
screenim = cv2.cvtColor(np.array(screenim_color),cv2.COLOR_BGR2GRAY)
else:
screenim = screenim_color
#try different scaling parameters and see which one matches best
found = None #bookeeping variable for the maximum correlation coefficient, position and scale
scalingrange = np.linspace(0.25,2,num=50)
for scale in scalingrange:
resizedtemplate = imutils.resize(templateim, width = int(templateim.shape[1]*scale) ) # resizing with imutils maintains the aspect ratio
r = float(resizedtemplate.shape[1])/templateim.shape[1] # recompute scaling factor
result = cv2.matchTemplate(screenim, resizedtemplate, cv2.TM_CCOEFF_NORMED) # template matching using the correlation coefficient
(_, maxVal, _, maxLoc) = cv2.minMaxLoc(result) #returns a 4-tuple which includes the minimum correlation value, the maximum correlation value, the (x, y)-coordinate of the minimum value, and the (x, y)-coordinate of the maximum value
if found is None or maxVal > found[0]:
found = (maxVal, maxLoc, r)
(maxVal, maxLoc, r) = found
if maxVal > confidence:
box = pyscreeze.Box(int(maxLoc[0]), int(maxLoc[1]), int(tW*r), int(tH*r) )
return box
else:
return None
def locate_center_with_scaling(image,gs=True):
loc = template_match_with_scaling(image,gs=gs)
if loc:
return pyautogui.center(loc)
else:
raise Exception("Image not found")
#sample usage
coords = locate_center_with_scaling('images/arrow.png')
The answer I found is taking a more accurate photo of what I need like trying to remove the background as much greyscale=ture and adding more confidence
I'm trying to build a macro for a game I play. I want to use pyautogui to find an image with confidence=(0.2) and then move the mouse to its position and click. I'm not sure what to do from here. It will find the image and print but when I try to add pyautogui.click('forgedspirit.png') I get an error: cannot unpack non-iterable NoneType object.
def new_method():
if pyautogui.locateOnScreen('forgespirit.png', confidence=(0.2)) != None:
print("I can see it.")
pyautogui.click('forgespirit.png')
time.sleep(0.5)
else:
print("image not found")
time.sleep(0.5)
new_method()
instead of telling pyautogui to click the image, you should tell it to click the coordinates of the image.
Try this:
try:
pos = pyautogui.locateOnScreen('forgespirit.png', confidence=(0.2))
if pos != None:
print("I can see it.")
pyautogui.click(pos[0], pos[1])
time.sleep(0.5)
else:
print("image not found")
time.sleep(0.5)
except:
print("image not found")
I keep getting an invalid syntax error with this. So, how can I resolve this and where can I find related documentation in the future.
import cv2
import numpy as np
drawing = False # true if mouse is pressed
mode = True # if True, draw rectangle. Press 'm' to toggle to curve
ix,iy = -1,-1
class DessinerLigne:
def dessinerLigne(self):
# Create a black image
self.img=np.zeros((512,512,3),np.uint8)
def draw_circle(event,x,y,flags,param):
global ix,iy,drawing,mode
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
ix,iy = x,y
elif event == cv2.EVENT_MOUSEMOVE:
if drawing == True:
if mode == True:
cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
else:
cv2.circle(img,(x,y),5,(0,0,255),-1)
elif event == cv2.EVENT_LBUTTONUP:
drawing = False
if mode == True:
cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
else:
cv2.circle(img,(x,y),5,(0,0,255),-
img = np.zeros((512,512,3), np.uint8)
cv2.imshow("Image", self.img)
# If q is pressed then exit program
self.k=cv2.waitKey(0)
if self.k==ord('q'):
cv2.destroyAllWindows()
if __name__=="__main__":
DL=DessinerLigne()
DL.dessinerLigne()
There clearly are multiple issues with this script. Ones that need immediate attention are:
There's an indentation error in the definition of dessinerLigne class.
Change:
class DessinerLigne:
def dessinerLigne(self):
# Create a black image
self.img=np.zeros((512,512,3),np.uint8)
to:
class DessinerLigne:
def dessinerLigne(self):
# Create a black image
self.img=np.zeros((512,512,3),np.uint8)
and the indentation error should be fixed.
There's an incomplete line of code in line 32.
Is line 33 a part of the method draw_circle()? If so, it has be properly indented. Add 4 whitespaces in front of it.
You seem to have pasted the code from somewhere. During this process, it is very likely that some invisible control characters that might break the syntax may have arrived. Use an editor that has 'show invisible' features to resolve this issue.