質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.47%
OpenCV

OpenCV(オープンソースコンピュータービジョン)は、1999年にインテルが開発・公開したオープンソースのコンピュータビジョン向けのクロスプラットフォームライブラリです。

Python 2.7

Python 2.7は2.xシリーズでは最後のメジャーバージョンです。Python3.1にある機能の多くが含まれています。

Leap Motion

Leap Motionは、Leap Motionによって開発、販売している、手のジェスチャーでパソコンを操作できるデバイスです。

Q&A

0回答

900閲覧

pythonでのLeapMotionとOpenCVの同時使用

huton

総合スコア30

OpenCV

OpenCV(オープンソースコンピュータービジョン)は、1999年にインテルが開発・公開したオープンソースのコンピュータビジョン向けのクロスプラットフォームライブラリです。

Python 2.7

Python 2.7は2.xシリーズでは最後のメジャーバージョンです。Python3.1にある機能の多くが含まれています。

Leap Motion

Leap Motionは、Leap Motionによって開発、販売している、手のジェスチャーでパソコンを操作できるデバイスです。

0グッド

0クリップ

投稿2020/11/29 13:53

編集2020/12/02 05:04

質問内容

python2.7に対応させたLeap MotionとOpenCVを用いたカメラ映像を一緒に使い、LeapMotionの上に手をかざしたときに取得したカメラ映像の画面内に文字をプリントする処理を行うプログラムを作りたいと考えています。
そのためのプログラムをLeapMotionをダウンロードしたときに付いてきたSample.pyとこちらのサイト内にあるcapture.pyを参考に自分なりにプログラムを考えてみたのですがleapmotionの処理が終わらないとにOpenCVの処理が始まらないようになってしまいます。どのようにしたらOpencvでカメラ画像を写しながらLeapMotionの処理を行うようにできるのでしょうか。

ソースコード

Sample.py

python

1import Leap, sys,thread, time 2 3 4class SampleListener(Leap.Listener): 5 finger_names = ['Thumb', 'Index', 'Middle', 'Ring', 'Pinky'] 6 bone_names = ['Metacarpal', 'Proximal', 'Intermediate', 'Distal'] 7 8 def on_init(self, controller): 9 print ("Initialized") 10 11 def on_connect(self, controller): 12 print ("Connected") 13 14 def on_disconnect(self, controller): 15 # Note: not dispatched when running in a debugger. 16 print ("Disconnected") 17 18 def on_exit(self, controller): 19 print ("Exited") 20 21 def on_frame(self, controller): 22 # Get the most recent frame and report some basic information 23 frame = controller.frame() 24 25 print ("Frame id: %d, timestamp: %d, hands: %d, fingers: %d" % ( 26 frame.id, frame.timestamp, len(frame.hands), len(frame.fingers))) 27 28 # Get hands 29 for hand in frame.hands: 30 31 handType = "Left hand" if hand.is_left else "Right hand" 32 33 print (" %s, id %d, position: %s" % ( 34 handType, hand.id, hand.palm_position)) 35 36 # Get the hand's normal vector and direction 37 normal = hand.palm_normal 38 direction = hand.direction 39 40 # Calculate the hand's pitch, roll, and yaw angles 41 print (" pitch: %f degrees, roll: %f degrees, yaw: %f degrees" % ( 42 direction.pitch * Leap.RAD_TO_DEG, 43 normal.roll * Leap.RAD_TO_DEG, 44 direction.yaw * Leap.RAD_TO_DEG)) 45 46 # Get arm bone 47 arm = hand.arm 48 print (" Arm direction: %s, wrist position: %s, elbow position: %s" % ( 49 arm.direction, 50 arm.wrist_position, 51 arm.elbow_position)) 52 53 # Get fingers 54 for finger in hand.fingers: 55 56 print (" %s finger, id: %d, length: %fmm, width: %fmm" % ( 57 self.finger_names[finger.type], 58 finger.id, 59 finger.length, 60 finger.width)) 61 62 # Get bones 63 for b in range(0, 4): 64 bone = finger.bone(b) 65 print (" Bone: %s, start: %s, end: %s, direction: %s" % ( 66 self.bone_names[bone.type], 67 bone.prev_joint, 68 bone.next_joint, 69 bone.direction)) 70 71 if not frame.hands.is_empty: 72 print ("") 73 74def main(): 75 # Create a sample listener and controller 76 listener = SampleListener() 77 controller = Leap.Controller() 78 79 # Have the sample listener receive events from the controller 80 controller.add_listener(listener) 81 82 # Keep this process running until Enter is pressed 83 print ("Press Enter to quit...") 84 try: 85 sys.stdin.readline() 86 except KeyboardInterrupt: 87 pass 88 finally: 89 # Remove the sample listener when done 90 controller.remove_listener(listener) 91 92if __name__ == "__main__": 93 main()

自作プログラム ka.py

python

1import cv2 2import Leap, sys, time 3 4cap = cv2.VideoCapture(0) 5 6class SampleListener(Leap.Listener): 7 finger_names = ['Thumb', 'Index', 'Middle', 'Ring', 'Pinky'] 8 bone_names = ['Metacarpal', 'Proximal', 'Intermediate', 'Distal'] 9 10 def on_init(self, controller): 11 print ("Initialized") 12 13 def on_connect(self, controller): 14 print ("Connected") 15 16 def on_disconnect(self, controller): 17 # Note: not dispatched when running in a debugger. 18 print ("Disconnected") 19 20 def on_exit(self, controller): 21 print ("Exited") 22 23 def on_frame(self, controller): 24 # Get the most recent frame and report some basic information 25 frame = controller.frame() 26 27 # Get hands 28 for hand in frame.hands: 29 30 handType = "Left hand" if hand.is_left else "Right hand" 31 32 cv2.putText(frame,"hand", (10,80), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255,155,0), 1, cv2.LINE_AA) 33 34 35def main(): 36 # Create a sample listener and controller 37 listener = SampleListener() 38 controller = Leap.Controller() 39 40 # Have the sample listener receive events from the controller 41 controller.add_listener(listener) 42 43 # Keep this process running until Enter is pressed 44 print ("Press Enter to quit...") 45 try: 46 sys.stdin.readline() 47 except KeyboardInterrupt: 48 pass 49 finally: 50 # Remove the sample listener when done 51 controller.remove_listener(listener) 52 53while(cap.isOpened()): 54 # フレームを取得 55 ret, frame = cap.read() 56 57 #LeapMotionに手をかざしたときの処理 58 if __name__ == "__main__": 59 main() 60 61 # フレームを表示 62 cv2.imshow("Flame", frame) 63 64 # qキーが押されたら途中終了 65 if cv2.waitKey(1) & 0xFF == ord('q'): 66 break 67 68cap.release() 69cv2.destroyAllWindows()

実行結果

OpenCVの関数であるputTextを使っているので当然なのですが、手をかざすと以下のようなエラーメッセージが繰り返されます。

python

1Traceback (most recent call last): 2 File "ka.py", line 33, in on_frame 3 cv2.putText(frame,"hand",(10,88),cv2.FONT_HERSHEY_SIMPLEX, 0.7,(255,155,0),1,cv2.LINEAA) 4TypeError;img is not a numpy array,neither a scalar

この処理の終了後にOpenCVの処理が始まるようになります。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.47%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問