Lucid Pi Prototype REM Sleep Mask

The first prototype version of the Lucid Pi sleep mask to detect REM and send a dream signal is up and running. Currently only the hardware is functioning. Software development is next.

Here you can see the overall system with portable power.

It’s based on the Raspberry Pi Zero W.

https://www.adafruit.com/product/3400#tutorials

Portable power is provided by a RAVPower 10000mAh power bank.

https://www.amazon.com/gp/product/B075ZR745R/ref=ppx_yo_dt_b_asin_title_o01_s00?ie=UTF8&psc=1

It’s built on top of a Mindfold sleep mask.

https://www.ebay.com/itm/MINDFOLD-RELAXATION-MASK-SLEEP-THERAPY-EYE-MASK-TRAVEL-EYE-MASK-FREE-EARPLUGS/391929042792

Uses the bottom half of a Pi Zero case for support.

https://www.ebay.com/itm/Protective-Shell-Case-Enclosure-For-Raspberry-Pi-Zero-Zero1-3-Module-Transparent/312633072467

Here’s a closer look at the mask which is wired with common dupont connectors.

A SainSmart Mini Noir camera module is used to monitor the left eye.

https://www.amazon.com/gp/product/B075WV92XJ/ref=ppx_yo_dt_b_asin_title_o00_s00?ie=UTF8&psc=1

Infrared LEDs, whose output cannot be seen and is generally considered safe, is used to illuminate the eye.

https://www.amazon.com/gp/product/B07VD1KXR1/ref=ppx_yo_dt_b_asin_title_o02_s00?ie=UTF8&psc=1

A variable resistor is used to adjust the brightness of the IR LEDs. Presently 10 mA is flowing through each of the four LEDs.

Input is provided by the user with a momentary on button.

https://www.ebay.com/itm/79BA-Matrix-Keyboard-Keypad-Push-Button-Module-Key-Membrane-Switch-For-Arduino/163500765170

Here you can see the user input button and variable resistor used as a dimmer.

Dream and other output signals will be sent using a surface mounted RGB LED to the likely dominate right eye.

https://www.ebay.com/itm/RGB-3-Color-Full-Color-LED-SMD-Module-For-Arduino-AVR-PIC-New-Hot/301454294497

Here you can see the camera as well as the illuminating IR LEDs on the left. On the right is the four color surface mounted LED used to send signals.

In addition to the Raspbian Linux operating system, software includes OpenCV for computer vision image processing and gpiozero to greatly simplify Python programming for the input button and output RGB LED.

To test the hardware configuration, I’m presently recording eye movements.

https://drive.google.com/open?id=1NzMFOmX1HwICMll4UiS-W6cWnP5f9uMO

The focal length of the camera needed to be adjusted to focus on the eye. Factory setting for the camera is 50 cm to infinity distance.

Next I’ll be looking at various image processing algorithms to detect REM, such as frame differencing with OpenCV.

https://subscription.packtpub.com/book/application_development/9781785283932/9/ch09lvl1sec74/frame-differencing

4 Likes

Woah that’s pretty cool. How much did this cost $ it total? Also do your eyes have to be open to detect anything? Sorry it’s all quite technical and confusing tbh, but super cool!

1 Like

Costs are roughly $15 for the Raspberry Pi Zero W, $12 for the sleep mask, $10 for the infrared camera, $6 for a micro SD card, $1 for a RGB led module, $1 for a Pi case, and a few more dollars for miscellaneous parts like resistors, connectors, electrical tape, cable ties, etc. It totals to about $50. Please be aware, though, that I order some parts from Asia on eBay. If you want them sooner, then you’ll have to pay more. It takes roughly a month for such parts to arrive, and some are lost in transit.

Next I’m going to use computer vision software to see if REM can be detected. Most likely there will be two thresholds, one to signal REM while in the sleeping state with eye lids closed, and a higher one to signal that normal eye moments are occurring with the eye lids open. A dream signal will be set when the lower threshold is met, but the higher one not exceeded. The input button could also be used to flash a state check signal.

If all goes well, since the Raspberry Pi Zero W has both blue tooth and wifi, then blue tooth headsets and speakers as well as home automation devices, e.g. smart power switches, might eventually be added to the system. Headsets and speaker might be used to send audio messages. Smart switches might be used to turn on a vibration pad or other electrical devices.

2 Likes

Looks like it might be fairly easy to differentiate between eyes closed and simulated “REM.” I simulated “REM” by rapidly moving my eyes with lids closed. Differentiating between simulated “REM” and eyes open, though, appears to be more difficult.

Here’s a recording of my left eye, just closed normally. I hope it’s typical of the eye when sleeping, but not in REM. The video is in gray scale at one quarter the actually color recording resolution, which is the usual input into the frame differencing algorithm referenced earlier.

https://drive.google.com/open?id=1FIW2_DkRBQ15w0BLeJCoPezV3HEEAKmV

(Sorry if you have any trouble viewing the MP4s. Video codecs which produce the MP4s on the Raspberry Pi are tricky. The format provided was the best I could do. It is viewable on my Windows machine, but may not be on Mac or other platforms. Looks like Google Drive’s video processing algorithms are having some trouble dealing with the format, so you will have to download the video to play it.)

Here’s a recording of the frame differencing algorithm run on the video with eye closed.

https://drive.google.com/open?id=1wo0GR7wlSoJ8XGUINnJbVmUVG74vvHwn

A black pixel in the image means that there is no movement detected for that pixel. A white pixel means that a great deal of movement was detected for that pixel. Some level of gray for the pixel means that movement was detected for that pixel. The lighter the shade of gray, the greater the movement.

The frame differencing images are just two dimensional arrays, containing integers between 0 and 255 indicating a gray scale. 0 is black, and 255 is white. The magnitude, a.k.a. norm, of the image arrays were calculated for each individual picture and plotted in the graph below.

The X axis is the frame or picture number, and the Y axis a calculation of the magnitude or norm of all the array’s elements. With eyes closed, the mean of the magnitude was 357.0, and the standard deviation of the samples was 236.5.

closed

Next, I simulated “REM” by moving my eyes rapidly with lids shut. Here’s a video of the simulation.

https://drive.google.com/open?id=1rM1YhRf6KRhc74l6zkQM-gD0KNF3NtV0

And here’s the associated frame differencing video.

https://drive.google.com/open?id=1dJ_vCNcvMQQMikQqbQYeajJlr_wV17zc

Again, the norm of the gray scale image arrays were plotted for each picture in the frame differencing video. The mean with eyes moving was much higher at 1232.8, as was the standard deviation at 630.7.

rem

I ran the same experiment five times, with slight shifts in the camera location. (I cannot shorten the focal length of the camera any more without the lens falling out. The somewhat blurred image of the eye lid is the best that can be done presently, but it still seems to work fine.) The average norm of the simulated “REM” was always three or four times bigger than that with eyes still and closed. The standard deviation was always two or more times bigger as well.

Consequently I think it might be possible to detect actual REM using a rolling average. For instance, if the rolling average of maybe 100 frame difference samples, over a period of roughly five seconds, is greater than a threshold of about 800 then REM may be likely.

Unfortunately detecting eyes open, perhaps upon waking during the night, appears to be much more challenging. Here’s a video of an open eye, looking around a bit, with a good deal of blinking.

https://drive.google.com/open?id=1XVXqfyedTvdxOUpAdrCD84wuZ5KQkGtI

Here’s the associated frame differencing video.

https://drive.google.com/open?id=13Th7OSVAd3rbHGeP9lDbgV2CEHaxhJnc

Like before, the norm of the image arrays is plotted in the graph below.

open

Unfortunately the mean magnitude of the arrays is usually pretty close to that of the simulated “REM.” (However, the standard deviation appears to be larger, and the peaks tend to be higher.) Such calculations though, does depend upon what the subject does when their eyes are open. This might not be terribly predictable or uniform. Past REM detecting sleep masks usually skip this eyes open use case, perhaps for good reason!

Other methods might be deployed to determine if the eye is open, such as circular object detection of the pupil and iris, should the the Raspberry Pi Zero have enough processing power.

https://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/hough_circle/hough_circle.html

Also it was common for earlier lucid dreaming masks to ask the user to indicate that they are going to sleep. No dream signals are sent until its likely that they have fallen asleep, after a certain amount of time has passed.

1 Like

Hello, good for you for getting this far!

I spent quite some time thinking about and working with similar projects in the past. I made progress on my own remote eye movement sensor with a Pi, but shelved it part way through.

Does the system run with the cable or can you power the Pi Zero with a battery?

You could incorporate a simple accelerometer / movement sensor that could eliminate most false REM detections when awake or falling asleep. Another feature that would probably help a lot is some kind of voice control to quickly and without body motion putting the device into different modes (e.g., WBTB, alarm-DEILD mode, for example).

1 Like