简体   繁体   中英

Print the string only, when it is 10x the same

I have made a face recognition camera with Jetson Nano. I have also made the Python function which returns me the recognized person name as a string every ~1s.

This is the simple test code:

from face import scanned_name

while True:
    print(scanned_name())

OUTPUT:

None
None
None
None
None
None
None
None
None
None
None
None
Pavol Bujna
Pavol Bujna
Pavol Bujna
Pavol Bujna
Pavol Bujna
Pavol Bujna
Pavol Bujna
Pavol Bujna
Pavol Bujna
Unknown
None
None
None
None
None
None
None
None
Lukas Smith
Pavol Bujna
None
None
None
None
None
Benjamin Moose
Charlie Grey
Pavol Bujna
Pavol Bujna
Pavol Bujna
Charlie Grey
None
None
None
None
None
None
None
None
None
None

As you can see, it's working quite fine when looking directly at the camera (9x Pavol Bujna). When coming or leaving the camera, it's printing sometimes random names.

That's why I'm trying to filter that out, only when the same name will be detected let's say 10x, it will print it. So in this case, will print my name (Pavol Bujna) just once.

I'm trying to do something like this:

if scanned_name() == "10x the same string":
    print(scanned_name())
elif scanned_name() == None:
    print("Camera ready")
else:
    print("Recognizing face")

I don't know how to do that "10x the same string" part. I have tried to append it each time to the list, and then compare if every item in a list is the same, but that wasn't working well.

Use a deque of length 10 and check if there is only one name in the queue, which is not 'None' . This will only print the found name once (the first time it is encountered 10 times in a row):

from collections import deque

# names = list-of-your-names

queue = deque(maxlen=10)

found = False
for name in names:
    queue.append(name)
    if len(set(queue)) == 1 and name != 'None':
        if not found:
            found = True
            print(name)
    else:
        found = False

names can also be something which is iterable and generates the nameson the fly (eg a generator), it doesn't have to be a list, like for example:

def gen_name():
    import time
    while True:
        name = guess_name_from_image()
        yield name
        time.sleep(1)

This code will print name that was scanned 10 times in a row

n_consec_scans_to_print = 10
consec_scanned_names_count = 0
last_scanned_name = None
while True:
    current_scanned_name = scanned_name()
    if current_scanned_name == last_scanned_name:
        consec_scanned_names_count += 1
        if consec_scanned_names_count == n_consec_scans_to_print:
            print(current_scanned_name)
            last_scanned_name = None
            consec_scanned_names_count = 0
    else:
        last_scanned_name = current_scanned_name
        consec_scanned_names_count = 0

EDIT
I don't know how scanned_name() works. So I advice you to consider handling the case when this function returns object that is incomparable with string

Modify your code as follow:

from face import scanned_name

same_string = []
while True:
    if not scanned_name() == None: 
        same_string += scanned_name()
        #"10x the same string" part.
        if same_string.count(scanned_name()) == 10:
            print(scanned_name())
        else:
            print("Recognizing face")
    else:
        print("Camera ready")
    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM