Directional Motion Detection and Tracking using Webcam in TouchDesigner #touchdesigner #tutorial

Function Store
16 Nov 202215:41

Summary

TLDRIn this video, Dan demonstrates how to use optical flow with a webcam to detect movement direction, creating job channels for triggering events based on left, right, up, and down movements. This technique is ideal for interactive installations or controlling visuals without the need for sensors. Dan guides viewers through the process, from setting up the optical flow to refining the output for a clean direction signal, and suggests adjusting parameters based on lighting conditions. The tutorial also promotes Dan's Patreon for additional resources and components.

Takeaways

  • 🎥 Dan is demonstrating how to use a webcam with optical flow to track movement for triggering events in a design software.
  • 👐 The process does not require special sensors, making it accessible for those without them.
  • 🔍 Optical flow is used to determine the direction of movement for each pixel in the webcam's incoming texture.
  • 📏 The script explains how to isolate the left-right movement by focusing on the red channel and using a reorder node.
  • ↔️ A threshold node is used to separate the movement into positive (right) and negative (left) directions.
  • 🔢 Analyze nodes are utilized to count the number of pixels moving in each direction, which is then summed up.
  • 📉 To reduce noise, a trail node is introduced to smooth out the data, making the movement detection more reliable.
  • 📊 A logic chop is used to define bounds that determine when the movement is considered significant enough to trigger an action.
  • 🔄 The final direction is determined by comparing the count of positive and negative pixels, with a sine function used to simplify the output to -1 or 1.
  • 🛠️ The setup can be adjusted with parameters such as the optical flow threshold and bounds to optimize performance under different lighting conditions.
  • 🔄 The same method can be applied to the green channel to detect up and down movements, with the final output being a combination of both directional movements.

Q & A

  • What is the main purpose of the component being built in the video?

    -The main purpose of the component is to detect movement direction using optical flow from a webcam input and generate job channels that can trigger events based on the movement direction.

  • What is Optical Flow and how does it help in this context?

    -Optical Flow is a method that determines the direction of movement for each pixel of an incoming texture. It helps by providing the direction of movement for the objects within the webcam's view, which can be used to trigger events or control visuals.

  • How can the optical flow component be accessed in the video?

    -The optical flow component can be accessed by going to the tools palette, pressing Ctrl+B, and selecting 'Optical Flow' from the list.

  • What does the Optical Flow component provide in terms of movement detection?

    -The Optical Flow component provides the direction of movement for each pixel, encoded in red and green channels, where red indicates left-right movement and green indicates up-down movement.

  • How does the video script describe the process of isolating movement direction?

    -The script describes isolating the movement direction by using a reorder node to focus on the red channel for left-right movement, applying a threshold to separate negative and positive pixel values indicating direction, and then analyzing the count of these pixels to determine the overall movement direction.

  • What is the purpose of the threshold node in the script?

    -The threshold node is used to filter out pixels based on their movement direction, separating the pixels that indicate movement to the left (negative values) from those indicating movement to the right (positive values).

  • How are the counts of positive and negative pixels used to determine the overall movement direction?

    -The counts of positive and negative pixels are converted to chop data and then subtracted from each other to get a value that indicates the overall movement direction, with positive values suggesting rightward movement and negative values suggesting leftward movement.

  • What is the role of the trail node in the script?

    -The trail node is used to smooth out the data and reduce noise, making the movement direction signal more reliable and easier to interpret.

  • How can the final direction signal be cleaned up for a more reliable output?

    -The final direction signal can be cleaned up by using a logic chop to define bounds that filter out noise, and by applying a blur node to smooth the data before processing.

  • What is the significance of the sine function in determining the final direction?

    -The sine function is used to convert the direction signal into a simple -1 or 1 value, where -1 represents leftward movement and 1 represents rightward movement, simplifying the output for use in controlling visuals or triggering events.

  • How can the optical flow threshold be adjusted to improve the component's performance?

    -The optical flow threshold can be adjusted to exclude minor movements or to focus on more significant movement, helping to reduce noise and improve the reliability of the direction signal.

  • What is the suggested method for controlling the circle's movement in the video?

    -The circle's movement is controlled by using a speed drop with a limited range between -0.5 and 0.5, and by offsetting the direction signal to move the circle left or right based on the detected movement.

  • How can the component's parameters be customized for different lighting conditions?

    -The parameters that may need to be adjusted for different lighting conditions include the optical flow threshold, the bounds for noise filtering, and potentially the lambda value in the blur node, although its specific function is not detailed in the script.

Outlines

00:00

🎥 Introduction to Optical Flow for Movement Detection

Dan introduces a project using optical flow technology to detect movement direction from a webcam feed without any special sensors. The goal is to create a component that outputs job channels based on movement direction, which can be utilized for triggering events in interactive installations or controlling visuals. He mentions that a pre-configured component with parameters is available on his Patreon, where he is more active than on YouTube. The process begins with setting up an NDI input for the webcam image and applying optical flow to determine the direction of movement for each pixel.

05:01

🔍 Isolating and Analyzing Movement Directions

The video script explains the process of isolating the red channel from the optical flow data to focus on left and right movements. A threshold is applied to separate negative and positive pixel values, representing movement directions. The analyze drop operation is used to count these pixels, and the results are converted to a chop format. The process involves subtracting the counts to get a net direction value, which is then filtered and trailed for a smoother output. The aim is to simplify the noisy data into a clean left-right direction signal.

10:05

📉 Refining the Direction Signal and Controlling Visuals

The script continues with refining the direction signal by using a logic chop to define bounds for noise and a blur for data smoothing. The sine function is applied to convert the direction value into a clean -1 or 1 output, representing left and right movements respectively. The process is repeated for the green channel to detect up and down movements, and the results are merged to create a comprehensive direction control. The optical flow threshold can be adjusted for different lighting conditions to improve reliability, and the final component can be used to control visuals, demonstrated by moving a circle in response to hand movements.

15:08

👍 Conclusion and Call to Action

In conclusion, the video demonstrates how to use optical flow to detect and control movement directions from a webcam feed. The script encourages viewers to subscribe for more content and to check out Patreon for additional resources and pre-configured parameters for the optical flow component. The video wraps up with a reminder of the practical applications of this technology in installations and the importance of adjusting parameters based on environmental conditions.

Mindmap

Keywords

💡Optical Flow

Optical flow is a computer vision technique that calculates the apparent motion of objects between successive frames in a video. It's crucial in this video as it provides the direction of movement for each pixel of the incoming texture, allowing the detection of movement direction without the need for physical sensors. The script describes using optical flow to detect if the hand is moving left or right, and up or down.

💡NDI

NDI stands for Network Device Interface, a protocol for real-time video over IP networks. In the context of the video, NDI is used to bring the webcam image into the system because OBS (Open Broadcaster Software) is also utilizing it. It's a key component for capturing the webcam feed for the optical flow analysis.

💡Threshold

In the video, a threshold is used to isolate movement in one direction by selecting pixels that are moving in a certain way, either less than or equal to zero for leftward movement or greater than or equal to zero for rightward movement. It's a method to filter out unwanted data and focus on the movement of interest.

💡Directional Isolation

Directional isolation refers to the process of separating the movement data into different directions. In the script, the presenter uses the reorder node to isolate the red channel, which contains left and right movement information. This isolation is essential for determining the general direction of movement from the image.

💡Analyze

The term 'analyze' in the script is related to counting or summing up the pixels that meet certain criteria after applying the threshold. This is done to determine the quantity of movement in a particular direction, which is then used to infer the overall direction of movement.

💡Chop

In the video, 'chop' is used to extract a single channel from the data, simplifying the information to be processed. It's a method to reduce the complexity of the data and focus on the essential part needed for determining movement direction.

💡Logic Chop

A logic chop is used to convert input values into binary on/off states based on defined bounds. In the script, it's used to determine when the movement is significant enough to be considered rather than just noise, helping to clean up the signal for more reliable detection of movement direction.

💡Sine Function

The sine function is used in the video to convert the raw movement data into a simple -1 or 1 value, representing left or right movement, respectively. It's a mathematical function that helps in normalizing the data to a more understandable format for controlling visual elements.

💡Denoising

Denoising in the context of the video refers to the process of reducing noise or unwanted fluctuations in the data to produce a cleaner, more reliable signal. The script mentions using a blur node and adjusting bounds to achieve this, which is essential for accurate movement detection.

💡Telekinesis

While not a technical term in computer vision, 'telekinesis' is used metaphorically in the script to describe the effect of controlling visual elements, like a circle, with hand movements detected by the webcam. It illustrates the interactive potential of the setup described in the video.

💡Patreon

Patreon is a membership platform that allows creators to receive financial support from their audience in exchange for exclusive content and benefits. In the script, the creator mentions Patreon as a place where viewers can find the component with all relevant parameters mapped out, as well as other useful content related to the video's theme.

Highlights

Introduction to a method for detecting movement direction using optical flow without special sensors.

Utility in triggering events based on movement direction for interactive installations.

Explanation of using basic left, right, up, and down directions for control.

Invitation to Patreon for the component with parameters mapped out and additional resources.

Setup begins with a clean slate and an NDI input for webcam image.

Optical flow is introduced to determine the movement direction of each pixel.

Demonstration of viewing pixel values to understand movement direction.

Isolating directional movement by focusing on the red channel for left and right movement.

Use of threshold to separate movement into negative and positive directions.

Counting pixels to determine the predominance of movement direction.

Conversion of pixel counts to a single channel for clarity.

Subtraction of pixel counts to get a general direction of movement.

Introduction of a trail effect for smoothing the movement data.

Recap of the process for obtaining general movement direction from pixel data.

Refinement of the signal by setting bounds to determine when movement is significant.

Inversion of the signal for a more intuitive understanding of left and right movement.

Use of sine function to convert movement data into a clean -1 or 1 value.

Adjustment of the optical flow threshold to reduce noise in the signal.

Application of the method to control a visual element, like a circle, in real-time.

Advice on adjusting parameters based on lighting conditions for optimal results.

Mention of Patreon for pre-configured parameters and additional resources.

Conclusion and invitation to subscribe for more content, with a reminder of Patreon offerings.

Transcripts

play00:00

hey everyone this is Dan today we're

play00:03

gonna move anything into designer

play00:06

using on our webcam we're not going to

play00:08

use anything special other than Optical

play00:10

flow but in the end we're gonna get some

play00:13

job channels out of this component that

play00:16

we're gonna build

play00:19

um this is very useful if you don't have

play00:20

any sensors at hand and you want to

play00:23

trigger events just based on the

play00:26

movement direction for example if you

play00:29

wanted to do something with your visuals

play00:31

you are interactive installation only

play00:34

when people were walking to the left or

play00:37

to the right you could do stuff or as

play00:40

you can see you can just control

play00:42

anything with these basic left right up

play00:46

and down

play00:47

directions

play00:49

if you would like to skip the video and

play00:51

just start using this component

play00:54

with all the relevant parameters mapped

play00:56

out for you

play00:58

and then you can find it on my patreon

play01:01

along with a lot of other useful stuff I

play01:04

tend to be a bit more active there than

play01:06

here on YouTube so consider checking it

play01:09

out

play01:11

alrighty let's start from a clean slate

play01:13

I'm gonna put down a ndi in because

play01:16

that's where I have my webcam image

play01:17

coming from

play01:19

because OBS is also using it where I am

play01:23

and we need an optical flow from the

play01:27

palette so player is Ctrl B

play01:30

and there under the tools you will find

play01:33

Optical flow

play01:35

and the optical flow

play01:37

basically gives us

play01:39

the direction of movement for each pixel

play01:42

of the incoming texture

play01:44

so if I try to

play01:47

pause it here

play01:51

um I'm moving my hands I guess from

play01:53

right to left

play01:55

and let's look at the pixel values by

play01:57

activating this viewer and hitting d

play02:01

so if you look at some of these pixels I

play02:03

use you can see the direct channel is in

play02:07

the negative

play02:08

meaning I was moving my hand from right

play02:12

to left I guess or the other way around

play02:14

we will see you soon

play02:16

and we also have some green values

play02:18

because I guess

play02:20

I'm moving to my hands a bit

play02:22

up and down also when I'm moving it to

play02:25

the left and right but basically yeah

play02:28

this Optical flow is giving us these red

play02:31

and green channels indirect channels we

play02:34

have the left and right movement encoded

play02:36

and the green Channel we have the up and

play02:38

down encoded for each pixel and we're

play02:42

gonna use all this data to get a general

play02:44

direction

play02:45

uh from the incoming image

play02:50

let's start with only the left and right

play02:53

movement so that means we need to

play02:56

isolate directional

play02:57

I'm gonna do that with the reorder

play03:00

I'm gonna output zero on the green and

play03:03

zero on the blues and get input one red

play03:08

so now we have all the uh red Channel

play03:11

stuff here which Maybe

play03:13

we can actually see now

play03:15

but it's there it's only only red left

play03:18

and right information

play03:20

so we have positive and negative pixels

play03:22

also depending on the direction so this

play03:25

is where we're gonna mainly uh separate

play03:28

the

play03:30

the data so I'm going to put down a

play03:33

threshold

play03:35

threshold top

play03:38

and in the threshold top we're gonna

play03:40

select less or equal

play03:43

and less or equal then zero so we get

play03:47

Only One Direction not the other

play03:51

and we're gonna do the the same but

play03:55

instead of less or equal we're gonna go

play03:57

to greater or equal

play04:01

so in these two structures we have now

play04:05

if I'm moving my hand

play04:07

to the left we have more pixels here if

play04:12

I'm moving it to the right we have more

play04:14

pixels here because these are the I

play04:17

guess uh negative values and these are

play04:19

going to be

play04:20

our positive values

play04:23

uh next up we're gonna count these

play04:26

pixels so I'm gonna put down an analyze

play04:29

drop

play04:30

and operation uh

play04:33

not count but let's sum them up

play04:37

um

play04:38

and it's going to give us this blinking

play04:41

something

play04:42

uh it's

play04:44

yeah it's not pretty but it's contains

play04:47

the information that we need

play04:50

I'm gonna put down here now we can do

play04:52

that without n

play04:55

like so

play04:58

and convert both of these

play05:01

to a chop

play05:06

like that

play05:08

that's only

play05:10

take one channel out of these

play05:13

great

play05:16

so now if we in principle

play05:20

subtract these two from each other

play05:24

we will get a number which is either

play05:27

positive or negative depending on the

play05:30

direction

play05:32

so if it combine shops subtract

play05:37

let's maybe filter this a bit because

play05:39

it's jumping quite a lot

play05:42

and let's put down a trail also

play05:45

so that we can see what's going on

play05:49

so I'm moving my hand to the left

play05:52

jumped up moving my hand to the right it

play05:55

jumps down

play05:58

severe

play06:00

heading in the right direction

play06:05

seems to be quite reliable

play06:10

like so

play06:12

so to quickly recap what we did here

play06:15

we have the optical flow giving us for

play06:17

each pixel whether that pixel is moving

play06:21

to the left or to the right or not the

play06:22

pixel but the the thing contained by the

play06:25

pixels are moving to the left or to the

play06:28

right but we have this data for each

play06:30

pixel and what we want in the end is

play06:34

just simple job data of the general

play06:37

direction

play06:38

of things moving on the camera so we

play06:43

isolated everything with the reorder to

play06:46

the red Channel meaning on it to the

play06:48

left and the right because it's only

play06:50

also contains up and down in the green

play06:52

and we're gonna also do that in the

play06:55

other Branch when we are done with this

play06:57

so we have only the red channel here and

play07:00

the threshold we can

play07:02

isolate only the negative

play07:04

pixels and only the positive pixels

play07:08

and with the analyze we count how many

play07:11

positive pixels there are and how many

play07:13

negative pixels there are

play07:15

we convert everything to chop now

play07:19

and if you subtract these two numbers

play07:23

um we can get if there are more negative

play07:25

or more positive numbers

play07:27

and this number here is gonna give us

play07:31

how many more

play07:32

uh positive or negative numbers there

play07:36

are

play07:37

in our Optical flow

play07:40

so this is cool but what I want in the

play07:43

end is just

play07:45

one value if things are moving to the

play07:48

left or to the right I don't want this

play07:51

kind of noisy data I want something

play07:53

clean

play07:55

so I'm gonna rename these channels or

play07:59

this channel

play08:00

to left right

play08:04

and we have to we have to draw a line uh

play08:08

where we say that okay this is now

play08:11

just noise or

play08:14

yeah we have to somehow

play08:18

contain this data

play08:21

a bit better so I'm going to put down a

play08:23

logic chop

play08:24

and convert input on

play08:28

off one outside bounds

play08:30

and these bounds I'm gonna

play08:34

I'm gonna Define

play08:36

as minus uh let's see

play08:42

so now even though I'm not really moving

play08:45

it's still jumping quite a lot

play08:47

and we can put a blur before everything

play08:50

kind of denies

play08:53

are

play08:55

our data I think yeah that already looks

play08:59

much less noisy

play09:02

and let's look at some values

play09:04

and move my hand to the left it jumps up

play09:07

to 2000 ish

play09:10

other direction also

play09:13

so let's say that when this value

play09:17

reaches

play09:19

uh minus 500 or 500

play09:26

that's when it should be on so this is

play09:31

not inverted so I'm just gonna

play09:34

uh Channel pre-op invert

play09:40

and now

play09:43

we're getting a bit more reliable signal

play09:49

like that

play09:51

and to get just a direction I can put

play09:56

down a function drop

play09:59

and use the sine function science sign

play10:04

so this is gonna give us one if it's

play10:07

positive minus one if it's negative

play10:11

and if we now

play10:15

multiply these two together

play10:19

combat jobs multiply

play10:22

we're gonna get

play10:24

minus one and one minus one one

play10:30

and I might want this to

play10:33

be the other way around so I can

play10:36

just here

play10:37

multiply this by minus one

play10:40

to me it makes more sense for left to be

play10:42

-1 and right to be positive

play10:45

I don't know why

play10:47

we can do some more denoising maybe

play10:52

but

play10:54

yeah this could be all right we can also

play10:56

change

play10:57

the optical flow threshold

play10:59

that can also help sometimes

play11:02

to make this a bit less noisy

play11:07

but I feel like

play11:09

this is quite okay

play11:13

yeah

play11:16

cool

play11:18

I like this

play11:20

so we can do the same kind of deal

play11:24

for the green Channel and I'm just gonna

play11:27

lazily copy and paste all this

play11:31

we don't need this Trail anymore

play11:34

and instead of getting directional here

play11:37

we're gonna get red zero and green

play11:40

Green from input one

play11:44

and in our

play11:47

yeah we can keep it like this it doesn't

play11:49

matter if it's red to the top two

play11:52

now in principle

play11:56

they have it up and down also

play11:59

I'm gonna rename this to

play12:03

down up

play12:05

I want down to be minus one

play12:09

up to V1

play12:14

like so

play12:17

if you look at this

play12:20

and we can just merge these two together

play12:25

put down and now

play12:29

select all these collapse it the

play12:32

component

play12:34

and let's use this data to control

play12:37

something

play12:39

let's do this circle

play12:42

let's move the circle

play12:46

so we have the circle I'm going to

play12:48

increase its resolution

play12:50

a little bit

play12:52

decrease the radius

play12:56

and

play12:58

let's use a speed drop

play13:06

that's limited between minus 0.5 and 0.5

play13:13

let's look at what happens if we

play13:18

offset this

play13:22

going to the right

play13:25

left right it's a bit

play13:29

yeah it's kind of like

play13:32

telekinesis it's not

play13:36

fully reliable but

play13:39

I've used this to my advantage and

play13:42

in an installation recently

play13:45

where we didn't have any

play13:47

connect or any sensor just webcam

play13:52

so the values that you have to change

play13:55

based on the lighting conditions because

play13:57

this is just taking the the camera image

play14:00

so it's they can just slide information

play14:03

so depending on the lighting conditions

play14:06

you have to change

play14:09

the bounds potentially

play14:13

or

play14:14

the threshold of the optical flow I

play14:18

don't recommend changing any of the

play14:20

other parameters here maybe the Lambda I

play14:22

don't really know what it does but yeah

play14:25

I just leave it this way

play14:27

so the threshold if you want to

play14:30

threshold

play14:32

you know some movement you want to

play14:35

exclude when it's just a little bit of a

play14:38

movement you can increase the threshold

play14:41

but yeah based on the lighting

play14:43

conditions you might you may wanna

play14:45

change the bounds

play14:48

and also you can play with the filter

play14:51

to give you some better results Maybe

play14:55

uh in the patreon file you will find all

play14:59

the relevant parameters that you might

play15:03

want to mess with already mapped for you

play15:07

I'm not gonna spend time on that in this

play15:10

video

play15:12

if you like the content please consider

play15:14

subscribing and again please check the

play15:17

patreon there's lots of uh cool stuff

play15:20

there

play15:21

much more than here on YouTube so yeah

play15:24

see you around and thanks for watching

Rate This

5.0 / 5 (0 votes)

Related Tags
Optical FlowWebcam TrackingInteractive ArtDirection DetectionPixel AnalysisThresholdingMotion ControlVisual EffectsEvent TriggeringDIY Tech