Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import dlib
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
# Inicialização do detector de faces e olhos
|
| 6 |
+
face_detector = dlib.get_frontal_face_detector()
|
| 7 |
+
eye_detector = dlib.shape_predictor("path_to_shape_predictor_68_face_landmarks.dat")
|
| 8 |
+
|
| 9 |
+
# Função para detectar piscadas de olhos
|
| 10 |
+
def detect_blinks():
|
| 11 |
+
cap = cv2.VideoCapture(0)
|
| 12 |
+
eye_closed = False
|
| 13 |
+
blink_count = 0
|
| 14 |
+
|
| 15 |
+
while True:
|
| 16 |
+
ret, frame = cap.read()
|
| 17 |
+
|
| 18 |
+
if not ret:
|
| 19 |
+
break
|
| 20 |
+
|
| 21 |
+
# Converta o frame para escala de cinza para melhor detecção
|
| 22 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 23 |
+
|
| 24 |
+
# Detectar faces no frame
|
| 25 |
+
faces = face_detector(gray)
|
| 26 |
+
|
| 27 |
+
for face in faces:
|
| 28 |
+
# Detectar olhos nas faces encontradas
|
| 29 |
+
landmarks = eye_detector(gray, face)
|
| 30 |
+
left_eye = landmarks.part(36).x, landmarks.part(36).y, landmarks.part(39).x, landmarks.part(39).y
|
| 31 |
+
right_eye = landmarks.part(42).x, landmarks.part(42).y, landmarks.part(45).x, landmarks.part(45).y
|
| 32 |
+
|
| 33 |
+
# Calcular o aspect ratio dos olhos (altura/largura)
|
| 34 |
+
left_eye_aspect_ratio = (left_eye[3] - left_eye[1]) / (left_eye[2] - left_eye[0])
|
| 35 |
+
right_eye_aspect_ratio = (right_eye[3] - right_eye[1]) / (right_eye[2] - right_eye[0])
|
| 36 |
+
|
| 37 |
+
# Detectar piscada se o aspect ratio for menor que um limite
|
| 38 |
+
if left_eye_aspect_ratio < 0.2 and right_eye_aspect_ratio < 0.2:
|
| 39 |
+
if not eye_closed:
|
| 40 |
+
blink_count += 1
|
| 41 |
+
eye_closed = True
|
| 42 |
+
else:
|
| 43 |
+
eye_closed = False
|
| 44 |
+
|
| 45 |
+
# Mostrar a contagem de piscadas na tela
|
| 46 |
+
cv2.putText(frame, f"Blinks: {blink_count}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
|
| 47 |
+
cv2.imshow("Eye Blink Detection", frame)
|
| 48 |
+
|
| 49 |
+
# Parar a execução ao pressionar a tecla 'q'
|
| 50 |
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
| 51 |
+
break
|
| 52 |
+
|
| 53 |
+
# Liberação dos recursos
|
| 54 |
+
cap.release()
|
| 55 |
+
cv2.destroyAllWindows()
|
| 56 |
+
|
| 57 |
+
return blink_count
|
| 58 |
+
|
| 59 |
+
# Interface do usuário Gradio
|
| 60 |
+
interface = gr.Interface(
|
| 61 |
+
fn=detect_blinks,
|
| 62 |
+
inputs=None,
|
| 63 |
+
outputs=gr.outputs.Label(),
|
| 64 |
+
live=True,
|
| 65 |
+
capture_session=True
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
if __name__ == "__main__":
|
| 69 |
+
interface.launch()
|