- Artificial intelligence
- Development & tooling
- Cybersecurity
- Methods
Blink: a neural network trained on your own eyes
A macOS app that counts your blinks through the webcam, with a convolutional network it trains on your own eyes, on your Mac, in about twenty seconds
Context
People blink much less when they look at a screen, and for dry eyes that is exactly when it hurts. Blink is a macOS menu-bar app that watches for it: it counts your blinks through the webcam, shows your rate minute by minute, and reminds you when it stays low.
The hard part is the model. Eyelids, lashes, glasses and light differ from one person to the next, so Blink does not ship one model for everybody: it ships without a model and builds one with each user. A voice guides two recordings of 16 poses, the app trains a convolutional neural network on those pictures on the Mac itself, in about twenty seconds, then tests it while you blink. The whole setup takes about eight minutes.
Only one of the four stages is machine learning, and I made it as small as possible on purpose. Apple Vision finds the eyes, each eye is cut to a 64×64 picture, the network answers one question (how sure is it that this eye is closed?), and plain rules turn thirty answers a second into blinks. A question that small needs a few thousand pictures, which one person records in four minutes.
I measured every figure I give, and the results decided what I changed. The model is judged on recording sessions it never saw, then on real blinks marked by hand frame by frame. The trainer written in Swift is held to the PyTorch reference on the same pictures and the same splits. The app’s cost is profiled stage by stage before anything is optimised. And nothing leaves the Mac: the app makes no network connection at all.
Architecture
Four stages, thirty times a second, and a training pipeline inside the app
What the app does
From the menu bar to the training window: the setup that builds the model with you, what the model sees, and what the app keeps.
-
Open the screenshot
The menu-bar panel
Your rate over the last minute, today at a glance and the last hour minute by minute, in orange under your limit. On the right, the same panel when the rate stays low, with the blink break highlighted.
-
Open the screenshot
Recording, labelled by instruction
A voice asks for 16 poses, eyes open and closed, three seconds each, twice. Every picture is saved with the pose it was taken in: nobody labels thousands of pictures by hand.
-
Open the screenshot
Training on the Mac
The network sees every picture 30 times, altered each time: turned, shifted, darker, blurred, so that the eyelid is the only thing left to learn. The time left is measured on this Mac, not guessed.
-
Open the screenshot
Live View
What the app sees: the model’s answer over the last ten seconds, each counted blink as a green band, both eyes read separately, and how long the last blink lasted.
-
Open the screenshot
History
The last seven days, a day hour by hour, the week as a heat map, and what stands out in one sentence, here in light and dark mode. Only numbers are stored: one line a minute in a CSV file.
-
Open the screenshot
The data, and where the first model failed
Top: the 16 poses of one recording, open and closed. Bottom: one real blink, frame by frame. The first model saw the lit eye close and missed the one in shadow; 350 pictures of real blinks fixed it.
The app draws its own screenshots, with made-up data, and leaves the camera picture out. The eye pictures of the last one are mine, from the project’s documentation.
What it achieves
Measured on one person, me, on recordings made in one morning: read them as “it works for its author”, not as a general claim. Every figure says how it was measured.
-
99.7%
Eye pictures read correctly, on sessions the model never saw
4,713 pictures. Each recording session is judged by a model trained without it: pictures of one session are near copies, and a random split would flatter the model.
measured
-
72 of 72
Real blinks counted, in 122 s of video marked by hand
One false blink. Each capture is replayed through a model that never saw it. The first version found 57.
measured
-
936 of 936
Blinks found by the app’s own Swift trainer, over 13 trainings
The same as the PyTorch reference on the same pictures and splits (7 false blinks against 6). Core ML’s own on-device training found 211 of 216.
measured
-
14 of 16
Mistakes in the recordings caught before training
A pose held the wrong way, judged by four models that never learned it. No good pose flagged in 37 trials. One such pose had made the model count 14 to 25 false blinks.
measured
-
9.1 → 0.12 ms
Handing one eye picture to the model
A profile put 968 of 1,003 samples of the frame pipeline in one call. Memory went from 2.6 GB after four hours, a leak, to 240 MB.
measured
-
−34%
Processor work while it watches, from version 1.0 to today
915 to 600 million instructions a second with a real camera, the same blinks counted. The graphics processor went from busy 4% of the time to under 1%.
measured
How I built it
- A recording protocol instead of hand labels: 16 poses, each recorded eyes open and closed so that the network cannot learn the head instead of the eyelid, some open poses chosen to look almost closed (the keyboard, a wide smile), one frame in three kept.
- EyeNet, a small convolutional network: four convolution stages from 64 to 4 pixels, global average pooling and one output, 60,465 weights in 250 KB. The preprocessing (greyscale, same brightness and contrast) lives inside the model, so the app cannot do it differently by mistake. Augmentation: mirror, rotations up to 35°, zoom, shift, blur.
- An evaluation that cannot flatter the model: leave-one-session-out cross-validation on pictures, then blink counting on hand-marked video, each capture replayed through a model that never saw it: found, missed, false.
- Training inside the app, so that nobody has to install 700 MB of Python: the same network, augmentation, optimiser (AdamW, one-cycle schedule) and 30 passes rewritten in Swift on MPSGraph, Apple’s framework for running and differentiating a network on the graphics processor. It runs in a process of its own: if the graphics frameworks crash, only the training run is lost, not the app.
- Two forms of every model, checked against each other: trained on the graphics processor with batch normalisation, then run on the processor as a network written out in Swift, with the normalisation folded into the weights. A model whose two forms differ by more than 0.001 is not installed, and a new model never overwrites the one in use.
- Counting with five tested rules rather than a second network: hysteresis (closed above 60%, open again below 40%), two closed frames or one at 80%, at most 0.6 s, nothing counted across a gap, and no blink if the eyes moved more than 0.15 of their distance: a nod is not a blink.
- Performance work driven by profiles, never by guesses: pixel buffers handed to the model in its own format, the face looked for twice a second instead of fifteen, Core Image’s resizing rewritten as plain arithmetic (6,755 pictures compared value by value), and the Swift network’s memory copies removed with answers identical to the last bit.
- Privacy and security by design: no network connection, every camera picture discarded once read, only 64×64 eye pictures kept, in folders only the user’s account can read. A hardened runtime with a single entitlement, the camera. A security review closed a path traversal in the training manifests and made private the log lines that could hold an account name; the CI pins its actions to commits.
- A personal limit that cannot hide a decline: the usual rate is measured once and kept, so a slow drop is reported instead of absorbed. The ranges the app shows come from a review of 56 sources, 16 of them read in full.
- Built with Claude Code as a coding agent, under written rules: one heavy job at a time, every change tested, every number measured before it is written down. 446 Swift tests and 32 Python tests run in CI on every push.
Delivered
- Blink 1.0, a macOS app with a hardened runtime, in a disk image published on GitHub under the MIT licence
- The setup: camera check, two recordings, training and test, about eight minutes in one window
- A PyTorch lab: reference trainer, Core ML conversion checked answer by answer, labelling and evaluation tools
- Two command-line tools: BlinkTrain, the app’s trainer on the comparison’s splits, and BlinkReplay, recorded frames through the pipeline with their cost in instructions
- Four documents: how it works, a review of the blink-rate studies, twelve lessons learnt and a developer guide
Terms
- CNN (convolutional neural network)
- A network that slides small pattern detectors over a picture. The same detector is used everywhere, so it needs few weights and learns from few pictures.
- Leave-one-session-out
- Each recording session is tested by a model trained on all the others. Pictures of one session are near copies, so a random split would overstate the score.
- MPSGraph
- Apple’s framework for building and differentiating a network on the Mac’s graphics processor. PyTorch’s own Mac backend is built on it.
- Hardened runtime
- A macOS protection that stops other programs from loading code into the app, which gets only the rights it declares: here, the camera.
Problems solved
-
99.6% on pictures, but half the fast blinks missed in real use
Played frame by frame, one blink showed that the model read the lit eye 100% closed and the eye in shadow 16%: eyes held still had taught it nothing about a dim lid blurred by its own speed. Labelling 3,600 frames by hand was out of the question, so a rule did it (both eyes blink together), checked against a second source, Vision’s eye outline. With 350 pictures of the right kind, two cued recordings went from 13 and 19 blinks out of 20 to 20 out of 20 each.
-
One call took almost all the processor time
The first build used 60% of a core and reached 2.6 GB of memory in four hours. I expected face finding to be the cost; a profile put 968 of 1,003 samples in the line that hands an eye picture to Core ML, which built a new Core Image context for each one. Handing it a pixel buffer in its own format took that step from 9.1 ms to 0.12 ms.
-
A pose held the wrong way
The labels come from the voice, so a recording where I kept my eyes open through “Close your eyes” taught the model 60 open eyes as closed, and it counted 14 to 25 false blinks. Each pose is now judged before training by four models that never learned it, and only a pose read the other way without doubt is flagged: 14 of 16 mistakes caught, no false alarm in 37 trials.
-
A nod is not a blink
From a camera above the screen, a lowered head shows its upper lids and the model answers “closed”: a minute of nodding counted 52 blinks. Logging what each stage saw showed two causes behind one symptom, and one measurement told both apart from a blink: the eyes move 0.06 of their distance in a real blink, 0.16 to 1.18 in a nod. A fifth rule took the count to 20, and lost none of the marked blinks counted before.
Limits and next steps
What results measured on one person cannot prove yet.
-
One person, one morning
The model has only seen its author, in daylight, and every figure is mine. That is why the setup ends with a test: each user checks their own model before it counts.
Measure it on other people, in other light, with other cameras.
-
The battery does not see the savings
The work was cut by a third, yet the whole Mac draws about 2 W more while a face is in view, at every level of effort. The processor’s part is 0.05 W; what the rest is made of is not known.
Measure the camera and Vision alone to find where those 2 W go.
-
Complete blinks only
Studies link dry eye more to the share of incomplete blinks than to the blink rate. Blink counts complete blinks, and the frames of real blinks only train the PyTorch route so far.
A third answer, half closed, learnt from real blinks by the app’s own trainer.
Skills demonstrated
Each skill links to the skills map on the Projects tab.
- Machine learning: training & evaluation
- Deep learning (convolutional networks)
- Computer vision (Apple Vision)
- On-device AI (Core ML, MPSGraph)
- Datasets: collection & labelling
- PyTorch
- Agentic AI & LLMs (Claude Code)
- Swift / SwiftUI
- Python
- System & network hardening
- Automated tests & quality gate
- Technical documentation