Atahan GüneyEEE

ProjectsProjeler / CV-01

Computer vision · real time · alertingGörüntü işleme · gerçek zamanlı · uyarı

Drowsiness detection with EAREAR ile yorgunluk tespiti

A Python and OpenCV program that watches a face through the webcam, measures how open the eyes are in every frame, and shows a visual alert when they stay closed for too long. It separates a normal blink from eyes that are actually closing.

Kamera üzerinden bir yüzü izleyen, her karede gözlerin ne kadar açık olduğunu ölçen ve gözler çok uzun süre kapalı kalınca görsel uyarı veren bir Python ve OpenCV programı. Normal bir göz kırpmayı, gerçekten kapanan gözlerden ayırıyor.

Webcam→face detectionyüz algılama→eye landmarksgöz noktaları→EAR→threshold + frame countereşik + kare sayacı→alertuyarı

The idea: one number per eye

Detecting "tired" directly from an image is hard. Measuring how open an eye is turns out to be much easier. A facial landmark model places six points around each eye: two at the corners (p1, p4) and two on each lid (p2, p3 on top, p5, p6 below). From those points you get the Eye Aspect Ratio:

Fikir: her göz için tek bir sayı

Bir görüntüden doğrudan "yorgun" sonucunu çıkarmak zor. Bir gözün ne kadar açık olduğunu ölçmek ise çok daha kolay. Bir yüz noktası (landmark) modeli her gözün etrafına altı nokta yerleştiriyor: ikisi köşelerde (p1, p4), ikişer tane de göz kapaklarında (üstte p2, p3; altta p5, p6). Bu noktalardan Göz En-Boy Oranı (EAR) çıkıyor:

p1p2p3p4p5p6 open · EAR ≈ 0.3 closing · EAR → 0
EAR = ( ‖p2 − p6‖ + ‖p3 − p5‖ ) / ( 2 · ‖p1 − p4‖ )

The top of the fraction is the eye's height, measured twice; the bottom is its width. Because it is a ratio, it barely changes when you move closer to the camera or further away. An open eye gives a roughly constant value, and it falls towards zero as the lids close. The left and right eye are averaged to make the signal a bit steadier.

Blink or drowsy?

A single low value is not enough, because every blink also pushes EAR down for a few frames. The program uses two conditions together: EAR must drop below a threshold, and it must stay there for a number of consecutive frames. A blink resets the counter before it reaches the limit; slowly closing eyes do not.

Kesrin üst kısmı gözün iki kez ölçülmüş yüksekliği, alt kısmı ise genişliği. Bir oran olduğu için kameraya yaklaştığınızda ya da uzaklaştığınızda neredeyse değişmiyor. Açık bir göz yaklaşık sabit bir değer veriyor, göz kapakları kapandıkça değer sıfıra doğru düşüyor. Sinyali biraz daha kararlı hale getirmek için sağ ve sol gözün ortalaması alınıyor.

Göz kırpma mı, uyuklama mı?

Tek bir düşük değer yeterli değil, çünkü her göz kırpma da EAR'ı birkaç kare boyunca düşürüyor. Program iki koşulu birlikte kullanıyor: EAR bir eşiğin altına inmeli ve orada art arda belirli sayıda kare boyunca kalmalı. Göz kırpma, sayaç sınıra ulaşmadan onu sıfırlıyor; yavaşça kapanan gözler ise sıfırlamıyor.

0.3thr0 t (frames) blinkkırpma blinkkırpma ALERTUYARI N frames
Illustration of the logic, not recorded data: short dips are blinks, a long dip after N frames triggers the alert.Mantığın çizimi, kaydedilmiş veri değil: kısa düşüşler göz kırpma, N kareden uzun süren düşüş ise uyarıyı tetikliyor.
# core of the loop (simplified)
ear = (eye_aspect_ratio(left) + eye_aspect_ratio(right)) / 2
if ear < EAR_THRESHOLD:
    closed_frames += 1
    if closed_frames >= CONSEC_FRAMES:
        draw_alert(frame)          # red banner on the video
else:
    closed_frames = 0

Things that mattered in practice

  • Lighting. Landmarks get noisy in poor light, and noisy landmarks mean a jumpy EAR. A good, even light source in front of the face helps more than any parameter.
  • The threshold is personal. Eye shapes differ, so a fixed threshold works for some faces better than others. Measuring the open-eye EAR for a few seconds at the start and setting the threshold relative to it is the obvious improvement.
  • Frame rate changes the meaning of "N frames". The same counter means a shorter time on a fast machine. Counting in seconds instead of frames makes the behaviour independent of the computer.
  • Glasses and head angle both change how well the eye points are placed, which is worth testing before trusting the result.

Concepts used

Face detection, facial landmarks, Euclidean distance, scale-invariant features, thresholding with hysteresis-like counters, real-time video loops in OpenCV. See the notes page.

Pratikte önemli olan şeyler

  • Işık. Kötü ışıkta noktalar titriyor, titreyen noktalar da zıplayan bir EAR demek. Yüzün önünde iyi ve eşit bir ışık kaynağı, herhangi bir parametreden daha çok işe yarıyor.
  • Eşik kişiye özel. Göz şekilleri farklı olduğu için sabit bir eşik bazı yüzlerde diğerlerinden daha iyi çalışıyor. Başlangıçta birkaç saniye açık gözün EAR değerini ölçüp eşiği buna göre ayarlamak akla gelen ilk iyileştirme.
  • Kare hızı "N kare"nin anlamını değiştiriyor. Aynı sayaç hızlı bir bilgisayarda daha kısa bir süreye denk geliyor. Kare yerine saniye saymak, davranışı bilgisayardan bağımsız hale getiriyor.
  • Gözlük ve baş açısı, göz noktalarının ne kadar iyi yerleştiğini etkiliyor; sonuca güvenmeden önce bunları test etmek gerekiyor.

Kullanılan kavramlar

Yüz algılama, yüz noktaları (landmark), Öklid uzaklığı, ölçekten bağımsız özellikler, sayaçla eşikleme, OpenCV'de gerçek zamanlı video döngüsü. Notlar sayfasına bakabilirsiniz.