Offline video analysis

332 views
Skip to first unread message

Marcelo J. Moglie

unread,
Jun 11, 2021, 2:47:17 AM6/11/21
to Bonsai Users
Hi all,

I have been searching in the group but couldn't find a precise answer to this questions. Is there any way to run an analysis on a video offline (e.g., tracking an object and saving X-Y positions) without having to replay the whole video? In any case, which would be the fastest way to do achieve? 

Kind regards,
Marcelo

brunocruz

unread,
Jun 11, 2021, 9:35:12 AM6/11/21
to Bonsai Users
Hi Marcelo,

The general workflow will probably be FileCapture -> "Processing Pipeline" -> CsvWritter. (check an example attached)
Since you are analyzing the video offline, you can set the PlaybackRate property (in the FileCapture node) to a very large value in order to let the analysis run as fast as possible.
Additionally, if you are interested in batch processing, you might want to look at the possibility of running bonsai without the visualizer Bonsai-CLI
Hope this helps,
Bruno
OfflineCentroidTracking.bonsai

Marcelo J. Moglie

unread,
Jun 12, 2021, 10:23:05 AM6/12/21
to Bonsai Users
Hi Bruno, 

Thank you very much for your response. It seems to be exactly what I was looking for! I will try these and write again in case I encounter any issues. 

Kind regards,
Marcelo

Marcelo J. Moglie

unread,
Oct 7, 2021, 5:19:54 AM10/7/21
to Bonsai Users
Hi all, 

The suggestions provided by Bruno was extremelly helpful. However, when I run the video analysis, only the CPU is being used and I cannot find the way to make it run on the GPU. I'm using a pipeline like  FileCapture -> "Processing Pipeline" -> CsvWritter with 5 GB videos and it would be much faster if I could make use of all the computer resources. Is there any way to achieve this on Bonsai? 

Many thanks,
Marcelo

Gonçalo Lopes

unread,
Dec 29, 2021, 7:36:33 PM12/29/21
to Marcelo J. Moglie, Bonsai Users
Hi Marcelo,

There are some image processing steps which could be parallelized into the GPU using OpenCV's GpuMat structure. This would require some low-level interface though, as it is not currently exposed by Bonsai.

However, not all OpenCV functions are available in the GPU since some of them depend on sequential processing and are not easily parallelizable. Unfortunately FindContours is one of them: c++ - Isn't there a OpenCV Cuda function similar to findContours? - Stack Overflow

This means that even with access to GpuMat you would still need to find some alternative means of processing blobs in the GPU without FindContours. If this is just for detecting one blob you might get away with extracting the centroid by averaging x,y coordinates of white pixels, essentially computing image moments, but even that I can't seem to find on the GPU module of OpenCV.

This is something we have in mind to improve, but there is a bit of work to be done before we can get this to be as practical as the CPU.

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/bonsai-users/d9b70cc6-954f-40df-ad22-fb00998a24f7n%40googlegroups.com.

Marcelo J. Moglie

unread,
Dec 30, 2021, 12:52:17 PM12/30/21
to Bonsai Users
Hi Gonçalo,

Thanks for your reply and the suggestions on how to achieve it. In this case, I decided to go with the standard CPU way to do it but it would be great to have something that allows a practical way to implement GPU parallelization in the future as you mentioned. 

Many thanks!! 

Marcelo

Marcelo J. Moglie

unread,
Jan 13, 2022, 12:16:33 PM1/13/22
to Bonsai Users
Hi Bruno and  Gonçalo ,

Both your suggestion have been really helpful and I managed to make my workflow run  without the visualizer and batch process multiple files using an external python routine. However, I am struggling to make work the last bit of the code. 

Everytime I launch a new Bonsai instance, the video starts running and is analyzed, writing the values to a csv file. However, once it reaches the end of the movie, Bonsai doesn't stop and I am forced to stop it manually. Is there anyway in which I could configure it to stop running whenever FileCapture reaches the last frame? 

Attached you can find a sample of the workflow I have been using where the file paths are being set externally. 

All the best, 
Marcelo
Video_post_analysis_Sample.bonsai
Reply all
Reply to author
Forward
0 new messages