Tuesday, 8 February 2022

Displaying 3 timers at once through Python's OpenCV

Background

I have a video with people walking around different homes. I now want to implement 3 different timers (for 3 different people) that run concurrently as soon as the video starts. It should pause when the people are outside of the bounding boxes (bboxes) of the house but resume when they are back within it.

enter image description here

The image above is an example of what the first frame of the video looks like. As you can see, person 0 is not within the bbox of a house and so the timer for person 0 must not begin until he/she enters one of these house bboxes. Person 1 and 2's individual timers must begin as soon as the video begins.

What I have done so far

I read about the concept of Threads in the documentation for the purpose of running multiple tasks at once so I decided to use them to create my 3 different timers. This is my first time using them so I'm not sure if I'm using them currently.

# boxes_houses = [(#, #, #, #), (#, #, #, #), ...]

while cap.isOpened():

    # [...]

    def stopwatch_timer():
        global start, end
        start = time.thread_time()
        x = 0
        while x < 58: # 58s is the duration of the video
            pass
            x += 1
        end = time.thread_time()

    # Check if any corner of the persons bbox is inside the house bbox
    isinside_checks = []
    for house in houses:
        isinside_checks.append(isInside(persons, house))

    if any(isinside_checks):
        print("Person", cnt_person, ": True\n")
        if cnt_person==0:
            t0 = threading.Thread(target=stopwatch_timer, args=())
            t0.start()
            t0.join()
            print("The time spent by person 0 is: {}".format(end - start))

        elif cnt_person==1:
            t1 = threading.Thread(target=stopwatch_timer, args=())
            t1.start()
            t1.join()
            print("The time spent by person 1 is: {}".format(end - start))

        elif cnt_person==2:
            t2 = threading.Thread(target=stopwatch_timer, args=())
            t2.start()
            t2.join()
            print("The time spent by person 2 is: {}".format(end - start))

    else:
        print("Person", cnt_person, ": False\n")
        time.sleep(1)

    cnt_person+= 1
    cnt_house+= 1


# show frame
cv2.imshow('MultiTracker', frame)

# quit on ESC button
if cv2.waitKey(1) & 0xFF == 27:  # Esc pressed
    break

As the video is playing, I would expect the time spent by person # to change, however, the time only outputs 0.0 as shown below. I'm not sure where I'm going wrong...

Person 0 : False
Person 1 : True
The time spent by person 1 is: 0.0
Person 2 : True
The time spent by person 2 is: 0.0
Person 0 : False
Person 1 : True
The time spent by person 1 is: 0.0
Person 2 : True
The time spent by person 2 is: 0.0
Person 0 : False
Person 1 : True
The time spent by person 1 is: 0.0
Person 2 : True
[...]


from Displaying 3 timers at once through Python's OpenCV

No comments:

Post a Comment