{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# 4. Running a full analysis\n", "\n", "*Written by Jin Hyun Cheong and Eshin Jolly*\n", "\n", "In this tutorial we'll perform a real analysis on part of the open dataset from [\"A Data-Driven Characterisation Of Natural Facial Expressions When Giving Good And Bad News\"](https://journals.plos.org/ploscompbiol/article?id=10.1371/journal.pcbi.1008335) by Watson & Johnston 2020. You can try it out interactively in Google Collab: [](https://colab.research.google.com/github/cosanlab/py-feat/blob/master/notebooks/content/04_fex_analysis.ipynb)\n", "\n", "In the original paper the authors had 3 speakers deliver *good* or *bad* news while filming their facial expressions. They found that could accurately \"decode\" each condition based on participants' facial expressions extracted either using a custom multi-chanel-gradient model or action units (AUs) extracted using [Open Face](https://github.com/TadasBaltrusaitis/OpenFace). \n", "\n", "In this tutorial we'll show how easiy it is to not only reproduce their decoding analysis with py-feat, but just as easily perform additional analyses. Specifically we'll:\n", "\n", "1. Download 20 of the first subject's videos (the full dataset is available on [OSF](https://osf.io/6tbwj/)\n", "2. Extract facial features using the `Detector`\n", "3. Aggregate and summarize detections per video using `Fex`\n", "2. Train and test a decoder to classify *good* vs *bad* news using extracted emotions, AUs, and poses\n", "3. Run a fMRI style \"mass-univariate\" comparison across all AUs between conditions\n", "4. Run a time-series analysis comparing videos based on the time-courses of extracted facial fatures " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Uncomment the line below and run this only if you're using Google Collab\n", "# !pip install -q py-feat" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## 4.1 Download the data\n", "\n", "Here's we'll download and save the first 20 video files and their corresponding attributes from OSF. The next cell should run quickly on Google Collab, but will depend on your own internet conection if you're executing this notebook locally. You can rerun this cell in case the download fails for any reason, as it should skip downloading existing files:" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2021-03-24T18:48:13.620297Z", "start_time": "2021-03-24T18:47:30.513392Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Downloaded 20 videos\n", "Downloaded attributes files with 20 rows\n" ] } ], "source": [ "import os\n", "import subprocess\n", "import numpy as np\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "from glob import glob\n", "import seaborn as sns\n", "from tqdm import tqdm\n", "sns.set_context(\"talk\")\n", "\n", "files_to_download = {\n", " \"4c5mb\": 'clip_attrs.csv',\n", " \"n6rt3\": '001.mp4',\n", " \"3gh8v\": '002.mp4',\n", " \"twqxs\": '003.mp4',\n", " \"nc7d9\": '004.mp4',\n", " \"nrwcm\": '005.mp4',\n", " \"2rk9c\": '006.mp4',\n", " \"mxkzq\": '007.mp4',\n", " \"c2na7\": '008.mp4',\n", " \"wj7zy\": '009.mp4',\n", " \"mxywn\": '010.mp4',\n", " \"6bn3g\": '011.mp4',\n", " \"jkwsp\": '012.mp4',\n", " \"54gtv\": '013.mp4',\n", " \"c3hpm\": '014.mp4',\n", " \"utdqj\": '015.mp4',\n", " \"hpw4a\": '016.mp4',\n", " \"94swe\": '017.mp4',\n", " \"qte5y\": '018.mp4',\n", " \"aykvu\": '019.mp4',\n", " \"3d5ry\": '020.mp4',\n", "}\n", "\n", "for fid, fname in files_to_download.items():\n", " if not os.path.exists(fname):\n", " print(f\"Downloading: {fname}\")\n", " subprocess.run(f\"wget -O {fname} --content-disposition https://osf.io/{fid}/download\".split())\n", "\n", "videos = np.sort(glob(\"*.mp4\"))\n", "\n", "# Load in attributes\n", "clip_attrs = pd.read_csv(\"clip_attrs.csv\")\n", "\n", "# Add in file names and rename conditions\n", "clip_attrs = clip_attrs.assign(\n", " input=clip_attrs.clipN.apply(lambda x: str(x).zfill(3) + \".mp4\"),\n", " condition=clip_attrs[\"class\"].replace({\"gn\": \"goodNews\", \"ists\": \"badNews\"}),\n", ")\n", "\n", "# We're only using a subset of videos for this tutorial so drop the rest\n", "clip_attrs = clip_attrs.query(\"input in @videos\")\n", "\n", "print(f\"Downloaded {len(videos)} videos\")\n", "print(f\"Downloaded attributes files with {clip_attrs.shape[0]} rows\")" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## 4.2 Extract facial features using `Detector`\n", "\n", "Now we'll initialize a new `Detector`, process each frame of each video using `.detect_video()`, and save the results to csv files named after the video." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "ExecuteTime": { "end_time": "2021-03-27T16:52:35.840134Z", "start_time": "2021-03-27T16:52:33.285107Z" }, "scrolled": true }, "outputs": [], "source": [ "from feat import Detector\n", "\n", "# Initialize the default detector\n", "detector = Detector()\n", "\n", "# Loop over and process each video and save results to csv\n", "for video in tqdm(videos):\n", " out_name = video.replace(\".mp4\", \".csv\")\n", " if not os.path.exists(out_name):\n", "\n", " print(f\"Processing: {video}\")\n", "\n", " # This is the line that does detection and auto-saving!\n", " fex = detector.detect(video, data_type=\"video\", save=out_name)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## 4.3. Aggregate detections using a `Fex` dataframe" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Then we can use `read_feat` to load each CSV file and concatenate them together:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2021-03-27T16:53:33.106459Z", "start_time": "2021-03-27T16:53:32.697327Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Unique videos: 20\n", "Total processed frames: 947\n", "Avg frames per video: 47.35\n" ] } ], "source": [ "from feat.utils.io import read_feat\n", "\n", "fex = pd.concat(map(lambda video: read_feat(video.replace(\".mp4\", \".csv\")), videos))\n", "\n", "print(f\"Unique videos: {fex.inputs.nunique()}\")\n", "print(f\"Total processed frames: {fex.shape[0]}\")\n", "print(f\"Avg frames per video: {fex.groupby('input').size().mean()}\")" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Our `Fex` dataframe now contains all detections for all frames of each video" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(947, 174)" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" }, { "data": { "text/html": [ "
| \n", " | FaceRectX | \n", "FaceRectY | \n", "FaceRectWidth | \n", "FaceRectHeight | \n", "FaceScore | \n", "x_0 | \n", "x_1 | \n", "x_2 | \n", "x_3 | \n", "x_4 | \n", "... | \n", "anger | \n", "disgust | \n", "fear | \n", "happiness | \n", "sadness | \n", "surprise | \n", "neutral | \n", "input | \n", "frame | \n", "approx_time | \n", "
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | \n", "364.351590 | \n", "281.705972 | \n", "393.321784 | \n", "538.626011 | \n", "0.999326 | \n", "355.590267 | \n", "361.166635 | \n", "370.024031 | \n", "382.002333 | \n", "405.379242 | \n", "... | \n", "0.000438 | \n", "0.000117 | \n", "0.000483 | \n", "0.981574 | \n", "0.001495 | \n", "0.013396 | \n", "0.002497 | \n", "001.mp4 | \n", "0 | \n", "00:00 | \n", "
| 1 | \n", "364.420098 | \n", "281.698321 | \n", "393.282234 | \n", "538.731126 | \n", "0.999327 | \n", "355.947047 | \n", "361.429636 | \n", "370.189673 | \n", "382.098332 | \n", "405.381871 | \n", "... | \n", "0.000422 | \n", "0.000110 | \n", "0.000471 | \n", "0.981490 | \n", "0.001407 | \n", "0.013663 | \n", "0.002437 | \n", "001.mp4 | \n", "1 | \n", "00:00 | \n", "
| 2 | \n", "366.712348 | \n", "280.100516 | \n", "393.075675 | \n", "538.872332 | \n", "0.999371 | \n", "357.488551 | \n", "363.557662 | \n", "372.905629 | \n", "385.126006 | \n", "408.333512 | \n", "... | \n", "0.000379 | \n", "0.000112 | \n", "0.000329 | \n", "0.988872 | \n", "0.001477 | \n", "0.006877 | \n", "0.001954 | \n", "001.mp4 | \n", "2 | \n", "00:00 | \n", "
| 3 | \n", "369.679081 | \n", "279.800566 | \n", "392.800373 | \n", "541.545800 | \n", "0.999298 | \n", "358.248297 | \n", "364.062520 | \n", "373.173968 | \n", "385.128103 | \n", "407.907217 | \n", "... | \n", "0.000451 | \n", "0.000122 | \n", "0.000229 | \n", "0.989229 | \n", "0.001519 | \n", "0.005104 | \n", "0.003346 | \n", "001.mp4 | \n", "3 | \n", "00:00 | \n", "
| 4 | \n", "368.641343 | \n", "273.729074 | \n", "397.080568 | \n", "550.337162 | \n", "0.999240 | \n", "353.728640 | \n", "360.555682 | \n", "370.127252 | \n", "382.294048 | \n", "405.861680 | \n", "... | \n", "0.000388 | \n", "0.000067 | \n", "0.000447 | \n", "0.976272 | \n", "0.001275 | \n", "0.016979 | \n", "0.004573 | \n", "001.mp4 | \n", "4 | \n", "00:00 | \n", "
5 rows × 174 columns
\n", "| \n", " | mean_FaceRectX | \n", "mean_FaceRectY | \n", "mean_FaceRectWidth | \n", "mean_FaceRectHeight | \n", "mean_FaceScore | \n", "mean_x_0 | \n", "mean_x_1 | \n", "mean_x_2 | \n", "mean_x_3 | \n", "mean_x_4 | \n", "... | \n", "mean_AU28 | \n", "mean_AU43 | \n", "mean_anger | \n", "mean_disgust | \n", "mean_fear | \n", "mean_happiness | \n", "mean_sadness | \n", "mean_surprise | \n", "mean_neutral | \n", "mean_frame | \n", "
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 001.mp4 | \n", "361.693876 | \n", "276.801922 | \n", "390.643836 | \n", "538.546526 | \n", "0.999415 | \n", "351.745947 | \n", "358.669057 | \n", "369.211596 | \n", "382.289621 | \n", "405.340749 | \n", "... | \n", "0.400157 | \n", "0.078763 | \n", "0.000840 | \n", "0.000170 | \n", "0.002119 | \n", "0.955028 | \n", "0.011139 | \n", "0.022887 | \n", "0.007818 | \n", "19.5 | \n", "
| 002.mp4 | \n", "362.782907 | \n", "273.463460 | \n", "381.666967 | \n", "534.798252 | \n", "0.999420 | \n", "351.152403 | \n", "356.936973 | \n", "366.408657 | \n", "378.847147 | \n", "401.092449 | \n", "... | \n", "0.310074 | \n", "0.123018 | \n", "0.000609 | \n", "0.000115 | \n", "0.001898 | \n", "0.879776 | \n", "0.012571 | \n", "0.094248 | \n", "0.010782 | \n", "13.5 | \n", "
| 003.mp4 | \n", "356.546617 | \n", "271.015871 | \n", "388.101834 | \n", "541.721541 | \n", "0.999401 | \n", "342.375212 | \n", "346.903972 | \n", "354.548829 | \n", "365.250301 | \n", "386.696666 | \n", "... | \n", "0.263471 | \n", "0.037146 | \n", "0.000355 | \n", "0.000061 | \n", "0.001733 | \n", "0.707242 | \n", "0.001066 | \n", "0.285287 | \n", "0.004256 | \n", "23.0 | \n", "
| 004.mp4 | \n", "340.658119 | \n", "271.997003 | \n", "390.052859 | \n", "545.905496 | \n", "0.999425 | \n", "328.644936 | \n", "334.260224 | \n", "342.829941 | \n", "354.501717 | \n", "376.853730 | \n", "... | \n", "0.264972 | \n", "0.099364 | \n", "0.000721 | \n", "0.000068 | \n", "0.001341 | \n", "0.847641 | \n", "0.012390 | \n", "0.132939 | \n", "0.004900 | \n", "22.0 | \n", "
| 005.mp4 | \n", "322.252894 | \n", "292.223503 | \n", "393.885578 | \n", "539.983786 | \n", "0.999394 | \n", "312.148711 | \n", "317.922322 | \n", "327.292733 | \n", "339.933222 | \n", "363.364786 | \n", "... | \n", "0.366397 | \n", "0.132650 | \n", "0.000226 | \n", "0.000043 | \n", "0.000490 | \n", "0.986752 | \n", "0.001082 | \n", "0.009684 | \n", "0.001723 | \n", "22.0 | \n", "
| 006.mp4 | \n", "351.829324 | \n", "263.138842 | \n", "383.265317 | \n", "541.386626 | \n", "0.999457 | \n", "339.691768 | \n", "343.841731 | \n", "351.142231 | \n", "361.316873 | \n", "382.081545 | \n", "... | \n", "0.366849 | \n", "0.059786 | \n", "0.000246 | \n", "0.000036 | \n", "0.000628 | \n", "0.941467 | \n", "0.001282 | \n", "0.054525 | \n", "0.001816 | \n", "23.5 | \n", "
| 007.mp4 | \n", "358.451365 | \n", "267.047528 | \n", "385.200341 | \n", "539.094825 | \n", "0.999380 | \n", "345.372773 | \n", "348.641482 | \n", "355.122228 | \n", "364.639966 | \n", "384.731837 | \n", "... | \n", "0.326419 | \n", "0.043416 | \n", "0.000374 | \n", "0.000058 | \n", "0.002243 | \n", "0.933058 | \n", "0.003285 | \n", "0.057281 | \n", "0.003701 | \n", "13.0 | \n", "
| 008.mp4 | \n", "335.531841 | \n", "269.932394 | \n", "389.309414 | \n", "543.007503 | \n", "0.999335 | \n", "322.590086 | \n", "328.698168 | \n", "337.769330 | \n", "348.969558 | \n", "369.878785 | \n", "... | \n", "0.482863 | \n", "0.037955 | \n", "0.000327 | \n", "0.000091 | \n", "0.004953 | \n", "0.592064 | \n", "0.003459 | \n", "0.393644 | \n", "0.005462 | \n", "17.5 | \n", "
| 009.mp4 | \n", "341.377368 | \n", "258.612550 | \n", "384.471775 | \n", "547.748099 | \n", "0.999307 | \n", "328.585905 | \n", "334.560713 | \n", "343.619836 | \n", "355.424001 | \n", "377.394425 | \n", "... | \n", "0.428292 | \n", "0.048874 | \n", "0.000317 | \n", "0.000195 | \n", "0.014244 | \n", "0.536781 | \n", "0.003003 | \n", "0.442032 | \n", "0.003427 | \n", "20.5 | \n", "
| 010.mp4 | \n", "350.612259 | \n", "285.415234 | \n", "382.804314 | \n", "537.392594 | \n", "0.999450 | \n", "336.439669 | \n", "342.711365 | \n", "352.625989 | \n", "365.147914 | \n", "388.132257 | \n", "... | \n", "0.452048 | \n", "0.068882 | \n", "0.000243 | \n", "0.000051 | \n", "0.005075 | \n", "0.691970 | \n", "0.008150 | \n", "0.290069 | \n", "0.004443 | \n", "26.5 | \n", "
| 011.mp4 | \n", "342.846589 | \n", "323.818531 | \n", "380.793417 | \n", "537.407032 | \n", "0.999462 | \n", "317.001702 | \n", "329.615415 | \n", "347.071755 | \n", "367.541387 | \n", "397.147639 | \n", "... | \n", "0.382106 | \n", "0.053013 | \n", "0.130460 | \n", "0.007293 | \n", "0.067473 | \n", "0.134826 | \n", "0.119239 | \n", "0.239828 | \n", "0.300882 | \n", "17.5 | \n", "
| 012.mp4 | \n", "346.793633 | \n", "345.102172 | \n", "376.265186 | \n", "526.341481 | \n", "0.999400 | \n", "321.367646 | \n", "334.862112 | \n", "352.977879 | \n", "374.908646 | \n", "405.053755 | \n", "... | \n", "0.308746 | \n", "0.191442 | \n", "0.002793 | \n", "0.001974 | \n", "0.073120 | \n", "0.066317 | \n", "0.176804 | \n", "0.657999 | \n", "0.020993 | \n", "23.5 | \n", "
| 013.mp4 | \n", "353.966822 | \n", "291.357734 | \n", "381.017808 | \n", "541.329173 | \n", "0.999402 | \n", "341.007234 | \n", "351.553327 | \n", "365.335356 | \n", "382.232456 | \n", "408.826613 | \n", "... | \n", "0.471486 | \n", "0.040775 | \n", "0.001247 | \n", "0.000341 | \n", "0.027139 | \n", "0.025583 | \n", "0.140288 | \n", "0.772149 | \n", "0.033251 | \n", "13.5 | \n", "
| 014.mp4 | \n", "349.458311 | \n", "277.770594 | \n", "378.477618 | \n", "543.834237 | \n", "0.999373 | \n", "326.507088 | \n", "338.703004 | \n", "355.255419 | \n", "375.844798 | \n", "405.824674 | \n", "... | \n", "0.320477 | \n", "0.036352 | \n", "0.004123 | \n", "0.002852 | \n", "0.259469 | \n", "0.000338 | \n", "0.242996 | \n", "0.452750 | \n", "0.037473 | \n", "22.0 | \n", "
| 015.mp4 | \n", "341.266907 | \n", "300.009642 | \n", "376.853981 | \n", "540.635116 | \n", "0.999404 | \n", "318.571982 | \n", "329.832546 | \n", "345.395775 | \n", "364.710552 | \n", "393.250724 | \n", "... | \n", "0.245678 | \n", "0.134054 | \n", "0.001375 | \n", "0.000311 | \n", "0.195097 | \n", "0.003530 | \n", "0.434903 | \n", "0.350142 | \n", "0.014644 | \n", "29.0 | \n", "
| 016.mp4 | \n", "311.328833 | \n", "290.488594 | \n", "383.377662 | \n", "533.601522 | \n", "0.999459 | \n", "297.753369 | \n", "309.231162 | \n", "324.626972 | \n", "343.137091 | \n", "370.771578 | \n", "... | \n", "0.408074 | \n", "0.042457 | \n", "0.002659 | \n", "0.001119 | \n", "0.053669 | \n", "0.005470 | \n", "0.150112 | \n", "0.737356 | \n", "0.049615 | \n", "38.5 | \n", "
| 017.mp4 | \n", "332.626607 | \n", "301.264397 | \n", "381.029439 | \n", "528.518111 | \n", "0.999355 | \n", "323.741976 | \n", "329.358193 | \n", "338.566349 | \n", "351.548930 | \n", "373.243177 | \n", "... | \n", "0.271463 | \n", "0.037381 | \n", "0.000944 | \n", "0.000358 | \n", "0.061095 | \n", "0.006041 | \n", "0.050949 | \n", "0.866098 | \n", "0.014514 | \n", "28.0 | \n", "
| 018.mp4 | \n", "353.881611 | \n", "288.094093 | \n", "379.735909 | \n", "543.683842 | \n", "0.999429 | \n", "328.829019 | \n", "337.406731 | \n", "350.572361 | \n", "368.037250 | \n", "395.964752 | \n", "... | \n", "0.328093 | \n", "0.033050 | \n", "0.007092 | \n", "0.002789 | \n", "0.155484 | \n", "0.012824 | \n", "0.175173 | \n", "0.519115 | \n", "0.127522 | \n", "26.5 | \n", "
| 019.mp4 | \n", "313.341504 | \n", "282.379830 | \n", "391.049851 | \n", "543.151199 | \n", "0.999275 | \n", "303.143011 | \n", "310.439069 | \n", "321.088562 | \n", "334.650816 | \n", "358.007455 | \n", "... | \n", "0.364117 | \n", "0.040580 | \n", "0.000663 | \n", "0.000140 | \n", "0.116914 | \n", "0.000767 | \n", "0.374723 | \n", "0.490313 | \n", "0.016481 | \n", "31.5 | \n", "
| 020.mp4 | \n", "303.021553 | \n", "287.952208 | \n", "385.861488 | \n", "524.438969 | \n", "0.999489 | \n", "293.626183 | \n", "302.517683 | \n", "314.678548 | \n", "329.424593 | \n", "353.108089 | \n", "... | \n", "0.322978 | \n", "0.040657 | \n", "0.000776 | \n", "0.000236 | \n", "0.086137 | \n", "0.014581 | \n", "0.059977 | \n", "0.822615 | \n", "0.015677 | \n", "32.5 | \n", "
20 rows × 172 columns
\n", "