1

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.

PabloS
  • 27
  • 5

1 Answers1

0

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!

  • Hello, Thank you for your answer, it works almost as it should, the concern is that time is running and if I am for example at 5 seconds and I detect a face, it only takes 2 seconds to display the "SCREAMER BOO" and not 7 . There is also a point that I do not quite understand, the part: face_apparition_time = time.time () - timer Since "timer = time.time()" (above), why doesn't that add up to 0? It's certainly a silly question but I'd rather ask than block on it Since it's surely in this part that I don't understand that the solution must be found, I have trouble unblocking it. – PabloS Apr 26 '21 at 13:42
  • Actually (you can try yourself), `time.time()` returns the actual number of seconds counted from a fixed moment (like, from january 1, 1900 by example) to now. So it will change every second. So you would get, at a time `t`, let's say 125.003 seconds. And if you call it again at `t + 3` seconds, you would get 125.006 seconds. Then you have to make the dfference between both to know that there has been 3 seconds between `t` and `t + 3`. – SpamBièreSki Apr 27 '21 at 09:47