Automatic video recording start and end

1,005 views
Skip to first unread message

Hideki Miwa

unread,
Jun 25, 2019, 1:53:02 PM6/25/19
to Bonsai Users

Hi everyone,


I am a new user for Bonsai and am using it to track a mouse in the open field test, but I have a trouble. 

I need to automatically start video recording if a mouse is put into the open field and then stop the recording at a designed recording duration (for example, 30 min).

 

I´m using the following workflow.

Camera Capture > Crop > Grayscale > Threshold > FindContours > BinaryRegionAnalysis > LargerstBinaryRegion > SourceArea > GreaterThan > VideoWriter


I'm trying to define the start recording point when "LargestBinaryRegin" is greater than a designed value (nearly equal to an averaged mouse size I use in experiments).

I tried to use Videowriter connecting to the GreaterThan but it did not work.

I don’t know how to define the recording duration. Should I use “Timer”?

 

Apologies if this has been asked previously, but I couldn't find a solution.

 

Thank you for your help.

 

Best,

Hideki

Gonçalo Lopes

unread,
Jun 25, 2019, 7:49:07 PM6/25/19
to Hideki Miwa, Bonsai Users
Hi Hideki and welcome to the forums,

This is an interesting question and a good exercise. You can use the SkipWhile operator to specify when to start recording, however because you want to record both the video and potentially tracking, it is best to separate the workflow into 2 parts. The first part extracts what should be recorded, the second one specifies when it should be recorded, like so (also attached a workflow for Bonsai 2.4-preview):

image.png

As you can see, I've defined two PublishSubject variables (Video and Tracking) in the top part. Then on the bottom half I decide when to start and stop the recording by using TakeUntil, SubscribeWhen and SkipWhile.

SkipWhile ignores values until some condition, SubscribeWhen starts the workflow only when something happens and TakeUntil terminates the workflow when something happens.

This way you can keep both your processing logic and start/stop recording logic clean and modular.

Hope this helps.

--
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/d9f08ad4-5f14-4ae5-9fd0-8a7996e3c8c2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
RecordingDynamicStartStop.bonsai

Vincent Prevosto

unread,
Jun 26, 2019, 9:47:24 AM6/26/19
to Bonsai Users
This schema is so useful for many applications that it could be added to the Bonsai gallery (I don't think there's anything similar).

On Tuesday, June 25, 2019 at 7:49:07 PM UTC-4, goncaloclopes wrote:
Hi Hideki and welcome to the forums,

This is an interesting question and a good exercise. You can use the SkipWhile operator to specify when to start recording, however because you want to record both the video and potentially tracking, it is best to separate the workflow into 2 parts. The first part extracts what should be recorded, the second one specifies when it should be recorded, like so (also attached a workflow for Bonsai 2.4-preview):

image.png

As you can see, I've defined two PublishSubject variables (Video and Tracking) in the top part. Then on the bottom half I decide when to start and stop the recording by using TakeUntil, SubscribeWhen and SkipWhile.

SkipWhile ignores values until some condition, SubscribeWhen starts the workflow only when something happens and TakeUntil terminates the workflow when something happens.

This way you can keep both your processing logic and start/stop recording logic clean and modular.

Hope this helps.

On Tue, 25 Jun 2019 at 18:53, Hideki Miwa <hidek...@gmail.com> wrote:

Hi everyone,


I am a new user for Bonsai and am using it to track a mouse in the open field test, but I have a trouble. 

I need to automatically start video recording if a mouse is put into the open field and then stop the recording at a designed recording duration (for example, 30 min).

 

I´m using the following workflow.

Camera Capture > Crop > Grayscale > Threshold > FindContours > BinaryRegionAnalysis > LargerstBinaryRegion > SourceArea > GreaterThan > VideoWriter


I'm trying to define the start recording point when "LargestBinaryRegin" is greater than a designed value (nearly equal to an averaged mouse size I use in experiments).

I tried to use Videowriter connecting to the GreaterThan but it did not work.

I don’t know how to define the recording duration. Should I use “Timer”?

 

Apologies if this has been asked previously, but I couldn't find a solution.

 

Thank you for your help.

 

Best,

Hideki

--
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...@googlegroups.com.

Hideki Miwa

unread,
Jun 26, 2019, 10:26:47 AM6/26/19
to Bonsai Users

Thanks Gonçalo !


This is almost what I wanted to do. 

I'll play around with this workflow for a while.
Please let me ask you again if I don't understand something.

>Prevosto
Yes! I agree with you. 
I hope that this workflow is helpful to all Bonsai users.

Best, 
Hideki
2019年6月26日水曜日 22時47分24秒 UTC+9 Vincent Prevosto:

aaron.sa...@gmail.com

unread,
Apr 20, 2021, 9:57:48 AM4/20/21
to Bonsai Users
Hi Gonçalo

I am performing a similar experiment as Hideki, with the time spent recording being the only minor difference.

I tried using the same workflow that you shared, however, the output is strange. I just get a brief < 1 s video and CSV file with one value as soon as I start the workflow. I thought I wasn't letting it run long enough, however, when I did let it run for 5 minutes, I get the same issue.

One related question, other than why I this workflow is not working is: if the experiment ran for less than the pre-defined time, will the workflow record till whatever time it ran? Or do I have to wait till the pre-defined time is completed to get a valid data file?
Best
Aaron
3chambertest.bonsai

aaron.sa...@gmail.com

unread,
Apr 20, 2021, 4:43:26 PM4/20/21
to Bonsai Users
Actually, now it seems to be working. Not sure why. I suppose it had something to do with the area values that I had specified, but not sure. Will update in case I figure out why it was not working previously.

Thanks!
Aaron
Reply all
Reply to author
Forward
0 new messages