Position monitor detector in Ubuntu(build with opencv on python3) | by Dmitriy Kisil | Towards Data Science

And keep distance appropriate, because sitting to close is not good for your eyes. After learning a bit of opencv library, I am thinking about the simple implementation of this idea.
According to ergonomics tips(get it from here):
Adjust the monitor height so that the top of the screen is at — or slightly below — eye level. Your eyes should look slightly downward when viewing the middle of the screen. Position the monitor at least 20 inches (51 cm) from your eyes — about an arm’s length distance.
Let’s create a program in Python3, which will remind us each time when we sit down too close (distance<51 cm or 20 inches) to monitor and display a message with warning. So, for face recognition part we will use default classifier from opencv, and tkinter as a way to send message on the screen.
Opencv is an awesome library, which allows you to do various operations on the image/video content such as convert to grayscale, make a mask, cropping, rescaling the image and many-many more.
After looking at this example from opencv docs, I think, that this is especially what I need to succeed goal — make simple face detection. Just tweaking example a little bit — remove eyes classifier, and that’s all.
The next problem is how to measure distance from face to camera. After searching a little, I found super article from pyimagesearch. In this material you can found that measuring distance could be calculated from height of the object now and knowing height of object from particular distance.
For example, height of object in pixels are 100 from distance 1 m. If we run detection and see, that height of object are 50 pixels — then object approximately two times further. If 120 pixels — then object is a little closer then 1 meter. So we simple check if height of face now is more then knowing face height from a selected distance — then send a message with tkinter.
Tkinter is a simple GUI tool, which get what we need: popup widget, which appeared if and only if face is detected and face too close to monitor. Just creating a one text widget with text of warning should be enough.
So, let’s implement all of that!
Install Tkinter:
sudo apt-get install python3-tk
Install additional python packages:
pip3 install numpy opencv-python
Create python script:
touch distance_detector.py
Fill script with the code below (added a lot of comments to code, so you can understood what’s going on here):
Quick explanation:
use argparse to allow set distance and known height from cmd(9–14);function, which create our tkinter message and return it(16–27);capture video from camera, grayscale it and make face detection(32–43);when no face detect(44–46) or face far away(56–59) — disable tkinter message. If don’t do that, warning message won’t disappear after one successful detection;when face detect — calculate a distance to detected face(48–52);if distance less then selected distance — enable tkinter message(53–55);show useful info in opencv window and close all after press Q (60–81).
use argparse to allow set distance and known height from cmd(9–14);
function, which create our tkinter message and return it(16–27);
capture video from camera, grayscale it and make face detection(32–43);
when no face detect(44–46) or face far away(56–59) — disable tkinter message. If don’t do that, warning message won’t disappear after one successful detection;
when face detect — calculate a distance to detected face(48–52);
if distance less then selected distance — enable tkinter message(53–55);
show useful info in opencv window and close all after press Q (60–81).
Before using we need to calibrate it according to your face. To doing that:
run python3 distance_detector.py;sit down in a front of camera on distance, which will be limit for eyes;notice face_height parameter and close script (press Q in window);run python3 distance_detector.py with your face_height parameter.
run python3 distance_detector.py;
sit down in a front of camera on distance, which will be limit for eyes;
notice face_height parameter and close script (press Q in window);
run python3 distance_detector.py with your face_height parameter.
For example, my face height in pixels is 300, when i sit from 51 cm. In my case, I need to run python3 distance_detector.py -f 300 -d 0.51.
How it looks like:
In this article you see, how after decomposition the idea into small chunks actual implementation became is not so hard. Yes, you can added more techniques to it to make detection work more accurate and improve performance — but as simple example of idea that’s should be enough.
P.S.: If you wear glasses, be aware that detection will be less accurate.
Thanks for your attention! Wish you have an awesome day=)