I am making a brushing app where I want to track how long a person brushes their teeth for. For that, I have built a toothbrush tracker, a mouth tracker, and also developed an algo to determine whether the toothbrush is inside the mouth (which I assume means the person is brushing).
Now, I just need to add a timer that can count real-time how long has the person been brushing for, and display it on the video feed. If the user pauses brushing, the timer should pause too; if the user resumes brushing, the timer should resume too. How do I make this?
My logic to detect when brushing is being done is the following:
- extract the vertices of the toothbrush's bounding box
- extract the left-most, top-most, right-most and bottom-most points of the mouth, and extract their coordinates
- find whether these two rectangles overlap (which I am doing using a library called
shapely- if you have a more primitive solution that doesn't need the use of a library, that will be great) - if they are overlapping, start the timer; if they aren't, pause the timer. This is the step I am not able to figure out how to do, and where I need help in.
This is roughly how my code looks like:
from shapely.geometry import Polygon
import cv2, face_recognition, time
brush_time = 0
video_capture = cv2.VideoCapture(0)
while True:
ret, frame = video_capture.read()
'''
Code for extracting the toothbrush's coordinates (using Yolo),
and the mouth's coordinates (using the face_recognition library)
...
...
...
'''
p1 = Polygon([(mouth_left_x, mouth_left_y), (mouth_top_x, mouth_top_y), (mouth_right_x, mouth_right_y), (mouth_bottom_x, mouth_bottom_y)])
p2 = Polygon([(brush_top_left_x, brush_top_left_y), (brush_top_right_x, brush_top_right_y), (brush_bottom_left_x, brush_bottom_left_y), (brush_bottom_right_x, brush_bottom_right_y)])
if p1.intersects(p2):
##############################################
# This is where I want the timer logic to be #
# start_time = time.time() #
# end_time = ???? #
##############################################
brush_time = end_time - start_time
text = 'Brushed teeth for {} seconds'.format(str(brush_time))