0
2

More than 1 year has passed since last update.

TouchDesignerで人物認識(人数)してみた。(mac)

Posted at

以下のページの通り、Mac Book Pro 2017で試した結果、
認識は一応できましたが、途中までしかうまくいかなかったので報告します。
(OpenCVのコードがなぜかエラーをはいて、四角形や文字を描いてくれなかったです。)

0. 上の記事通り、Pythonの仮想環境を作って、TouchDesignerにパスを通してください。

1. OP Excuteを作成し、以下を記載。

以下を、DATの、OP Excuteに記載します。
ssdlite_mobilenetv2.onnxのファイルパスのところだけ自分の環境に合わせて変えてください。

# me - this DAT.
# changeOp - the operator that has changed
#
# Make sure the corresponding toggle is enabled in the OP Execute DAT.

import cv2
import numpy as np
import onnxruntime

# 上のテストと一緒なので省略します(使うときは上からコピーして使ってください)
coco_classes = {
    1: 'person',
    2: 'bicycle',
    3: 'car',
    4: 'motorcycle',
    5: 'airplane',
    6: 'bus',
    7: 'train',
    8: 'truck',
    9: 'boat',
    10: 'traffic light',
    11: 'fire hydrant',
    12: 'stop sign',
    13: 'parking meter',
    14: 'bench',
    15: 'bird',
    16: 'cat',
    17: 'dog',
    18: 'horse',
    19: 'sheep',
    20: 'cow',
    21: 'elephant',
    22: 'bear',
    23: 'zebra',
    24: 'giraffe',
    25: 'backpack',
    26: 'umbrella',
    27: 'handbag',
    28: 'tie',
    29: 'suitcase',
    30: 'frisbee',
    31: 'skis',
    32: 'snowboard',
    33: 'sports ball',
    34: 'kite',
    35: 'baseball bat',
    36: 'baseball glove',
    37: 'skateboard',
    38: 'surfboard',
    39: 'tennis racket',
    40: 'bottle',
    41:'wine glass',
    42: 'cup',
    43: 'fork',
    44: 'knife',
    45: 'spoon',
    46: 'bowl',
    47: 'banana',
    48: 'apple',
    49: 'sandwich',
    50: 'orange',
    51: 'broccoli',
    52: 'carrot',
    53: 'hot dog',
    54: 'pizza',
    55: 'donut',
    56: 'cake',
    57: 'chair',
    58: 'couch',
    59: 'potted plant',
    60: 'bed',
    61: 'dining table',
    62: 'toilet',
    63: 'tv',
    64: 'laptop',
    65: 'mouse',
    66: 'remote',
    67: 'keyboard',
    68: 'cell phone',
    69: 'microwave',
    70: 'oven',
    71: 'toaster',
    72: 'sink',
    73: 'refrigerator',
    74: 'book',
    75: 'clock',
    76: 'vase',
    77: 'scissors',
    78: 'teddy bear',
    79: 'hair drier',
    80: 'toothbrush'
}

# ここは自分のpc内のssdlite_mobilenetv2.onnxファイルへのパスを書いてください!
session = onnxruntime.InferenceSession("~~~~~~/ssdlite_mobilenetv2.onnx")

def onPreCook(changeOp):
    return

def onPostCook(changeOp):
    # 画像の読み込み
    frame = changeOp.numpyArray(delayed=True)
    arr = frame[:, :, 0:3]
    arr = arr * 255
    arr = arr.astype(np.uint8)
    arr = np.flipud(arr)
    width, height = arr.shape[0:2]
    image_data = np.expand_dims(arr, axis=0)

    # モデルの推論の準備
    input_name = session.get_inputs()[0].name   # 'image'
    output_name_boxes = session.get_outputs()[0].name     # 'boxes'
    output_name_classes = session.get_outputs()[1].name   # 'classes'
    output_name_scores = session.get_outputs()[2].name    # 'scores'
    output_name_num = session.get_outputs()[3].name       # 'number of detections'

    # 推論
    outputs_index = session.run([output_name_num, output_name_boxes,
                                output_name_scores, output_name_classes],
                                {input_name: image_data})

    # 結果を受け取る
    output_num = outputs_index[0] # 検出した物体数
    output_boxes = outputs_index[1] # 検出した物体の場所を示すボックス
    output_scores = outputs_index[2] # 検出した物体の予測確率
    output_classes = outputs_index[3] # 検出した物体のクラス番号

    # 検出したものの中で人だけのリスト
    op('constant1').par.value0 = np.count_nonzero(outputs_index[3][0][0:int(outputs_index[0][0])] == 1)

def onDestroy():
    return

def onFlagChange(changeOp, flag):
    return

def onWireChange(changeOp):
    return

def onNameChange(changeOp):
    return

def onPathChange(changeOp):
    return

def onUIChange(changeOp):
    return

def onNumChildrenChange(changeOp):
    return

def onChildRename(changeOp):
    return

def onCurrentChildChange(changeOp):
    return

def onExtensionChange(changeOp, extension):
    return

2. TOPのvideodeviceinを作成。OP ExcuteのMonitor OPsにvideodeviceinを記載。

image.png

3. CHOPのConstantを作成。

constantの名前を、「constant1」に。
これで、constant1の値は、カメラで認識された人の人物数になります。

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2