Monday, 14 January 2019

closed eyes detection


closed eyes detection

turn off the lights by closing your eyes

 --------------------------------------------------------------------------------

 

Brain waves - alpha waves 

 

Brain waves are produced by synchronised electrical pulses of neurons. Imagine you are at a party and everybody is having fun. You sit at the table alone and listen. You can hear that everybody is having fun, but you cannot interpret every conversation. If somebody makes a joke, you can hear the laugh, but you don't know what is it about. That is, what approximately brain waves represent. They can be measured by EEG, whose electrodes are placed on the scull.
Well known is alpha brain wave, with frequency from 8 to 13 Hz. Good intuition is: when eyes are open, brain is processing colored images and interpreting them up to about 150 frames per second. When eyes are closed, brain sees only darkness, the activity is decreased and brain switch to another mode of synchrony, which is called alpha. Most of this processes are located in occipital and parietal regions of the cerebral cortex. 


There are also other brain waves bandwidths: beta(13-30 Hz),  delta (1-5 Hz), theta (4-8 Hz).




Projects

 

Because, alpha waves are easy to detect, there are number of projects, which are using them to control cool stuff. For example Michael Reeves used them and beta brain waves to control speed of the car: Using Mind Control to Drive a Car . Also Wired made video about OpenBCI:  How to Control Things Using Your Brain (and Open-Source Hardware) , where were many stuff controlled by alpha waves.
Controlling something by tracking magnitude of alpha frequency is a good way to impress people, because many of them think, that when you close your eyes, you are focusing on something, and EEG can read what you think. 

Controlling light

 

I've decided to impress people by controlling ligh, with OpeBCI Ganglion, which is open sourced 4 channel EEG bluetooth device with CSR 4.0 dongle and Raspberry pi 3b+. I had few difficulties with connection - a lots of dropped packets, but I managed to solve it - see this thread in openbci forum.
My goal: create program, which can in real time determine if mine eyes are closed or open, based on magnitude of alpha frequency and turn on or off lights.

Recording and preprocessing data

 

For recording data I used OpenBCI_Python library and for data processig brain_waves_openbci, which I wrote for brain waves frequency analysis. It basically receives data, filters them, apply FFT (Fast Fourier Transform) and pass them to the callback function, which is specified by user. Learn more about this process here: eeg-data-processing-in-python
I placed only 1 electrode by 10-20 system on O1 position on skull. 


import brain_waves_openbci as waves
from openbci.ganglion import OpenBCIGanglion

eeg = OpenBCIGanglion()
channels_num = 4
fs = 200 # recording frequency of ganglion
br = waves.Brain_waves(eeg, channels_num, fs)

def nothing(sample):
    pass


br.start_streaming(nothing, None, 25, file_name = 'data/alpha_test.csv')

Brain_waves.start_streaming first argument is function / callback object to which magnitude of selected frequency bandwidth is passed. I wanted to only record data, so function does nothing. Second argument is bandwidth, which is also not needed (for now). If file_name is specified, recorded data are collected in csv file.
 

Selecting threshold

 

For selecting threshold , it is good to visualize data.

import matplotlib.pyplot as plt
import brain_waves_openbci as waves

channels_num = 4
fs = 200 
alpha_band = (8,13)

br = waves.Brain_waves(None, channels_num, fs)
band_vals, time = br.read_csv_waves('data/alpha_test.csv', alpha_band)
plt.plot(time,band_vals[0])
plt.show()
 

It is easy to see on the plot, that when I closed my eyes, the magnitude of alpha frequency increased. I selected threshold for closed eyes to be 0.34

Real time

 

I've connected light to a relay, which I wired to Raspberry pi 's GPIO pins. Raspberry pi connects to OpenBCI board, receives data in real time, process them,  compute magnitude of alpha frequency and if is more than threshold  switches relay.  I won't share the code, but instead of that there is a simple real time closed eyes analyzer for one channel.


import brain_waves_openbci as waves
from openbci.ganglion import OpenBCIGanglion

threshold = 0.34

eeg = OpenBCIGanglion()
channels_num = 4
fs = 200
band = (8,13)
br = waves.Brain_waves(eeg, channels_num, fs)

def check_magnitude(sample):
    if sample[0] > threshold:
        print('eyes are closed')
    else:
        print('eyes are open')

br.start_streaming(check_magnitude, band, 25)



...

 


Are you too lazy to turn the lights off ? Or do you wanna save electricity by turning on the lights only when needed ? This is for you!
.
.
.
Just kidding, this is useless.

Feel free to use brain_waves_openbci to make something better than this.

closed eyes detection

closed eyes detection turn off the lights by closing your eyes  --------------------------------------------------------------------...