TheImagingSource / ic4-examples

IC4 Example Programs
Apache License 2.0
5 stars 3 forks source link

Using Imaging Source Camera (IC4) for experiments with Python #25

Open KalpitBakal opened 3 days ago

KalpitBakal commented 3 days ago

Hello! I have been using Imaging Source camera with IC Capture for my microscopy experiments for a while now. However, now I want to automate a few things and save the new API of IC4 in Python. I will explain a few things what I want to achieve. It would be great if you could nudge me in the right directions with the examples and the documentation. I have a basic knowledge in Python. So here it goes,

  1. I want to use the Imaging Source camera to display the live video in a window. I know I could use cv2 to show the webcam of my laptop live. What function/class should I look into to display the live video of the Imaging Source Camera on window.

  2. In addition to point 2, I want to save this live video into a folder just as I can do in the IC Capture application. I would like to set the format, fps etc

  3. In addition to point 2 and 3, I would like to snap an image after a set time and save it to a particular folder. Is it possible to use time.sleep() and not disrupt the live video display and saving? Because I know this is a problem when I want to use it with cv2 and the webcam.

If doing all this 3 steps in one python function is not possible, please let me know that too. This is a general description of my project but if you want more details I will give more explanation happily.

TIS-Tim commented 3 days ago

The qt6-first-steps example shows how to display live video: https://github.com/TheImagingSource/ic4-examples/tree/master/python/qt6/qt6-first-steps

The demoapp example expands on this to manually allow saving single images and video files: https://github.com/TheImagingSource/ic4-examples/tree/master/python/qt6/demoapp

The documentation contains a section explaining how to programmatically configure camera parameters like resolution and exposure time: https://www.theimagingsource.com/en-us/documentation/ic4python/guide-configuring-device.html (frame rate is not shown there, but setting ic4.PropId.ACQUISITION_FRAME_RATE will do the trick.

The easiest way to build some regular/timed image capture is probably to extend one of the Qt examples with a Qt timer. sleep will block the UI thread and make the program unresponsive.

KalpitBakal commented 3 days ago

Hello Tim,

Thank you for your quick response. I will look into these examples.

The easiest way to build some regular/timed image capture is probably to extend one of the Qt examples with a Qt timer. sleep will block the UI thread and make the program unresponsive.

Will this stop showing the live video and stop saving the video in the folder as well?

TIS-Tim commented 3 days ago

Yes, rendering using ic4.pyside6.DisplayWidget requires main thread interaction. If you do time.sleep on the main thread, live display will halt.

KalpitBakal commented 3 days ago

Then basically I will have to use Parallel Processing in Python and run this on a separate thread?

TIS-Tim commented 3 days ago

No, just set up a timer: https://doc.qt.io/qtforpython-6/PySide6/QtCore/QTimer.html

In the connected callback, do what you want to do after the time has elapsed, like capture the next image. No need to create threads.

KalpitBakal commented 3 days ago

No, just set up a timer: https://doc.qt.io/qtforpython-6/PySide6/QtCore/QTimer.html

In the connected callback, do what you want to do after the time has elapsed, like capture the next image. No need to create threads.

But will this still stop the live display and saving video to a file right?

TIS-Tim commented 3 days ago

No, that will just register a function to be called later. The program continues on doing whatever it does.

KalpitBakal commented 3 days ago

On a side note, if we use the Imaging Source Camera as a webcam, will there a problem like jumping frames amongst many others? Or you think it will work smoothly?

TIS-Tim commented 3 days ago

What do you mean by "use as a webcam"?

Please direct questions unrelated to ic4 to our normal support channel: https://www.theimagingsource.com/en-de/company/contact/