FER(Face Expression Recognition)というモジュールを使うと、いとも簡単に表情認識できました。
以下、環境構築手順と簡単なサンプルの共有です。
サンプル:https://github.com/thinpedelica/fer-sample
アドカレの記事用✍️ pic.twitter.com/FeFw7BmMRp
— Shampagne (@Shampedelica) November 30, 2025
環境
- os: win11
- TouchDesiner : 2023.11600 (python 3.11.1, 古くてスミマセン)
- PCローカルのpython : 3.11.1
ザックリ手順
- TouchDesignerと同じバージョンのpythonをPCにインストール
- PCのpython環境でFERをインストール
- FERのインストール先をTouchDesignerのPythonパスに指定する
TouchDesignerと同じバージョンのpythonをPCにインストール
TDのバージョンによりPythonのバージョンが異なるので、ちゃんと確認します。
TD上でAlt+tでTextPortを開くとPythonのバージョンが先頭に表示されているはずです。
PCへのPythonのインストールは、自分はpyenvが好きです。
複数バージョンを利用する可能性がある人はぜひご利用ください。
PCのpython環境でFERをインストール
pip install fer
FERのインストール先をTouchDesignerのPythonパスに指定する
TouchDesignerにローカルPCのPythonモジュールのパスを通す方法はいくつかあるので、比嘉先生の記事を再掲します。
-> 外部ライブラリのインストール
「方法2: モジュール探索パスの設定」についてですが、sys.path.append()よりもsys.path.insert(0, NEW_path) のほうが堅いと先生から教わったので、ここに書き残しておきます。
※TouchDesigner2025では tdPyenvManager という便利そうなものもでているみたいです。
https://zenn.dev/aadebdeb/articles/touchdesigner-tdpyenvmanager
サンプルコード
上手く環境構築できていれば、以下のようなスクリプトで表情毎の認識結果が得られるようになります。
from fer import FER
import cv2
import numpy
detector = FER(mtcnn=True)
fer_result = op('fer_result')
def onCook(scriptOp):
input_image = scriptOp.inputs[0].numpyArray()
image = input_image * 255
image = image.astype('uint8')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# image coordinate
# opencv's : top/left
# TouchDesigner : bottom/left
image = cv2.flip(image, 0)
result = detector.detect_emotions(image)
if result:
emotions = result[0]['emotions']
# print(f"neutral: {emotions['neutral']}, angry: {emotions['angry']}")
# print(emotions)
fer_result.par.value0 = emotions['angry']
fer_result.par.value1 = emotions['disgust']
fer_result.par.value2 = emotions['fear']
fer_result.par.value3 = emotions['happy']
fer_result.par.value4 = emotions['sad']
fer_result.par.value5 = emotions['surprise']
fer_result.par.value6 = emotions['neutral']
return
おしまい