USB 2.0 ポート又はUSB 3.0 ポート (Leap Motion Controller 2 使用時)
本稿のテスト環境は下記のとおりになります。
Ubuntu® 22.04
Raspberry Pi 4 model B
4 GB RAM
3.「Gemini」 のインストール手順
3-1) Raspberry Pi SDKをダウンロードします。 Ultraleap社のWEBサイトから「ultraleap-hand-tracking_v5.17.1-2023.11.16_raspberry-pi-os.tar.gz」をダウンロードします。ダウンロードの際にはUltrleap社のWEBサイトにユーザー登録をする必要があります。
import time
import leap
import sys
from leap import datatypes as ldt
import pygame as pg
pos_x = 0.0
pos_z = 0.0
pinching = False
pg.init()
pgwindow = pg.display.set_mode((300, 150))
font = pg.font.SysFont(None, 50)
#Windowにテキスト出力
def DispText(txt):
text = font.render(txt, True, (255, 255, 255))
pgwindow.blit(text, (40, 30))
#指の位置情報取得
def location_end_of_finger(hand: ldt.Hand, digit_idx: int) -> ldt.Vector:
digit = hand.digits[digit_idx]
return digit.distal.next_joint
#位置情報のマップを作成
def sub_vectors(v1: ldt.Vector, v2: ldt.Vector) -> list:
return map(float.__sub__, v1, v2)
#Pinching動作の判断
def fingers_pinching(thumb: ldt.Vector, index: ldt.Vector):
diff = list(map(abs, sub_vectors(thumb, index)))
if diff[0] < 20 and diff[1] < 20 and diff[2] < 20:
print(“pinching!”)
return True, diff
else:
return False, diff
#Pinching動作を認識する
class PinchingListener(leap.Listener):
def on_tracking_event(self, event):
global pos_x, pos_z, pinching
if event.tracking_frame_id % 30 == 0:
for hand in event.hands:
thumb = location_end_of_finger(hand, 0)
index = location_end_of_finger(hand, 1)
pinching, array = fingers_pinching(thumb, index)
pos_x = list(thumb)[0]
pos_z = list(thumb)[2]
def main():
global pinching, pos_x, pos_z
drum1 = pg.mixer.Sound(“sounds/drum1.mp3”)
drum2 = pg.mixer.Sound(“sounds/drum2.mp3”)
guitar1 = pg.mixer.Sound(“sounds/guitar1.mp3”)
guitar2 = pg.mixer.Sound(“sounds/guitar2.mp3”)
listener = PinchingListener()
connection = leap.Connection()
connection.add_listener(listener)
channel1 = pg.mixer.Channel(0)
channel2 = pg.mixer.Channel(1)
channel3 = pg.mixer.Channel(2)
channel4 = pg.mixer.Channel(3)
with connection.open():
while True:
pgwindow.fill((20, 20, 20))
for event in pg.event.get():
if event.type == pg.KEYDOWN:
if event.key == pg.K_q:
print(“quit the program”)
pg.quit()
sys.exit()
#Pinchingの位置(親指の位置)毎に異なる音を再生する
if pinching:
if pos_x >=0 and pos_z >=0:
channel1.queue(drum1)
DispText(“drum1”)
elif pos_x <0 and pos_z >=0:
channel2.queue(drum2)
DispText(“drum2”)
elif pos_x >=0 and pos_z <0:
channel3.queue(guitar1)
DispText(“guitar1”)
elif pos_x <0 and pos_z <0:
channel4.queue(guitar2)
DispText(“guitar2”)
pg.display.flip()
if __name__ == “__main__”:
main()
6. おわりに
本稿では最近公開されたUltraleap社のRaspberry Pi用SDKとPython-bindingを用いて、手の動作を認識し、音を再生するデモの作成についてご紹介しました。上記のサンプルコードのような動作以外にも、手の位置座標や移動速度、方向などの情報を用いて多様な応用プログラムを作成することができます。Leap MotionはRaspberry Pi 環境で動作できるようになることによって応用分野が広がると期待されています。
この記事の監修者
コーンズテクノロジー編集部
コーンズテクノロジーでは先進的な製品・技術を日本産業界へ紹介する技術専門商社として、通信計測・自動車・防衛セキュリティ・電子機器装置・航空宇宙・産業機械といった技術分野のお役立ち情報を紹介しています。