> For the complete documentation index, see [llms.txt](https://help.imotions.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://help.imotions.com/lab/analysis/eye-tracking/other-eye-tracking-analysis/r-notebook-eye-tracking-based-blink-detection.md).

# R-Notebook: Eye tracking-based blink detection

Run the eye tracking-based blink detection R notebook to detect blinks from gaps in gaze data and get blink count, blink rate and interblink intervals.

‘Blinking’, the rapid closing of the eyelids, is a semi-autonomous body function that is associated with psychological processes, including attention processes (along with retaining moisture across the eyes).

The eye tracking (ET)-based blink detection R-Notebook detects and reports eye blink based measure of eyelid opening/closure. If eyelid data is not available, blinks are interpreted from gaps in gaze data.

## Applying the ET Blink Detection (R-Notebook) to an analysis

The ET-based blink detection can be added to an analysis during the initiation of a new analysis, or later, by returning to the signal processing page from within an existing analysis.

[What are R-Notebooks?](https://help.imotions.com/lab/analysis/signal-processing-r-notebooks/what-are-r-notebooks.md)

![Slide1.PNG](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-51a463679010ae59c8562f98cac2a9487a52dc67%2Fdcb248cd45_slide1-6-.webp?alt=media)

#### Video Walkthrough

This video provides a brief walkthrough and explanation of the notebooks for both eye tracking and facial expression analysis-based blink detection:

{% embed url="<https://vimeo.com/876318043>" %}

{% hint style="info" %}
NOTE:

You are now able to add multiple "Active flows" of the Eye Tracker Blink Detection R Notebook, with different parameter settings for example. You will be able to get corresponding results altogether later. Each "Active flows" must have a different, unique name.
{% endhint %}

## Details about the algorithm

The algorithm is based on either ‘eyelid opening/closure’ or ‘missing values’ in the eye tracking data. ‘Missing values’ occur during an eye blink, for the brief moment of time that the eyes are fully covered by the eyelid.

The algorithm first tags all instances of eyelid data or ‘missing values’ that meet blink definitions/thresholds in *both the left and right eye*. Three evaluation steps are applied (as described below).

Detected blinks are used to calculate Blink Count (total blinks), Blink Rate (blinks per minute), and Interblink Intervals (average time between blinks).

Note: ‘Missing values’ can also be present for other reasons – for instance, because respondents are looking away from the screen, because the eye tracker is temporarily covered by something (e.g., by the hand of the respondent during tablet testing), or simply because of poor data quality. These elements are considered by the algorithm.

* **Evaluation 1**: Two chunks of ‘missing values’ that are separated by only a few samples are usually part of one and the same eye blink; these chunks are first merged (see parameter: minimum duration between two blinks).
* **Evaluation 2**: Instances of ‘missing values’ shorter than a minimum duration can be due to incidental data loss rather than blinks; these instances are removed (see parameter: minimum duration).
* **Evaluation 3**: Typical blinks have a duration of about 100 to 400 milliseconds. Instances of ‘missing values’ longer than that are often due to reasons that are not blinks – for instance, the respondent looking away from the screen; these instances are removed (see parameter: maximum duration).

## Parameters

The sensitivity of the blink detection R-Notebook can be altered by changing the following three parameters:

* **Minimum duration \[ms] needed between two blinks to be considered as separated:** The minimum duration between two blinks in milliseconds (i.e., from the last ‘missing data’ sample of the first blink to the first ‘missing data’ sample of the next blink)
* **Minimum blink duration \[ms]:** The minimum duration of a blink in milliseconds (i.e., duration of ‘missing data’)
* **Maximum blink duration \[ms]:** The maximum duration of a blink in milliseconds (i.e., duration of ‘missing data’)

The defaults are set to:

* **Minimum duration \[ms] needed between two blinks to be considered as separated:** 70 milliseconds
* **Minimum blink duration \[ms]:** 20 milliseconds
* **Maximum blink duration \[ms]:** 500 milliseconds

![](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-382aa0213809b81e7e97fbcceed184a4e518878f%2F3984fd58d2_image-1698924778329.webp?alt=media)

These parameters were chosen because they rendered the most optimal results during our in-house testing (which was done with a 60 Hz eye-tracker).

Note that the minimum duration of 20 milliseconds roughly corresponds to the duration of one ‘missing data’ value (as a 60 Hz eye tracker records one data sample every 16.7 milliseconds). If your eye tracker has a *lower* sampling rate, for instance, of 30 Hz, it is recommended to change the parameter to 35 milliseconds – which, in this case, is roughly the duration of one ‘missing data’ value (as a 30 Hz eye tracker records one data sample every 33.3 milliseconds). If your eye tracker has a *higher* sampling rate, for instance, 100 Hz, it could be a good idea to lower the minimum duration further to 10 milliseconds (as a 100 Hz eye tracker records one data sample every 10 milliseconds).

## What the R-Notebook delivers: replay, export files, report

The ET-based blink detection R-Notebook performs the detection of eye blinks and further computes **(1)** the total number of blinks during a stimulus, scene and/or annotation, and **(2)** the corresponding blink rate during the same time windows (i.e., the number of blinks per minute). **(3)** the average of interblink intervals.

* **Replay:** The R-Notebook adds two sections to the signals page in the Replay: ‘Blink Detection’ and ‘Blink Detection Metrics’. The section ‘Blink Detection’ contains a binary signal that can be used for visualizing the blinks on the timeline (**Blink Detected**). The section ‘Blink Detection Metrics’ further provides three quick statistics – the total number of blinks (**Blink count**), blink rate (**Blinks per minute**) and average of interblink intervals (**Average Interblink Intervals (ms)**) for the respondent and stimulus currently displayed. Note that, here, this is only the blink count, blink rate and interblink interval for the *entire* stimulus duration.

![](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-929124b220706cbc5f7d1d02551f66bfeb40d5e9%2F92fe28d54f_image-1698855132890.webp?alt=media)

Now in the replay window, you will also be able to obtain Analysis markers generated by the R Notebook. Please see the screenshot below as an illustration of where to find them. The Analysis markers for Eyetracking Blink Detection R Notebook are composed of "ET Blink Start" and "ET Blink End". Each marker section is named after the name of the "Active flows" user-defined when setting up Analysis. Users can also create Automated annotations based on these markers. Please see [this article](https://help.imotions.com/lab/analysis/signal-processing-r-notebooks/my-library/annotations.md) for more guides on how to create Automated annotations.

![](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-ec8ae8736697453a00539782162e24d83f385aa7%2Fcc786932fc_image-1698855323983.webp?alt=media)

* **Export blink metrics:** The R-Notebook also produces two types of export files: EyetrackerBlinkMetrics and EyetrackerBlinkTable.

![](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-a64beef332376fcf74cfb94ae610584223fd2361%2F930c7f56b7_image-1698923267314.webp?alt=media)

File **EyetrackerBlinkMetrics:**

File **EyetrackerBlinkMetrics**, for *every* respondent included in the analysis and for *every* stimulus, scene, or annotation present in the study (each row), lists the corresponding number of blinks (Blink count), Blinks Rate and Interblink Interval.

![](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-a46b322ed369ae790cb2be11e2029b17077370c8%2Fb6cb7806a2_image-1698857812853.png?alt=media)

File **EyetrackerBlinkTable:**

File **EyetrackerBlinkTable**, for every detected blink (each row), lists the corresponding Blink number, counting from the start of the recording (**Blink Index**), Blink number, counting from start of the stimulus (**Blink Index by Stimulus**), Start time of the blink (relative to the recording start) in Milliseconds (**Blink Start**), End time of the blink (relative to recording start) in Milliseconds (**Blink End**), Time interval between two subsequent blinks in Milliseconds (**Interblink Interval**), and the corresponding hardware used, Active flows the blink belongs to. If the "Annotations" or "Scenes" were clicked when exporting from iMotions, there will be added columns of Scenes and Annotations indicating whether the blink falls into them or not.

{% hint style="info" %}
If you see your Blink Index does not start at 1, it could be because the first few blinks were detected before your first stimulus included in your analysis, or it could be some blinks were falling in between stimuli so you won't get them in your export. And the counter will restart for each respondent.
{% endhint %}

![](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-f2ffc4f91c3dc990fe8bedb813e3cf4fe8311506%2F471f052d60_image-1698857969203.png?alt=media)

* **Export sensor data:** It is further possible to export the binary blink detection data (**Blink Detected**) as part of the sensor data export file. This is done by enabling the following in the "Sensor Data" Export:

![](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-a50d7fbb86cc390912083de44cd8267c5a9dc0cc%2F5d16248b35_image-1698858161312.webp?alt=media)

Read more: [Export: Sensor data](https://help.imotions.com/lab/exports/export-sensor-data.md)

If you wish to review the R-code:

* **Report:** By clicking into "Report" button you will be able to view a report with the R-code embedded and a step-by-step description of the algorithm.

  ![](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-272f4c9f2f7266f047c0490aec4b570f04430f38%2F049420cbf8_image-1698858325200.webp?alt=media)

  ![](https://286068319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNLNBWkNpljDaAj5rQtNa%2Fuploads%2Fgit-blob-b652b7699b36f51f576572fd56c33daa20d7dd8b%2F2fd05011eb_image-1698858264177.webp?alt=media)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://help.imotions.com/lab/analysis/eye-tracking/other-eye-tracking-analysis/r-notebook-eye-tracking-based-blink-detection.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
