Help with GetFiles

373 views
Skip to first unread message

Bruno Cruz

unread,
Mar 11, 2017, 2:22:04 PM3/11/17
to Bonsai Users
Hi everyone, 

I have never used the GetFiles node before and i am having a bit of trouble getting the script to iterate over all videos in a folder. The idea of the script is going into a folder, and per Video do some simple analysis of pixel diff per frame. These frame values are then saved onto a CSV file with the same name of the original. The analysis part works, however the script only runs the first video and saves the first CSV then it finishes (although the Path Strings are all there). Could someone point me on the right direction? Thanks!

Also, on the same topic, how can i can the FileCaptureNode to read as fast as possible and not at the actual video speed?

Bruno
takeVideoDerivative.bonsai

adam....@gmail.com

unread,
Mar 11, 2017, 2:28:06 PM3/11/17
to Bruno Cruz, Bonsai Users

Put a “concat” after getfiles (or a selectmany)...Goncalo can explain why this works (is necessary). 😉

--
You received this message because you are subscribed to the Google Groups "Bonsai Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users...@googlegroups.com.
Visit this group at https://groups.google.com/group/bonsai-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/bonsai-users/d5bbe8d1-37f6-4b56-b0dc-c6233d006e79%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

 

Bruno Cruz

unread,
Mar 11, 2017, 2:31:44 PM3/11/17
to Bonsai Users, bruno...@neuro.fchampalimaud.org
That part i managed to reverse engineer from another script :P (the attached workflow already had the concat node after getFiles), the problem is that even tho i have access to all the strings of the files in the folder, the script only runs the first one

Gonçalo Lopes

unread,
Mar 11, 2017, 2:54:14 PM3/11/17
to Bruno Cruz, Bonsai Users
Hi Bruno,

The problem is that each video needs to be processed in its own workflow. When FileCapture starts, it opens a video, reads all the frames until the end, and then it completes successfully. However, here you want FileCapture to start everything all over again once it is done with a video.

In order to do this, you need to either Repeat the workflow again but now with the next video, or you need to set things up such that each filename actually creates a new processing workflow, and then process all of them. In this case, Repeat has a problem in that all your filenames are actually coming in at once because of GetFiles.

It is easier if you formalize the problem in the following way:


1. GetFiles outputs a sequence of arrays of strings with only one element - the array of filenames.

2. Concat is used to iterate over each string in the array and produce a sequence of strings corresponding to each filename (i.e. from a sequence with only one array of strings, we get a sequence of many strings, one for each filename).

3. ProcessVideo is a WindowWorkflow describing all the processing you want to do with the video, including saving, etc. Basically it says, for every filename, create a new Workflow (now we have a sequence of Workflows).

4. Concat is used again to say "process all the workflows, one after the other".

ProcessVideo can then be specified simply like so:



This way you can separate the two main problems in your scenario, processing a single video and finding the videos to process.

I'm attaching a modified workflow that should do something similar to what you wanted.

To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users+unsubscribe@googlegroups.com.
takeVideoDerivative_v2.bonsai

Bruno Cruz

unread,
Mar 11, 2017, 6:57:41 PM3/11/17
to Bonsai Users, bruno...@neuro.fchampalimaud.org
Thanks for the solution gonçalo! I understand the problem you just described however i am still a bit confused as to how the second concat actually achieves that purpose. Especially, because i have only used concat to achieve results similar to what the first instance of the node in the script is doing (i.e. generate multiple "entries"). Whenever you have time could you elaborate a little bit as to how that second concat actually works?

Gonçalo Lopes

unread,
Mar 12, 2017, 11:04:36 AM3/12/17
to Bruno Cruz, Bonsai Users
I understand your difficulties. The problem is that when I started to introduce people to Bonsai I avoided discussing what is really going on behind the scenes in order to simplify the explanation of basic usage. That was a mistake on my part that I will try to rectify in the near future with a set of tutorial videos and a proper introduction.

Without those fundamental concepts in mind, it is simply impossible to understand what is going on when higher-order nodes like WindowWorkflow or SelectMany appear in the examples, and I cannot emphasize enough that they are really the most powerful pieces in the Bonsai language.

Nevertheless, I can try and provide a basic intuition. Let's think about what happens when you have the following workflow in Bonsai:


This is just a FileCapture by itself. When you hit "play" in Bonsai, it will:

1. Open the file
2. Output all the frames, one after the other
3. Close the file and then Bonsai will "stop".

I can add more nodes to the workflow in order to process the frames and save data, and so on, but in essence, this workflow will process one video, and only one video.

Imagine that I now had 10 videos to process. Well, I could still use this workflow, but I would have to manually change the FileName property to point to each video, play the workflow, wait until its done, and then change to the next video.

If you want to automate this process to many videos, the tricky bit to think about is: if a workflow can only process one video and then stops, how exactly do you start the workflow again after that one video is done?

One possibility that is easy to understand is to have something like this:


In this case, what the Repeat node is doing is the following: whenever the sequence that is upstream of Repeat terminates, start it again.

This means that now when FileCapture closes the video and terminates the sequence, Repeat will ask FileCapture to start the process all over again (open the video, read all frames and close). Every time, whenever FileCapture reaches the end, Repeat will start the node again, forever.

In practice what this means is that the video will loop endlessly. However, if you could somehow magically change the name of the file at the time when FileCapture is done with one video, then when Repeat starts the sequence again, it would now start playing the next video.

This would not be impossible, but it leads to a somewhat clunky solution.

Another way to control how workflows play and stop is by using WindowWorkflow. The best analogy I can make to explain WindowWorkflow without going in detail to the fundamentals, is to say that WindowWorkflow creates new workflows.

For example, the following workflow:


What happens here is that whenever a key is pressed, a new workflow is created with whatever is nested inside the WindowWorkflow node.

The important bit is that the workflow is created, but is not started. This means that just by having WindowWorkflow there, nothing will happen. You have a bunch of workflows, but you still need to start them. The reason this is important is because this gives you the possibility to decide when to start them. You can start them in parallel (Merge), start only the latest one (Switch), or start them one after the other (Concat).

That is what the last Concat is doing, it is taking all the workflows created by WindowWorkflow and starting them one after the other, i.e. when one workflow stops, the next one starts.

This is the best description I can do for now. Hope it helps and soon I will try to put out the video tutorials on the fundamentals.


To unsubscribe from this group and stop receiving emails from it, send an email to bonsai-users+unsubscribe@googlegroups.com.

Bruno Cruz

unread,
Mar 12, 2017, 12:35:56 PM3/12/17
to Bonsai Users, bruno...@neuro.fchampalimaud.org
Thanks for the explanation i think i have a better intuition of what you meant before, especially the subtle difference between the workflow being created and actually played.
Looking forward for those tutorials ;)

Thanks,
B
Reply all
Reply to author
Forward
0 new messages