Spaces:
Build error
Build error
| 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() |