質問編集履歴
1
自分なりに進めていたら変更点があった
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
python
|
1
|
+
pythonでのLeapMotionとOpenCVの同時使用
|
test
CHANGED
@@ -2,9 +2,9 @@
|
|
2
2
|
|
3
3
|
---
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
そのためのプログラムをLeapMotionをダウンロードしたときに付いてきたSample.pyと[こちらのサイト](https://qiita.com/opto-line/items/7ade854c26a50a485159)内にある`capture.py`を参考に
|
5
|
+
python2.7に対応させたLeap MotionとOpenCVを用いたカメラ映像を一緒に使い、LeapMotionの上に手をかざしたときに取得したカメラ映像の画面内に文字をプリントする処理を行うプログラムを作りたいと考えています。
|
6
|
+
|
7
|
+
そのためのプログラムをLeapMotionをダウンロードしたときに付いてきたSample.pyと[こちらのサイト](https://qiita.com/opto-line/items/7ade854c26a50a485159)内にある`capture.py`を参考に自分なりにプログラムを考えてみたのですがleapmotionの処理が終わらないとにOpenCVの処理が始まらないようになってしまいます。どのようにしたらOpencvでカメラ画像を写しながらLeapMotionの処理を行うようにできるのでしょうか。
|
8
8
|
|
9
9
|
|
10
10
|
|
@@ -16,356 +16,354 @@
|
|
16
16
|
|
17
17
|
```python
|
18
18
|
|
19
|
+
import Leap, sys,thread, time
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
class SampleListener(Leap.Listener):
|
26
|
+
|
27
|
+
finger_names = ['Thumb', 'Index', 'Middle', 'Ring', 'Pinky']
|
28
|
+
|
29
|
+
bone_names = ['Metacarpal', 'Proximal', 'Intermediate', 'Distal']
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
def on_init(self, controller):
|
34
|
+
|
35
|
+
print ("Initialized")
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
def on_connect(self, controller):
|
40
|
+
|
41
|
+
print ("Connected")
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
def on_disconnect(self, controller):
|
46
|
+
|
47
|
+
# Note: not dispatched when running in a debugger.
|
48
|
+
|
49
|
+
print ("Disconnected")
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
def on_exit(self, controller):
|
54
|
+
|
55
|
+
print ("Exited")
|
56
|
+
|
57
|
+
|
58
|
+
|
59
|
+
def on_frame(self, controller):
|
60
|
+
|
61
|
+
# Get the most recent frame and report some basic information
|
62
|
+
|
63
|
+
frame = controller.frame()
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
print ("Frame id: %d, timestamp: %d, hands: %d, fingers: %d" % (
|
68
|
+
|
69
|
+
frame.id, frame.timestamp, len(frame.hands), len(frame.fingers)))
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
# Get hands
|
74
|
+
|
75
|
+
for hand in frame.hands:
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
handType = "Left hand" if hand.is_left else "Right hand"
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
print (" %s, id %d, position: %s" % (
|
84
|
+
|
85
|
+
handType, hand.id, hand.palm_position))
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
# Get the hand's normal vector and direction
|
90
|
+
|
91
|
+
normal = hand.palm_normal
|
92
|
+
|
93
|
+
direction = hand.direction
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
# Calculate the hand's pitch, roll, and yaw angles
|
98
|
+
|
99
|
+
print (" pitch: %f degrees, roll: %f degrees, yaw: %f degrees" % (
|
100
|
+
|
101
|
+
direction.pitch * Leap.RAD_TO_DEG,
|
102
|
+
|
103
|
+
normal.roll * Leap.RAD_TO_DEG,
|
104
|
+
|
105
|
+
direction.yaw * Leap.RAD_TO_DEG))
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
# Get arm bone
|
110
|
+
|
111
|
+
arm = hand.arm
|
112
|
+
|
113
|
+
print (" Arm direction: %s, wrist position: %s, elbow position: %s" % (
|
114
|
+
|
115
|
+
arm.direction,
|
116
|
+
|
117
|
+
arm.wrist_position,
|
118
|
+
|
119
|
+
arm.elbow_position))
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
# Get fingers
|
124
|
+
|
125
|
+
for finger in hand.fingers:
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
print (" %s finger, id: %d, length: %fmm, width: %fmm" % (
|
130
|
+
|
131
|
+
self.finger_names[finger.type],
|
132
|
+
|
133
|
+
finger.id,
|
134
|
+
|
135
|
+
finger.length,
|
136
|
+
|
137
|
+
finger.width))
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
# Get bones
|
142
|
+
|
143
|
+
for b in range(0, 4):
|
144
|
+
|
145
|
+
bone = finger.bone(b)
|
146
|
+
|
147
|
+
print (" Bone: %s, start: %s, end: %s, direction: %s" % (
|
148
|
+
|
149
|
+
self.bone_names[bone.type],
|
150
|
+
|
151
|
+
bone.prev_joint,
|
152
|
+
|
153
|
+
bone.next_joint,
|
154
|
+
|
155
|
+
bone.direction))
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
if not frame.hands.is_empty:
|
160
|
+
|
161
|
+
print ("")
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
def main():
|
166
|
+
|
167
|
+
# Create a sample listener and controller
|
168
|
+
|
169
|
+
listener = SampleListener()
|
170
|
+
|
171
|
+
controller = Leap.Controller()
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
# Have the sample listener receive events from the controller
|
176
|
+
|
177
|
+
controller.add_listener(listener)
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
# Keep this process running until Enter is pressed
|
182
|
+
|
183
|
+
print ("Press Enter to quit...")
|
184
|
+
|
185
|
+
try:
|
186
|
+
|
187
|
+
sys.stdin.readline()
|
188
|
+
|
189
|
+
except KeyboardInterrupt:
|
190
|
+
|
191
|
+
pass
|
192
|
+
|
193
|
+
finally:
|
194
|
+
|
195
|
+
# Remove the sample listener when done
|
196
|
+
|
197
|
+
controller.remove_listener(listener)
|
198
|
+
|
199
|
+
|
200
|
+
|
201
|
+
if __name__ == "__main__":
|
202
|
+
|
203
|
+
main()
|
204
|
+
|
205
|
+
```
|
206
|
+
|
207
|
+
自作プログラム ka.py
|
208
|
+
|
209
|
+
```python
|
210
|
+
|
211
|
+
import cv2
|
212
|
+
|
19
213
|
import Leap, sys, time
|
20
214
|
|
21
215
|
|
22
216
|
|
217
|
+
cap = cv2.VideoCapture(0)
|
218
|
+
|
23
219
|
|
24
220
|
|
25
221
|
class SampleListener(Leap.Listener):
|
26
222
|
|
27
|
-
finger_names = ['Thumb', 'Index', 'Middle', 'Ring', 'Pinky']
|
28
|
-
|
29
|
-
bone_names = ['Metacarpal', 'Proximal', 'Intermediate', 'Distal']
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
def on_init(self, controller):
|
34
|
-
|
35
|
-
print ("Initialized")
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
def on_connect(self, controller):
|
40
|
-
|
41
|
-
print ("Connected")
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
def on_disconnect(self, controller):
|
46
|
-
|
47
|
-
# Note: not dispatched when running in a debugger.
|
48
|
-
|
49
|
-
print ("Disconnected")
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
def on_exit(self, controller):
|
54
|
-
|
55
|
-
print ("Exited")
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
def on_frame(self, controller):
|
60
|
-
|
61
|
-
# Get the most recent frame and report some basic information
|
62
|
-
|
63
|
-
frame = controller.frame()
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
print (" %s, id %d, position: %s" % (
|
84
|
-
|
85
|
-
handType, hand.id, hand.palm_position))
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
# Get the hand's normal vector and direction
|
90
|
-
|
91
|
-
normal = hand.palm_normal
|
92
|
-
|
93
|
-
direction = hand.direction
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
# Calculate the hand's pitch, roll, and yaw angles
|
98
|
-
|
99
|
-
print (" pitch: %f degrees, roll: %f degrees, yaw: %f degrees" % (
|
100
|
-
|
101
|
-
direction.pitch * Leap.RAD_TO_DEG,
|
102
|
-
|
103
|
-
normal.roll * Leap.RAD_TO_DEG,
|
104
|
-
|
105
|
-
direction.yaw * Leap.RAD_TO_DEG))
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
# Get arm bone
|
110
|
-
|
111
|
-
arm = hand.arm
|
112
|
-
|
113
|
-
print (" Arm direction: %s, wrist position: %s, elbow position: %s" % (
|
114
|
-
|
115
|
-
arm.direction,
|
116
|
-
|
117
|
-
arm.wrist_position,
|
118
|
-
|
119
|
-
arm.elbow_position))
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
# Get fingers
|
124
|
-
|
125
|
-
for finger in hand.fingers:
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
print (" %s finger, id: %d, length: %fmm, width: %fmm" % (
|
130
|
-
|
131
|
-
self.finger_names[finger.type],
|
132
|
-
|
133
|
-
finger.id,
|
134
|
-
|
135
|
-
finger.length,
|
136
|
-
|
137
|
-
finger.width))
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
# Get bones
|
142
|
-
|
143
|
-
for b in range(0, 4):
|
144
|
-
|
145
|
-
bone = finger.bone(b)
|
146
|
-
|
147
|
-
print (" Bone: %s, start: %s, end: %s, direction: %s" % (
|
148
|
-
|
149
|
-
self.bone_names[bone.type],
|
150
|
-
|
151
|
-
bone.prev_joint,
|
152
|
-
|
153
|
-
bone.next_joint,
|
154
|
-
|
155
|
-
bone.direction))
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
if not frame.hands.is_empty:
|
160
|
-
|
161
|
-
print ("")
|
223
|
+
finger_names = ['Thumb', 'Index', 'Middle', 'Ring', 'Pinky']
|
224
|
+
|
225
|
+
bone_names = ['Metacarpal', 'Proximal', 'Intermediate', 'Distal']
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
def on_init(self, controller):
|
230
|
+
|
231
|
+
print ("Initialized")
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
def on_connect(self, controller):
|
236
|
+
|
237
|
+
print ("Connected")
|
238
|
+
|
239
|
+
|
240
|
+
|
241
|
+
def on_disconnect(self, controller):
|
242
|
+
|
243
|
+
# Note: not dispatched when running in a debugger.
|
244
|
+
|
245
|
+
print ("Disconnected")
|
246
|
+
|
247
|
+
|
248
|
+
|
249
|
+
def on_exit(self, controller):
|
250
|
+
|
251
|
+
print ("Exited")
|
252
|
+
|
253
|
+
|
254
|
+
|
255
|
+
def on_frame(self, controller):
|
256
|
+
|
257
|
+
# Get the most recent frame and report some basic information
|
258
|
+
|
259
|
+
frame = controller.frame()
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
# Get hands
|
264
|
+
|
265
|
+
for hand in frame.hands:
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
handType = "Left hand" if hand.is_left else "Right hand"
|
270
|
+
|
271
|
+
|
272
|
+
|
273
|
+
cv2.putText(frame,"hand", (10,80), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255,155,0), 1, cv2.LINE_AA)
|
274
|
+
|
275
|
+
|
162
276
|
|
163
277
|
|
164
278
|
|
165
279
|
def main():
|
166
280
|
|
167
|
-
# Create a sample listener and controller
|
281
|
+
# Create a sample listener and controller
|
168
|
-
|
282
|
+
|
169
|
-
listener = SampleListener()
|
283
|
+
listener = SampleListener()
|
170
|
-
|
284
|
+
|
171
|
-
controller = Leap.Controller()
|
285
|
+
controller = Leap.Controller()
|
172
|
-
|
173
|
-
|
174
|
-
|
286
|
+
|
287
|
+
|
288
|
+
|
175
|
-
# Have the sample listener receive events from the controller
|
289
|
+
# Have the sample listener receive events from the controller
|
176
|
-
|
290
|
+
|
177
|
-
controller.add_listener(listener)
|
291
|
+
controller.add_listener(listener)
|
178
|
-
|
179
|
-
|
180
|
-
|
292
|
+
|
293
|
+
|
294
|
+
|
181
|
-
# Keep this process running until Enter is pressed
|
295
|
+
# Keep this process running until Enter is pressed
|
182
|
-
|
296
|
+
|
183
|
-
print ("Press Enter to quit...")
|
297
|
+
print ("Press Enter to quit...")
|
184
|
-
|
298
|
+
|
185
|
-
try:
|
299
|
+
try:
|
186
|
-
|
300
|
+
|
187
|
-
sys.stdin.readline()
|
301
|
+
sys.stdin.readline()
|
188
|
-
|
302
|
+
|
189
|
-
except KeyboardInterrupt:
|
303
|
+
except KeyboardInterrupt:
|
190
|
-
|
304
|
+
|
191
|
-
pass
|
305
|
+
pass
|
192
|
-
|
306
|
+
|
193
|
-
finally:
|
307
|
+
finally:
|
194
|
-
|
308
|
+
|
195
|
-
# Remove the sample listener when done
|
309
|
+
# Remove the sample listener when done
|
196
|
-
|
310
|
+
|
197
|
-
controller.remove_listener(listener)
|
311
|
+
controller.remove_listener(listener)
|
312
|
+
|
313
|
+
|
314
|
+
|
198
|
-
|
315
|
+
while(cap.isOpened()):
|
316
|
+
|
199
|
-
|
317
|
+
# フレームを取得
|
318
|
+
|
200
|
-
|
319
|
+
ret, frame = cap.read()
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
#LeapMotionに手をかざしたときの処理
|
324
|
+
|
201
|
-
if __name__ == "__main__":
|
325
|
+
if __name__ == "__main__":
|
202
|
-
|
326
|
+
|
203
|
-
main()
|
327
|
+
main()
|
328
|
+
|
329
|
+
|
330
|
+
|
331
|
+
# フレームを表示
|
332
|
+
|
333
|
+
cv2.imshow("Flame", frame)
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
# qキーが押されたら途中終了
|
338
|
+
|
339
|
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
340
|
+
|
341
|
+
break
|
342
|
+
|
343
|
+
|
344
|
+
|
345
|
+
cap.release()
|
346
|
+
|
347
|
+
cv2.destroyAllWindows()
|
204
348
|
|
205
349
|
```
|
206
350
|
|
207
|
-
|
351
|
+
実行結果
|
352
|
+
|
353
|
+
---
|
354
|
+
|
355
|
+
OpenCVの関数であるputTextを使っているので当然なのですが、手をかざすと以下のようなエラーメッセージが繰り返されます。
|
208
356
|
|
209
357
|
```python
|
210
358
|
|
211
|
-
import cv2
|
212
|
-
|
213
|
-
import Leap, sys, time
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
cap = cv2.VideoCapture(0)
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
c
|
359
|
+
Traceback (most recent call last):
|
222
|
-
|
223
|
-
|
360
|
+
|
224
|
-
|
225
|
-
bone_names = ['Metacarpal', 'Proximal', 'Intermediate', 'Distal']
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
def on_init(self, controller):
|
230
|
-
|
231
|
-
print ("Initialized")
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
def on_connect(self, controller):
|
236
|
-
|
237
|
-
print ("Connected")
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
def on_disconnect(self, controller):
|
242
|
-
|
243
|
-
|
361
|
+
File "ka.py", line 33, in on_frame
|
244
|
-
|
245
|
-
|
362
|
+
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
def on_exit(self, controller):
|
250
|
-
|
251
|
-
print ("Exited")
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
def on_frame(self, controller):
|
256
|
-
|
257
|
-
# Get the most recent frame and report some basic information
|
258
|
-
|
259
|
-
frame = controller.frame()
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
# Get hands
|
264
|
-
|
265
|
-
for hand in frame.hands:
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
handType = "Left hand" if hand.is_left else "Right hand"
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
363
|
+
cv2.putText(frame,"hand",(10,88),cv2.FONT_HERSHEY_SIMPLEX, 0.7,(255,155,0),1,cv2.LINEAA)
|
274
|
-
|
275
|
-
|
276
|
-
|
277
|
-
|
278
|
-
|
279
|
-
|
364
|
+
|
280
|
-
|
281
|
-
# Create a sample listener and controller
|
282
|
-
|
283
|
-
listener = SampleListener()
|
284
|
-
|
285
|
-
controller = Leap.Controller()
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
# Have the sample listener receive events from the controller
|
290
|
-
|
291
|
-
controller.add_listener(listener)
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
365
|
+
TypeError;img is not a numpy array,neither a scalar
|
296
|
-
|
297
|
-
print ("Press Enter to quit...")
|
298
|
-
|
299
|
-
try:
|
300
|
-
|
301
|
-
sys.stdin.readline()
|
302
|
-
|
303
|
-
except KeyboardInterrupt:
|
304
|
-
|
305
|
-
pass
|
306
|
-
|
307
|
-
finally:
|
308
|
-
|
309
|
-
# Remove the sample listener when done
|
310
|
-
|
311
|
-
controller.remove_listener(listener)
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
while(cap.isOpened()):
|
316
|
-
|
317
|
-
# フレームを取得
|
318
|
-
|
319
|
-
ret, frame = cap.read()
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
#LeapMotionに手をかざしたときの処理
|
324
|
-
|
325
|
-
if __name__ == "__main__":
|
326
|
-
|
327
|
-
main()
|
328
|
-
|
329
|
-
|
330
|
-
|
331
|
-
# フレームを表示
|
332
|
-
|
333
|
-
cv2.imshow("Flame", frame)
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
|
339
|
-
# qキーが押されたら途中終了
|
340
|
-
|
341
|
-
if cv2.waitKey(1) & 0xFF == ord('q'):
|
342
|
-
|
343
|
-
break
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
cap.release()
|
348
|
-
|
349
|
-
cv2.destroyAllWindows()
|
350
366
|
|
351
367
|
```
|
352
368
|
|
353
|
-
実行結果
|
354
|
-
|
355
|
-
---
|
356
|
-
|
357
|
-
|
369
|
+
この処理の終了後にOpenCVの処理が始まるようになります。
|
358
|
-
|
359
|
-
```python
|
360
|
-
|
361
|
-
Traceback (most recent call last):
|
362
|
-
|
363
|
-
File "ka.py", line 33, in on_frame
|
364
|
-
|
365
|
-
cv2.putText(frame,"hand",(10,88),cv2.FONT_HERSHEY_SIMPLEX, 0.7,(255,155,0),1,cv2.LINEAA)
|
366
|
-
|
367
|
-
TypeError;img is not a numpy array,neither a scalar
|
368
|
-
|
369
|
-
```
|
370
|
-
|
371
|
-
この処理を Ctrl + c で終わらせるとカメラ映像が映し出されるのですがすぐに固まって応答なしとなります。
|