Making program that warn you to rest your eyes (with python)

Hello everyone, My name is Nonthakon Jitchiranant. This is my first post in English. I’m not native speaker so I might have to say apologize if Imissed something.

As I just said in the article in this post we gonna make a program. That would warn you to rest your eyes when staring at the screen for a time. A time that i meant is about 20 minutes.

following the 20–20–20 rule ( https://goo.gl/LqGhEo).

Let’s have a plan!

So what do we want? We need the program that would detect our face. And when it detected for a while it would warn us. So how could we do?

Well, talking about face detect I just thinking that we might need to use a camera! Then they would warn us when detect for a while. You can actually just print it out through the console. But I need fancy way. Let me use the GUI.

Let’s list the library that we’ll use

  • OpenCV (for detecting eyes)
  • EasyGUI (for GUI things)
  • time (to tell us how much time passed)

Let’s code!


Firstly, let’s import the library that we would use.

Then, we define our variable.

  • face_cascade is use to detect faces(because eyes is always in faces :P)
  • cap is for webcam recording
  • last_time is define as this time for now.
  • elapse stand for the time elapse we are staring right now.

In this part we just making a loop. when elapse is less or equal to 20*60 (convert 20 minute to second because time module using second as a unit). We gonna read the camera. convert in to gray to make it easier to detect faces. Then we find faces so if faces is find.

we define new temporary variable name elapsePerFrame and it would equal to time right now minus by last time that we getting in loop.

plus elapse by it. then print something just like in this frame we detect your eyes or something like that. after that set last_time to current time

but if faces is not detected we gonna print out something and set last_time to current time

then sleep for a while

but what would we do if elapse is more than 20 minute?

We gonna print out something that would make us notice. like ‘Rest your eyes now!’ but to make it more fancy(but not necessary) we gonna make an GUI!

after doing fancy thing just sleep for 20 second to rest our eyes (20–20–20 rule my friend.) then we reset everything (don’t forget to make a fancy thing like GUI. but again not necessary)

OK, so this is all it would take.

I will put my code below here in case you need to try it out. See you later, Good bye :D

https://pastebin.com/ff4MSSyN
see?! it’s working!!