File size: 2,698 Bytes
f32989c
 
 
 
 
 
132a78d
f32989c
7a41def
 
 
f32989c
 
7a41def
f32989c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a41def
 
 
f32989c
 
 
 
 
 
7a41def
 
 
 
 
 
f32989c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a41def
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import cv2
import dlib
import gradio as gr

# Inicialização do detector de faces e olhos
face_detector = dlib.get_frontal_face_detector()
eye_detector = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")

# Variável para controlar a exibição da frase
mostrar_frase = False

# Função para detectar piscadas de olhos
def detect_blinks():
    global mostrar_frase
    cap = cv2.VideoCapture(0)
    eye_closed = False
    blink_count = 0

    while True:
        ret, frame = cap.read()

        if not ret:
            break

        # Converta o frame para escala de cinza para melhor detecção
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        # Detectar faces no frame
        faces = face_detector(gray)

        for face in faces:
            # Detectar olhos nas faces encontradas
            landmarks = eye_detector(gray, face)
            left_eye = landmarks.part(36).x, landmarks.part(36).y, landmarks.part(39).x, landmarks.part(39).y
            right_eye = landmarks.part(42).x, landmarks.part(42).y, landmarks.part(45).x, landmarks.part(45).y

            # Calcular o aspect ratio dos olhos (altura/largura)
            left_eye_aspect_ratio = (left_eye[3] - left_eye[1]) / (left_eye[2] - left_eye[0])
            right_eye_aspect_ratio = (right_eye[3] - right_eye[1]) / (right_eye[2] - right_eye[0])

            # Detectar piscada se o aspect ratio for menor que um limite
            if left_eye_aspect_ratio < 0.2 and right_eye_aspect_ratio < 0.2:
                if not eye_closed:
                    blink_count += 1
                    # Exibir a frase quando detectar uma piscada
                    if blink_count == 1:
                        mostrar_frase = True
                eye_closed = True
            else:
                eye_closed = False

        # Mostrar a contagem de piscadas na tela
        cv2.putText(frame, f"Blinks: {blink_count}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)

        # Exibir a frase se a variável mostrar_frase for verdadeira
        if mostrar_frase:
            cv2.putText(frame, "Quero falar pessoal", (10, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
            mostrar_frase = False

        cv2.imshow("Eye Blink Detection", frame)

        # Parar a execução ao pressionar a tecla 'q'
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    # Liberação dos recursos
    cap.release()
    cv2.destroyAllWindows()

    return blink_count

# Interface do usuário Gradio
interface = gr.Interface(
    fn=detect_blinks,
    inputs=None,
    outputs=gr.outputs.Label(),
    live=True,
    capture_session=True
)

if __name__ == "__main__":
    interface.launch()