Code Monkey home page Code Monkey logo

rpi_eye_blink_detector's Introduction

Raspberry Pi eye blink detector

Someone told me that there're buses that vibrate the seat when the driver closes his eyes. I want to do something similar (Especially because I've bought a camera for my Raspberry pi)

Fist I will use a Relay attached to my Raspberry pi. I can switch on/off with gpio

import RPi.GPIO as gpio
import time

RELAY = 17
gpio.setmode(gpio.BCM)

gpio.setup(RELAY, gpio.OUT, initial=gpio.LOW)

time.sleep(2)
gpio.output(RELAY, True)
time.sleep(2)
gpio.output(RELAY, False)
gpio.cleanup()

Now I also need a bit of openCV magic. I will detect faces and eyes. When I detect faces and no eyes I will trigger the relay. It's just an experiment.

from picamera.array import PiRGBArray
import RPi.GPIO as gpio
from picamera import PiCamera
import time
import cv2

RELAY = 17
gpio.setmode(gpio.BCM)
gpio.setup(RELAY, gpio.OUT, initial=gpio.LOW)
camera = PiCamera()
camera.resolution = (640, 480)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(640, 480))

faceCascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
eyesCascade = cv2.CascadeClassifier("haarcascade_eye_tree_eyeglasses.xml")

time.sleep(0.1)

for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
    image = frame.array

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(50, 50))

    for (x, y, w, h) in faces:
        cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
        roi_gray = gray[y:y + h, x:x + w]
        roi_color = image[y:y + h, x:x + w]
        eyes = eyesCascade.detectMultiScale(roi_gray)
        for (ex, ey, ew, eh) in eyes:
            print eyes
            cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (100, 255, 255), 2)

        if len(faces) >= 1 and len(eyes) >= 2:
            # cv2.putText(image, 'WARNING!', (10, 500), cv2.FONT_HERSHEY_SIMPLEX, 4, (255, 255, 255), 2)
            gpio.output(RELAY, False)
        else:
            gpio.output(RELAY, True)

    cv2.imshow("Frame", image)
    key = cv2.waitKey(1) & 0xFF
    rawCapture.truncate(0)

    if key == ord("q"):
        break

gpio.output(RELAY, False)
gpio.cleanup()

cv2.destroyAllWindows()

Hardware:

  • Raspberry Pi 3
  • Raspberry Pi Camera
  • 1 Relay
  • 1 Bedside lamp

References

rpi_eye_blink_detector's People

Contributors

gonzalo123 avatar

Stargazers

HEMSSI AHMED avatar

Watchers

HEMSSI AHMED avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.