-1

How can I call a function, for example Play a sound, if I do 3 blinks in a space of 2 seconds?

if blinking_ratio > 5.7:
    cv2.putText(frame, "BLINKING", (50, 150), font, 7, (255, 0, 0))
    winsound.PlaySound("campainha.wav.wav", winsound.SND_FILENAME)

Here if I blink, the sound plays... I want to play, only if I blink 3 times in 2 secs for example...

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • You could make a list of timestamps for when "blink" was called. Then each time it is called you check if the previous 2 calls were not longer than 2 seconds ago. – mkrieger1 May 28 '20 at 17:50
  • See for example https://stackoverflow.com/questions/415511/how-to-get-the-current-time-in-python and https://stackoverflow.com/questions/646644/how-to-get-last-items-of-a-list-in-python. – mkrieger1 May 28 '20 at 17:52

1 Answers1

0

You could set up a Time class by using

class Time(datetime.tzinfo):
  def utcoffset(self, x):
    return datetime.timedelta(hours=-5) + self.dst(x)

  def dst(self, x):
    date = datetime.datetime(x.year, 3, 8)
    self.dston = date + datetime.timedelta(days=6-date.weekday())
    date = datetime.datetime(x.year, 11, 1)
    self.dstoff = date + datetime.timedelta(days=6-date.weekday())
    if self.dston <= x.replace(tzinfo=None) < self.dstoff:
      return datetime.timedelta(hours=1)
    else:
      return datetime.timedelta(0)

  def tzname(self, x):
    return 'Time'

def timeStamp():
  return datetime.datetime.now(tz=Time()).strftime('%Y-%m-%d %H:%M:%S')

From this, you could do something like

while . . .:
  start = timeStamp()
  blinkOnce()
  end = timeStamp()
  if start - end < 2:
    pass
  else:
    break

Of course you would adjust the second code segment to whatever conditions you want to be met, but the end is just some pseudocode that you could edit accordingly.

Johnny
  • 211
  • 3
  • 15