Build a computer vision timer tracker | OpenCV with Python tutorial

According to research on Computer vision syndrome (a.k.a. digital eye strain) by Mark Rosenfield of the State University of New York College of Optometry, ” Computer Vision Syndrome, also known as digital eye strain, is the set of eye and vision problems associated with computer use. ” For these reasons, especially if you are subjected to many hours of work, it becomes necessary to use a computer vision timer tracker.

In fact according to Understanding and Preventing Computer Vision Syndrome by Sagili Chandrasekhara Reddy :

Proper eye careTaking a short break, stretching the muscles, change ofscenery and a quick walk around the office have been shownto improve productivity and reduce ocular symptoms of stress.Working non stop for more than 4 hours has been associatedwith eye strain. Frequent short break can restore and relaxthe accommodative system of the eyes and preventing ocularstrain and visual fatigue. Workers who have recurrentsymptoms of computer vision syndrome are encouraged toget proper optometrist review and assessment.

For these reasons, I decided to make this small exercise with OpenCV, which allows you to track the time in which you are in front of the computer by automatically resetting it to zero when the software detects that the user has moved away.

Let’s make our computer vision timer tracker

For this exercise, we will only use the OpenCV library and python so by following this quick step-by-step tutorial you will be able to easily complete the project.

Time tracker installation

Before proceeding, install the OpenCV library from the command prompt

pip install opencv-python
time tracker install opencv python

if the installation gives no errors proceed with mediapipe

pip install mediapipe
install mediapipe

mediapipe installation did not give you any errors you can proceed with the exercise. Speaking of mediapipe I also recommend you this project How I built a Computer Vision Game with Opencv, Mediapipe and Python

Write python file

As a first step, import all previously installed libraries and start setting up the code

import cv2
import mediapipe as mp
import time

# Settings
maximum_time = 15 # Seconds

# Load Face Detector
face_detection = mp.solutions.face_detection.FaceDetection()

# Take frame from capera
cap = cv2.VideoCapture(0)

# Track TIME
starting_time = time.time()

In order for the software to check how long your eyes are staring at the computer, it is obviously necessary to turn on the PC’s webcam and obviously process the video stream.

For OpenCV, the video is considered to be a sequence of images so it all has to be put into a while loop so that control can be applied to each individual frame.

while True:
    # Take frame from camera
    ret, frame = cap.read()

    # Display frame
    cv2.imshow("Frame", frame)

this in the image is an initial result

time tracker first frame

To determine how the computer vision timer tracker app counts the time it needs to figure out when the face is in front of the computer. The next step is and then to identify the face.

    # Face Detection
    results = face_detection.process(rgb_frame)
   
    # Is the face DETECTED?
    if results.detections:
       print("Face looking at the screen")

and this is the result

face looking at the screen

Now by adding the conditions, we stipulate that after 15 seconds a window with a red border opens, so it draws our attention and reminds us to take a break.

    # Is the face DETECTED?
    if results.detections:
        elapsed_time = int(time.time() - starting_time)

        if elapsed_time > maximum_time:
            # Reached maximum time, show alert
            cv2.rectangle(frame, (0, 0), (width, height), (0, 0, 225), 10)
            cv2.setWindowProperty("Frame", cv2.WND_PROP_TOPMOST, 1)

        # Draw elapsed time on screen
        cv2.putText(frame, "{} seconds".format(elapsed_time), (10, 50), cv2.FONT_HERSHEY_PLAIN,
                    3, (15, 225, 215), 2)
        print("Elapsed: {}".format(elapsed_time))
        print("Face looking at the screen")
    else:
        print("NO FACE")
        # Reset the counter
        starting_time = time.time()

    # Display frame
    cv2.imshow("Frame", frame)
    key = cv2.waitKey(1)
    if key == 27:
        break

this is the result

computer vision timer tracker take a break

As soon as you move away from the camera everything resets to zero

Blueprint

Learn to build Computer Vision Software easily and efficiently.

This is a FREE Workshop where I'm going to break down the 4 steps that are necessary to build software to detect and track any object.

Sign UP for FREE