bothlab / pomidaq

Portable Miniscope Data Acquisition
GNU Lesser General Public License v3.0
20 stars 7 forks source link

Inquiry Regarding Video File Location for UCLA Miniscope Operation #13

Closed tamai8810 closed 5 months ago

tamai8810 commented 8 months ago

Dear Ximion,

I am currently in the process of modifying the 'pomidaq-master/py/example.py' script on a Linux system to operate the UCLA miniscope. Upon reviewing the script, I noted that the video file (identified as VIDEO_FILENAME) is presumably saved to '/tmp/miniscope-test.mkv'. However, after executing the script, I was unable to locate the '/tmp' directory or the aforementioned video file.

Could you please provide guidance on where the recorded video files are actually stored, or if there are additional steps required to access them?

Thank you for your assistance.

Best regards, Hayato

ximion commented 8 months ago

Hi Hayato!

Can you give some more details on what you are trying to do? :-) (Potentially, Syntalos would be an option as well) Also, fairly obvious question: What happens if you select a different path?

I made a slight tweak to example.py, can you test if that changed the behavior for you?

Best, Matthias

tamai8810 commented 8 months ago

Mattias

Thank you for your patience and support.

I must apologize for my earlier misunderstanding. Initially, I was under the impression that example.py would automatically save the video files in a directory named /tmp. However, it appears that this assumption was incorrect.

My actual objective is to export the frames captured by the miniscope into a video format, such as .mp4 or .avi, and to save these files to a specified location of my choosing. To accomplish this, I have made modifications to example.py and also created a following new script.

example_hayato0315.py

Could you please review my approach to see if there might be any errors or oversight on my part?

Your insights would be greatly appreciated.

Best regards, Hayato

tamai8810 commented 8 months ago

I successfully executed the scripts, which resulted in the video being saved to /videos/recordedVideo.avi. However, I encountered an issue where the saved video file does not play.

IMG_3693

IMG_3695

Unfortunately, I am unable to provide a screenshot of the error or the script's output.

ximion commented 8 months ago

I must apologize for my earlier misunderstanding. Initially, I was under the impression that example.py would automatically save the video files in a directory named /tmp. However, it appears that this assumption was incorrect.

It's almost correct, it will save the video as the respective filename. Replace /tmp/miniscope-test.mkv with another absolute (or relative) path and you should be good :-)

My actual objective is to export the frames captured by the miniscope into a video format, such as .mp4 or .avi, and to save these files to a specified location of my choosing.

Why wouldn't you use PoMiDAQ directly for that but use the Python scripting interface? It does that already :-) I would strongly advise against using MP4 as container, there have been problems with it in the past. MKV is by far the best due to the metadata that can be added and the relative modernity of the format and much better error recovery in case of partially damaged file as well as streaming support. However, if you need an older format, AVI is also a viable choice.

Could you please review my approach to see if there might be any errors or oversight on my part?

You don't need any of the added code, just give the Miniscope code a proper filename and it will do the right thing. You can also set a different container format via mscope.video_container (use VideoContainer.MATROSKA or VideoContainer.AVI).

Besides that, in your code: fourcc = cv2.VideoWriter_fourcc('d', 'i', 'v', '3') - using DivX is a terrible choice, as it is a lossy compression codec which will loose you data from your recording (these codecs aren't made for scientific data like this, you for sure want a lossless codec) - in addition, DivX isn't even widely supported anymore, which is most likely why you can't play the video. Try the built-in video recorder, it will save you a lot of effort :-)

tamai8810 commented 8 months ago

I realize my previous explanations may have overly simplified my objectives. To clarify, my ultimate goal involves a more intricate process with the miniscope frames. Specifically, I aim to:

  1. Divide the captured frames into odd and even frames.
  2. Display these frames in real-time.
  3. Convert and save each frame as an individual .avi file.

However, I am uncertain about how to effectively implement this process, particularly the real-time display of divided frames and their subsequent conversion into .avi format.

That is why, I edited raw Python script for implementing recording function provided by cv2 as follows:

example_hayato0320.py

Can this be accomplished by using the PoMiDAQ feature?

Thank you for your understanding and for any guidance you can provide.

ximion commented 8 months ago

Ah, that clears things up!

The reason your code does not work is because you are configuring the video encoders wrong (not the right codec and not the right frame dimensions). However, I am a bit hesistant to resolve this for a different reason: You are using the mscope.current_disp_frame for recording. This frame potentially has things like indicators applied and changes with the display settings, but much more importantly, if your code is too slow with grabbing the frame, you will loose data during your recording.

The solution for that would be to give the Python API access to the raw frame callback, like the C++ API has. It's not too hard to implement, but I currently have very little time to do so. However, another software I write, Syntalos, can solve your problem with ease and much less code, so I just quickly tested it:

grafik

(the Miniscope was in use, so I used a webcam, which is why this looks odd) What is going on here is that you can see the Miniscope data flow into a Python script module which splits it into odd and even frames and then exports the result into two separate displays and also two separate recorders which save it to disk in a lossless format.

You can get Syntalos from Flathub: https://flathub.org/apps/io.github.bothlab.syntalos

I am currently improving the Python support, so it is a bit janky, but otherwise works. Here is the configuration file to load, if you want to play with it: SyntalosVideoSplitExample.zip

I'll look into adding the necessary API to the Miniscope Python bindings as well, but I don't know yet when I'll get to it. You can try to fix your video recorder and the Python script will most likely be fast enough so no frames are silently dropped, but that's not something I'd be happy with... But maybe Syntalos already does what you want to achieve :-)

tamai8810 commented 8 months ago

Thank you for your swift response and the valuable insights provided.

I'm eager to share the outcomes soon after trying Syntalos.

tamai8810 commented 7 months ago

Thanks for sharing your solution. Syntaros is a great way to go.

Thanks to you, I can start experimenting.

スクリーンショット 2024-03-29 135930

ximion commented 7 months ago

Fantastic! I will keep this issue open to eventually expand the Python API here a bit. I am also currently working a lot on Syntalos, so please let me know if you find any issues or things to improve over there!