I am trying to make a script that can detect if an eye is open or close. I am doing this by using the code below.
https://github.com/balajisrinivas/Color-Detection-OpenCV
Thank you to balajisrinivas for providing the code!
Using this code I can add live video capture and create a single image that is refreshed every 2 seconds in a while loop instead of a single image(colorpic.jpg being used as an example). Also, I made it so that if the colour is brown or has an RGB near brown the computer beeps but if the colour is white, black, or the colour of your iris then it knows your eyes aren’t closed. There is one problem. The problem is that I have to click on the image to get the results but I want it to happen automatically. So, I used if pyautogui.locateOnScreen(‘needle.jpg’, confidence=0.8)
instead of if clicked
in the while True:
loop. The picture ‘needle..jpg’ is a picture of my eye. After that I added pyautogui.click(‘needle.jpg’)
which would click the center of the picture and give me the result of an open or closed eye without a manual click.
Where I am getting stuck:
while True:
cv2.imshow("image", img)
if pyautogui.locateOnScreen('needle.jpg', confidence=0.8):
pyautogui.click('needle.jpg')
# cv2.rectangle(image, start point, endpoint, color, thickness)-1 fills entire rectangle
cv2.rectangle(img, (20, 20), (750, 60), (b, g, r), -1)
# Creating text string to display( Color name and RGB values )
text = get_color_name(r, g, b) + ' R=' + str(r) + \
' G=' + str(g) + ' B=' + str(b)
Before the code was:
while True:
cv2.imshow("image", img)
if clicked:
# cv2.rectangle(image, start point, endpoint, color, thickness)-1 fills entire rectangle
cv2.rectangle(img, (20, 20), (750, 60), (b, g, r), -1)
# Creating text string to display( Color name and RGB values )
text = get_color_name(r, g, b) + ' R=' + str(r) + \
' G=' + str(g) + ' B=' + str(b)
The only problem is that I am getting a Type Error.
This is the error:
Traceback (most recent call last):
File "C:\Users\user1\Desktop\Color-Detection-OpenCV\hello.py", line 89, in <module>
pyautogui.click('needle.jpg')
line 598, in wrapper
returnVal = wrappedFunction(*args, **kwargs)
File "C:\Users\user1\AppData\Local\Programs\Python\Python39\lib\site-packages\pyautogui\__init__.py", line 980, in click
x, y = _normalizeXYArgs(x, y)
TypeError: cannot unpack non-iterable NoneType object
Just to let you know I am a newbie in python and I am a school student. If you know the answer to why I am getting this error please help me. Thank you!
Looks like it's just intended oversight from pyautogui. Because we already know where image is on your locateOnScreen
call, so why we search again when clicking.
From their source code:
def _normalizeXYArgs(firstArg, secondArg):
# skip
elif isinstance(firstArg, str):
# If x is a string, we assume it's an image filename to locate on the screen:
try:
location = locateOnScreen(firstArg)
# The following code only runs if pyscreeze.USE_IMAGE_NOT_FOUND_EXCEPTION is not set to True, meaning that
# locateOnScreen() returns None if the image can't be found.
if location is not None:
return center(location)
else:
return None
# skipping lines past this
At last we see comment saying that locateOnScreen
return None if image is not found.
For your call on locateOnScreen
, you passed confidence=0.8
but default confidence is 0.999.
So with confidence 0.8 image is found, but not with 0.999. Therefore it returned None
to location
and then it lead if block to return None
- which is not unpackable.
Since I am currently on mobile I can't test myself, but I bet locateOnScreen
returns x, y coord when matched.
so try change if block into something like this.
try:
x, y = pyautogui.locateOnScreen( ~~~ )
except TypeError:
# value unpack failed, so we got None. Therefore no image matched.
# therefore no x exist.
# Put things you want to do when there's NO image matched.
pass
else:
# Put things you want to do when image matched.
pyautogui.click(x, y)
I strongly suggest reading official document of Error handling part.