> 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/facial-expression-analysis/affdex/r-notebook-facial-expression-analysis-based-blink-detection.md).

# R-Notebook: Facial expression analysis-based blink detection

Run the AFFDEX-based blink detection R notebook to detect blinks from facial expression analysis data and get blink counts and blink rates per stimulus.

*This Help Center article provides information about the facial expression analysis-based blink detection R-Notebook; it is only available for Affectiva AFFDEX.*

‘Blinking’, the rapid closing of the eyelids, is a semi-autonomous bodily function. It serves, in the first place, the moisturizing of the eyes by spreading the tears and lubricants the eyes secrete across the surface of the eyeball. However, blinking has also been associated with psychological processes, including attention processes.

The facial expression analysis (FEA)-based blink detection R-Notebook detects and reports eye blinks based on the ‘eye closure’ metric provided by Affectiva AFFDEX.

*In this help center article:*

* [How to add the FEA-based blink detection R-Notebook to your analysis](#how-to-add-the-fea-based-blink-detection-r-notebook-to-your-analysis)
* [Details about the algorithm](#details-about-the-algorithm)
* [Parameters](#parameters)
* [What the R-Notebook delivers: replay, export files, report](#what-the-r-notebook-delivers-replay-export-files-report)

## How to add the FEA-based blink detection R-Notebook to your analysis

The FEA-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.

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

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

## 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>" %}

## Details about the algorithm

***If you have collected data using Affectiva's AFFDEX SDK 5.1, released February 2022:***

For studies collected with Affectiva's AFFDEX SDK 5.1, blink statistics are *not* computed by iMotions. Therefore, the parameters described below won't be applicable to data collected from Affectiva's AFFDEX SDK 5.1. AFFDEX SDK 5.1 provides "Blink" and "BlinkRate" directly.

Instead, the blink signal provided by Affdex is used and made available in the blink metrics export file.

To review the blink data, please refer to the blink signals from the Affdex algorithm.

Refer to [*this article*](https://help.imotions.com/lab/analysis/facial-expression-analysis/affdex/affectiva-affdex-signals-overview.md) to know more about changes in AFFDEX SDK 5.1.

***If you have collected data using Affectiva's AFFDEX SDK 4.0:***

The algorithm is based on the ‘eye closure’ metric provided by Affectiva AFFDEX. This metric expresses the likelihood of the eyes being closed on a scale from 0 to 100; this could be roughly understood as the *probability* that the eyes are closed.

The algorithm then simply tags blinks if the eye closure values exceed a given probability threshold (see parameter: probability threshold). Specifically, a blink is tagged from the moment that the eye closure values first exceed the threshold until the eye closure values drop below the threshold again.

These tags are then used to count the number of blinks and to compute the blink rate (i.e., number of blinks per minute) and interblink intervals.

## Parameters

The sensitivity of the blink detection R-Notebook can be altered by changing the following two 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 sample-above-threshold of the first blink to the first sample-above-threshold of the next blink).
* **AFFDEX threshold for eye closure:** The threshold used for detecting a blink in the ‘eye closure’ data

The defaults are set to:

* **Minimum duration \[ms] needed between two blinks to be considered as separated:** 1 millisecond
* **AFFDEX threshold for eye closure:** 0.5

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

This probability threshold of 0.5 (which means 0.5%, not 50%) seems low, but was chosen because it rendered the most optimal results during our in-house testing. It combines a maximal sensitivity of the blink detection with a negligible amount of false positives.

Also, note that the 'minimum duration between two blinks' is set to 1 millisecond. This means that a single data sample below the threshold is sufficient to separate two consecutive eye blinks. It often happens that respondents make two blinks in fast succession; in that case, the 'eye closure' values sometimes do not even drop below the threshold in between the two blinks - or only very briefly. Setting the 'minimum duration between two blinks' to 1 millisecond maximally separates these double-blinks (i.e., it counts double-blinks as two blinks instead of one). However, by increasing this value, you can choose not to separate those double-blinks (i.e., always count double-blinks as one).

For that reason, the algorithm will not always be able to count these blinks as separate blinks; the algorithm counts them as a single blink.

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

The FEA-based blink detection R-Notebook performs 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’ (AFFDEX SDK 4.0 only) contains a binary signal that can be used for visualizing the blinks on the timeline (**Blink Detected**) and another binary signal that can be used for visualizing the maximum value of the "Eye Closure" metrics of each blink on the timeline (**Blink Occurrences**). 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-5e0dc51b00d26203d4359878fd3fba3d14cc5f32%2F8292df92c7_image-1698922438246.webp?alt=media)

(Data using Affectiva's AFFDEX SDK 4.0)

***If you have collected data using Affectiva's AFFDEX SDK 5.1:***

You will only have the "Blink Detection Metrics" displayed. You can expand the "Blink" and "BlinkRate" metrics provided by AFFDEX SDK 5.1 to visualize where the blinks are detected.

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

(Data using Affectiva's AFFDEX SDK 5.1)

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 FEA Blink Detection R Notebook are composed of "FEA Blink Start" (AFFDEX SDK 4.0 only), "FEA Blink" and "FEA Blink End" (AFFDEX SDK 4.0 only). 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-68083ea54c8592ab6b337b714f5fc02c13510bf7%2Fc92a285ea9_image-1698922900787.webp?alt=media)

(Data using Affectiva's AFFDEX SDK 4.0)

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

(Data using Affectiva's AFFDEX SDK 5.1)

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

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

File **FEABlinkMetrics:**

File **FEABlinkMetrics**, 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-8c209a00f1a1a6e0fee12357c3f907858f9aa2b3%2Ff97f959568_image-1698923381213.png?alt=media)

File **FEABlinkTable:**

File **FEABlinkTable**, for every detected blink (each row), lists the corresponding Blink number, counting from 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**) (AFFDEX SDK 4 only), Time of the blink when the maximum value of the "FEA" signal was reached (relative to recording start) (**Blink Time**), End time of the blink (relative to recording start) in Milliseconds (**Blink End**)(AFFDEX SDK 4 only), 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.

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

(Data using Affectiva's AFFDEX SDK 4.0)

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

(Data using Affectiva's AFFDEX SDK 5.1)

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

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

(Data using Affectiva's AFFDEX SDK 4.0)

Read more: Export: Sensor data

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-f07b13936e3f6a9acd3e58c867cf0ae84fc2e879%2F3e86461fd2_image-1698924082852.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/facial-expression-analysis/affdex/r-notebook-facial-expression-analysis-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.
