DEV Community

Peiguan
Peiguan

Posted on

Are My Eyes Closed in This Selfie? A Python Eye-Aspect-Ratio Check Before a Malfoid AI Upload

People ask assistants things like "will this selfie work if my eyes are almost shut?" and "can a script tell if I blinked?" I build malfoidai.com, an independent unofficial studio that makes a still 3:4 Malfoid (Female Malfoy / Fem Draco) portrait from one photo of an adult, or without a photo. A portrait is $4.99 and spends 2 credits; a delivered one is not refunded as a change of mind, so a closed-eye source is an expensive way to find that out. The photo guide already asks for a clear, front-facing picture of one adult. This post is a small pre-upload check for a narrower question: are the eyes open, squinting, or shut?

I measured a landmark-free eye-aspect-ratio (EAR) detector on synthetic, shape-only sitters (no real face, no AI output, nobody's photo): eight sitters, three known eye states, and three file variants. The combined rule labelled every file correctly.

Short answer

  • Eye aspect ratio, not a trained blink model. For each eye, recover six points around the palpebral fissure and compute the Soukupová–Čech EAR: (||p2−p6|| + ||p3−p5||) / (2 × ||p1−p4||). That is opening height relative to eye width. On these drawings the oracle EAR (from the known geometry) is 0.5625 open (18/32), 0.2500 squint (8/32), 0.0938 closed (3/32).
  • The image-measured EAR is close, and the three states stay far apart. Mean measured EAR over the eight PNG sitters: open 0.5625, squint 0.3125, closed 0.0667. A squint reads a little higher than the oracle (the lash line adds a couple of pixels to the opening) and a closed eye reads a little lower (the lash is thinner than the 3 px construction box). The gaps are still large.
  • Thresholds I shipped: closed if EAR ≤ 0.16; squint if EAR ≤ 0.38; otherwise open. A "retake" flag fires only on closed eyes. A mild squint is reported but not treated as a failed upload.
  • On this set the labels were exact. 8 sitters × 3 states × 3 variants (PNG, JPEG quality 95, and a 5% down-up resample) = 72 files. 3-class accuracy 72/72 = 1.000, including 24/24 on PNG alone. Binary retake (closed vs not-closed): tp=24 fp=0 tn=48 fn=0, precision 1.000, recall 1.000. Every leave-one-sitter-out PNG fold was 3/3. Median analysis time was 20.36 ms per 768×1024 picture.

This is a check on synthetic geometry, not a claim about a particular vendor model, and not a medical blink detector. If you still have a frame with both eyes open, use that one.

The test set

Eight sitters from an extension of make_test_portrait.py (seeds 1–8, 768×1024, --texture). The helper draws an oval face, platinum hair, a white shirt, a green-and-silver tie and a dark robe in a candlelit corridor — shapes only. I then redrew the eyes in one of three known states:

  • open: white sclera 32×18 px, iris inside it (oracle EAR 18/32 = 0.5625)
  • squint: the same width, an 8 px fissure, iris clipped inside it (oracle EAR 8/32 = 0.25)
  • closed: no sclera; a curved lash line about 3 px tall (oracle EAR 3/32 ≈ 0.0938)

Each drawing was also saved as JPEG quality 95 (4:4:4) and as a 95% down-up bilinear resample, so the thresholds are not only true on clean PNGs. That is 72 files. There is no real photograph in the bench, and none of these pictures is a studio result — the site's own showcase frames are labelled "not made by this studio" and I did not use them here.

How the detector finds the six points

A phone selfie will not come with the construction landmarks, so the script does not use them at test time. It:

  1. Finds a face box as the largest skin-coloured blob near the centre of the frame (platinum hair is excluded so the hair oval does not win).
  2. Finds two eye windows in the upper half of that box, one per side, by scoring a combination of horizontal-edge energy (eyelids) and a bright-sclera bonus.
  3. Walks each window column by column. A column counts as part of the eye only if it has a real sclera (much brighter than the local skin) or a compact iris/lash (much darker). Columns that are just skin return nothing. An earlier version invented a 2-pixel "slit" at the darkest row of every column; the measured width grew to the whole window and EAR collapsed toward 0.17 even on open eyes. That fallback is gone on purpose.
  4. Takes the longest contiguous run of eye columns as the width (p1–p4), and the openings at one-third and two-thirds of that span as ||p2−p6|| and ||p3−p5||.
# Soukupová–Čech EAR on the recovered points.
# Thresholds from the 8-sitter bench (open / squint / closed).
T_CLOSED, T_SQUINT = 0.16, 0.38

def ear(open_13, open_23, width):
    return (open_13 + open_23) / (2.0 * width)

def classify(ear):
    if ear is None:
        return "unknown"
    if ear <= T_CLOSED:
        return "closed"
    if ear <= T_SQUINT:
        return "squint"
    return "open"
Enter fullscreen mode Exit fullscreen mode

The retake rule is conservative in one direction: if either readable eye is closed, the verdict is retake_closed_eyes. A mixed open/squint pair uses the mean EAR. An unreadable pair is unknown, not a silent pass.

What the numbers say

Means over eight sitters, from src/dv/work/run_log.txt:

eye      var        n correct  mean_ear     p25     p50     p75  confusions
open     png        8       8    0.5625  0.5625  0.5625  0.5625  open:8
open     jpeg95     8       8    0.5625  0.5625  0.5625  0.5625  open:8
open     downup     8       8    0.5605  0.5625  0.5625  0.5625  open:8
squint   png        8       8    0.3125  0.3125  0.3125  0.3125  squint:8
squint   jpeg95     8       8    0.3125  0.3125  0.3125  0.3125  squint:8
squint   downup     8       8    0.3242  0.3125  0.3125  0.3438  squint:8
closed   png        8       8    0.0667  0.0667  0.0667  0.0667  closed:8
closed   jpeg95     8       8    0.0667  0.0667  0.0667  0.0667  closed:8
closed   downup     8       8    0.0803  0.0812  0.0833  0.0833  closed:8
Enter fullscreen mode Exit fullscreen mode

Open PNG EAR is exactly the oracle 0.5625 on every sitter — the recovered width was 32 px and both openings were 18 px. Closed PNG EAR is 0.0667 (width 30, opening 2). The 5% resample is the noisiest variant, and it still stays on the correct side of both thresholds: the closed down-up mean is 0.0803, well under 0.16; the open down-up mean is 0.5605.

I did not fit a new threshold per sitter. The same 0.16 / 0.38 cuts were applied to every file, including the leave-one-sitter-out PNG folds, all of which were 3/3.

What this is not

It is not a substitute for the rest of a pre-upload checklist. A previous post of mine looked at face count, face size, sharpness and exposure; a later one looked at motion blur versus defocus. Closed eyes are a different failure mode: the picture can be sharp, well exposed and front-facing, and still be a bad source if both lids are shut. The script also does not decide whether a portrait "looks like you," and it will not help if you upload someone else's face — don't do that without asking.

It is also not a general blink benchmark. These sitters are drawn, the eyelids are a clean geometry, and there is no mascara, glasses glare, or extreme head turn in the set. If you adapt the script for a real camera roll, re-measure the thresholds on your own pictures. The useful property to keep is the one that failed in my first draft: do not invent an eye column where there isn't one, or EAR stops meaning opening-over-width.

How to run it

pip install numpy opencv-python-headless pillow
python blink_check.py selfie.jpg --pretty
# verdict is "ok" or "retake_closed_eyes"
# state is open, squint, closed, or unknown
Enter fullscreen mode Exit fullscreen mode

The script prints one JSON object (mean EAR, left and right EAR, the face box, and the thresholds). Exit code 0 means ok; 2 means retake. Seven unit tests ship with it, including "open is open," "closed is closed," "squint sits between the two cuts," and a CLI check. They passed. The bench is bench_blink.py.

A still Malfoid portrait on malfoidai.com is $4.99 / 2 credits at 1K, from a photo or without, with the total on the button before you pay. Guest checkout is fine for a single picture; credit packs need an account, are one-time, and the pricing page says credits never expire. If you are paying for a from-photo portrait, it is worth spending a second to confirm the eyes are actually open.

Malfoid is unofficial fan art: not from the books or films and not affiliated with the author, publishers or studios. A portrait made from a photo is not a verified likeness of that person.

Top comments (0)