1

だから私はリープモーションSDKを学び、4〜5年でそれに触れなかった後、Pythonを再学習しようとしています.Python 2.7のジェネレーターで問題が発生しています.

基本的に私は単語のリストを持っており、リープモーションが新しい「円」ジェスチャーを拾うたびにリストの次の単語を出力したいと考えています。私が見ているのは、on_frameコールバックが発生するたびに、リストの最初の単語だけが出力されるということです。起こっていることは、Python ランタイムがイベント間のジェネレーターの状態を忘れていることだと思います。ジェスチャ イベント間でジェネレータの状態を維持する方法はありますか?

if not frame.hands.is_empty:
        for gesture in frame.gestures():
            if gesture.type == Leap.Gesture.TYPE_CIRCLE:
                circle = CircleGesture(gesture)
                # Determine clock direction using the angle between the pointable and the circle normal
                action = None
                if circle.pointable.direction.angle_to(circle.normal) <= Leap.PI/4:
                    action = GestureActions.clockwiseCircleGesture()
                else:
                    action = GestureActions.counterClockwiseCircleGesture()

                print action.next()

  def clockwiseCircleGesture():
       words = ["You", "spin", "me", "right", "round", "baby", "right", "round", "like", "a", "record", "baby",                          "Right", "round", "round", "round", "You", "spin", "me", "right", "round", "baby", "Right", "round", "like", "a",
         "record", "baby", "Right", "round", "round", "round"]
      for word in words:
          yield word

これについての洞察は素晴らしいでしょう。ありがとう

4

2 に答える 2

2

Python には詳しくありませんが、ジェネレーター、イテレーター、列挙子などを学習している場合、これはよくある問題です。ループを通過するたびにイテレータを再作成し、状態を失います。

代わりに、それぞれを最大 1 回作成します

clockwise = GestureActions.clockwiseCircleGesture()
counter_clockwise = GestureActions.counterClockwiseCircleGesture()
# then

action = clockwise if foo else counter_clockwise

action.next()
于 2013-12-09T02:01:27.313 に答える
2

actionイベントが発生するたびに変数がリセットされている と思われます。

イベント処理関数の外でジェネレーターを初期化します。clockwise_actionとの 2 つが必要なようですcounterclockwise_action

于 2013-12-09T01:57:31.027 に答える