I would like to change the color of my rectangle if i clicked on this selfmade button. Unfortunately, I have managed to change the colour when my index finger points to this button. When I remove my index finger from the button, it returns to its old colour.
How can I implement it so, that when I move my index finger to this button and hold it for a certain second (for exl. 4 seconds), the purple colour of the button should change to green permanently. If I hold it again, my green button should change to purple permanently.
cv2.rectangle(image, (100, 300), (200, 200), (255, 0, 255), cv2.FILLED)
thumb = res.right_hand_landmarks[8]
if ((thumb.x < 0.40 and thumb.x >= 0.30) and
(thumb.y < 0.69 and thumb.y >= 0.64)):
cv2.rectangle(image, (100, 300), (200, 200), (0, 255, 0), cv2.FILLED)
CodePudding user response:
Here, instead of just only depending on the position of the index finger, you can add another condition in the if
statement, depending whether the variable (example, ift
is either True
or False
. This might be confusing, but let me explain it with the example:
ift = False
# Keep the var 'False' if the index finger was not in the self-made button for 4 seconds or more, else 'True'
Now, for calculating the time, you can use built-in time
module. Here's an example:
from time import time, sleep
val = time()
sleep(1)
print(int(time() - val)) # Converting to integer, as the default is set to 'float'
# OUTPUT
# 1
Here's the documentation to the time
module where you can find time.time()
:
time Documentation
Now, I know for cv2
, you can't practically use sleep
as it will stop the whole iteration, so here's another work around, for example:
from time import time
var = False
x = time()
y = time()
while True:
if not var:
x = time()
# If the 'var' is False, it will refresh the 'time()' value in every iteration, but not if the value was True.
if int(time() - y) >= 4:
print(int(time() - x), int(time() - y))
break
# OUTPUT
# 0 4
Now, we know how we can calculate time passed using the logic above, let's implement it in your code:
from time import time
# Variables below outside the loop, so that it doesn't reset at every iteration
ift = False # Let's assume 'False' is for 'purple' and 'True' is for 'green'
inside = False # It's to check whether the finger is in or not
t = time() # If the below code (you provided) is in a function, then I prefer adding it in the function before the loop starts
# Everything under is assumed to be inside the loop (assuming you are using the loop)
thumb = res.right_hand_landmarks[8]
if ((thumb.x < 0.40 and thumb.x >= 0.30) and
(thumb.y < 0.69 and thumb.y >= 0.64)):
cv2.rectangle(image, (100, 300), (200, 200), (255, 0, 255), cv2.FILLED)
inside = True
else:
cv2.rectangle(image, (100, 300), (200, 200), (0, 255, 0), cv2.FILLED)
inside = False
t = time() # Refreshes the time, because the finger is no more inside
if inside and int(time() - t) >= 4:
ift = not ift # Changes the value of 'ift' to its opposite, i.e., if the value was False, it will become True and vice versa
if ift:
cv2.rectangle(image, (100, 300), (200, 200), (0, 255, 0), cv2.FILLED)
else:
cv2.rectangle(image, (100, 300), (200, 200), (255, 0, 255), cv2.FILLED)
The code mentioned above might or might not work as it is not tested. But, it is enough to provide you with the information on what logic you can use to solve your issue.
NOTE, that the code above can be reduced as it is expanded and there might be useless lines that can be reduced, but for the sake of explanation, it has been expanded.