Search code examples
pythonif-statementtimetrackingseconds

Python: if X is detected more than T seconds:


I am looking for Knowledge.

I'm new to python and need to do a project with a camera Gravity: HuskyLens. It allows you to display blocks when it detects faces (around the face, it's tracking in summary) And I would like, when I detect a Block, to know if it is detected for more than 7 seconds.

import time
import json
from huskylib import HuskyLensLibrary

# Initialize
#hl = HuskyLensLibrary("I2C", "", address = 0x32)
hl = HuskyLensLibrary("SERIAL", "/dev/ttyUSB0", 3000000)

# Change to face recognition algorithms
hl.algorithms("ALGORITHM_FACE_RECOGNITION")

timer = time.time()

while True:
    blocks = hl.requestAll()

    for block in blocks:
        if block.type == "BLOCK": # If a block is detected
            print("Face !")
            if BLOCK DETECTED MORE THAN 7 SECONDS: # If a block is detected more than 7 seconds
                print("SCREAMER ! BOO !")
                time.sleep(0.5)
        else:
            print("No Face !")
            time.sleep(0.5)

I do not know if it is sufficiently clear, I am interested in any information allowing me to progress

ps: I have already gone for a walk on the Time library but I have not managed to understand everything and therefore find my happiness.


Solution

  • I may be wrong, but you could try to track what the latest block was, and then as long as the type of the latest block is "BLOCK", you can increase the timer.

    What I mean:

    face_apparition_time = 0  # We haven't seen a face yet, so apparition time is 0
    # Gives a time in seconds since a fixed point we don't control, we'll use it as a checkpoint
    timer = time.time()
    last_block_seen = None  # Begin with None, we haven't seen any block yet
    
    while True:
        blocks = hl.requestAll()
    
        for block in blocks:
            if block.type == "BLOCK":  # If a block is detected
                print("Face !")
    
                # Then we check if the last block was also a "BLOCK". If yes, we increase our timer.
                if last_block_seen == "BLOCK":
                    # We count the number of seconds between the last checkpoint and now.
                    face_apparition_time = time.time() - timer
    
                # Then we chack if a face has appeared for more than 7 seconds:
                if face_apparition_time > 7:  # If a block is detected more than 7 seconds.
                    print("SCREAMER ! BOO !")
    
            else:
                print("No Face !")
    
                # As we don't see no face, we have to reset our checkpoint to "now"
                timer = time.time()
                face_apparition_time = 0
    
            # Do not forget that we are going to look at the next block, so this block must be stored :)
            last_block_seen = block.type
            time.sleep(0.5)
    

    I'm not sure of where the time.sleep() should go, you can try yourself what suits better :) Hope this helps, do not hesitate for further questions!