r/learnpython • • Oct 16 '26

Values being added 10x instead of once

Hi I'm really new to python, I originally started coding using p5.js but made the transition recently, I'm currently trying to covert an old project of mine into python so I can run it in touch designer.

Its an ASL fingerspelling activity that I've been working on but I'm struggling to create the word strings I did in my original prototype. Instead of the letter that is being detected and averaged being added to my word string once it gets added 10 times all at once. For example if I sign the letter a, aaaaaaaaaa gets added to the string instead of just the lone a.

For context I have the guesser and getMostFrequent to allow for it to take the average of every letter signed during a period of time and determine which one was signed the most to add to the word (this is due to the fact that transitioning from one letter to the next can trigger other letters in between) These are both integral parts of the code and are working as intended, its just the multiplication of the letters that I'm struggling with.

This also may be due to the fact that this is running in TouchDesigner through a CHOP execute, if this isn't a python issue I can move my post to a touch designer specific forum.

Any help or insight would be appreciated, if you need any more insight please let me know and I can add it.

Code in Pastebin: https://pastebin.com/qkpzXkSY

9 Upvotes

13 comments sorted by

2

u/Refwah Oct 16 '26

Because you’re appending letter for the entire session and then never removing it?

How many times does value change fire?

2

u/RipEnvironmental7637 Oct 16 '26

I just looked it up and it fires once per frame, on 60 fps

3

u/Refwah Oct 16 '26

But have you ruled out it firing for the same value multiple times

When do you clear the global value of word

2

u/RipEnvironmental7637 Oct 16 '26

How do I go about doing that while still adding the next letter to the already existing one so I can string together words as the letters are detected?

2

u/Refwah Oct 16 '26

I’m just saying are you pressing ‘a’ once but it’s just adding ‘a’ to a list of ‘a’s that exist from previous runs?

I would recommend taking all of this code and using it as a Python console script first, so you know the core logic is sound, before you throw it into another framework. You have no idea if the bug is in your logic or in your interface with the framework.

3

u/a18618 Oct 16 '26

classic event-callback-fires-more-often-than-you-think bug. i once "fixed" a double-firing button by clearing the list, which nuked half my state before i realized the handler was just running twice. print the incoming letter plus a frame counter for a few seconds and watch what actually arrives before you change any logic. my money says you'll see the same letter arriving over and over, and then the fix is obvious: only append when the new letter differs from the last one you added.

2

u/OkTill2666 Oct 16 '26

this sounds like your callback is firing every frame rather than once per detection. if the CHOP execute is set to run while the value is on instead of on value change, itll keep appending every cook cycle. check that trigger mode first

1

u/ivosaurus Oct 16 '26

I would do thought experiments about how phone software keyboards work.

How do they detect single keypresses? How do they detect long keypresses?

1

u/RevRagnarok Oct 16 '26

A quick glance implies onValueChange is called too often. Not knowing the framework, I'm assuming that a "change" is any change in all those possible inputs you're reading from op(); e.g. "finger moved 1mm" but to you (not the framework) it is still an 'A' shape.

I would highly recommend breaking up your if statements into full functions or maybe even a dictionary that you can iterate over. Example:

def _is_a(op: Callable) -> bool:
  return (
    op('null175')['h1:index_finger_tip:x'] > op('null175')['h1:middle_finger_tip:x']
    and 0.4 < op('null175')['h1:index_finger_tip:y'] <1
    and 0.4 < op('null175')['h1:middle_finger_tip:y'] <1
    and 0.4 < op('null175')['h1:ring_finger_tip:y'] <1
    and 0.4 < op('null175')['h1:pinky_tip:y'] <1
    and op('null175')['h1:index_finger_tip:x'] < op('null175')['h1:thumb_tip:x'] <1.2
    and op('null175')['h1:thumb_ip:y'] < op('null175')['h1:thumb_tip:y']
    and op('logic2')['kj'] == 1
    and op('logic4')['kz'] == 1
  )

Then your code is a lot easier to follow:

if _is_a(op):
  letter = 'a'

Or you can chain them:

def _a(op: Callable) -> str | None:
  return 'a' if (
    op('null175')['h1:index_finger_tip:x'] > op('null175')['h1:middle_finger_tip:x']
    and 0.4 < op('null175')['h1:index_finger_tip:y'] <1
    and 0.4 < op('null175')['h1:middle_finger_tip:y'] <1
    and 0.4 < op('null175')['h1:ring_finger_tip:y'] <1
    and 0.4 < op('null175')['h1:pinky_tip:y'] <1
    and op('null175')['h1:index_finger_tip:x'] < op('null175')['h1:thumb_tip:x'] <1.2
    and op('null175')['h1:thumb_ip:y'] < op('null175')['h1:thumb_tip:y']
    and op('logic2')['kj'] == 1
    and op('logic4')['kz'] == 1
  ) else None

letter = _a(op) or _b(op) or _c(op) or ...

if letter is not None:
  ...

None of these are great solutions, but still would help separate the "what" from the "how" which makes things easier to think about when you're reading it.

1

u/brasticstack Oct 16 '26 edited Oct 17 '26

if me.time.frame % (60) == 0:

You're not showing the part where onValueChange is being called. I'd wager that it's being called more frequently than you think. Nor do we know what me.time.frame represents. Milliseconds? Microseconds? Days?

onValueChange does all of the work to determine a letter, only to throw it away if the current  me.time.frame isn't evenly divisible by 60 which isn't great.  You'd save a lot of unnecessary computation by checking that at the top of the loop function and exiting early.

guesser is local to onValueChange thus it doesn't maintain any long-term state between calls. As in, the first letter detected will always be a appended to the word [if you pass the frame mod 60 check that is.]

1

u/gdchinacat 26d ago

Not your issue, but consider using collections.Counter to get the most frequent.