Running a sCMOS camera using a software with GUI is nice, but at some point I want to control the camera from Jupyter notebook, so that I can acquire images and analyse them on the same page of code. Unfortunately many camera manufacturers don't provide Python API (any of them do?). So the hard way would to ferret out camera's drivers (.dll files), figure out the names and arguments of functions, and write a home-made Python wrapper for a DLL file using ctypes, like I did for Thorlabs wavefront sensor. This is a way of tears and pain.
Luckily, there is a silk road to the camera control. The API for many cameras and other instruments is already implemented in MicroManager, which also has a Python wrapper MMCorePy around it! So, after quick and easy installation of the MMCorePy, Python code becomes simple and clean, with all heavy lifting done in MicroManager API running under the hood:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This python code uses MicroManager device drivers to control a camera. | |
# Tested on Hamamatsu Orca Flash 4 camera, should work similarly on other cameras. | |
# Original idea: Kay Schink @koschink | |
# Gist implementation: Nikita Vladimirov @nvladimus | |
#Environment setup instructions: https://micro-manager.org/wiki/Using_the_Micro-Manager_python_library | |
import sys | |
sys.path.append("C:\\Program Files\\Micro-Manager-1.4") | |
import MMCorePy #load MicroManager for device control | |
import matplotlib.pyplot as plt | |
#load camera and set it up | |
mmc = MMCorePy.CMMCore() | |
mmc.getVersionInfo() | |
mmc.loadDevice('Camera', 'HamamatsuHam', 'HamamatsuHam_DCAM') | |
mmc.initializeAllDevices() | |
mmc.setCameraDevice('Camera') | |
#Optional block: set an output trigger for synchronization with other devices | |
#mmc.getDevicePropertyNames('Camera') #execute if forgot prop names | |
#mmc.getProperty('Camera','OUTPUT TRIGGER PERIOD UNITS') #seconds | |
#mmc.getAllowedPropertyValues('Camera','OUTPUT TRIGGER KIND[0]') #check what's allowed | |
mmc.setProperty('Camera','OUTPUT TRIGGER KIND[0]','PROGRAMABLE') | |
mmc.setProperty('Camera','OUTPUT TRIGGER PERIOD[0]',0.001) | |
mmc.setProperty('Camera','OUTPUT TRIGGER POLARITY[0]','POSITIVE') | |
mmc.setExposure(20) #ms | |
mmc.snapImage() | |
img = mmc.getImage() #this is numpy array, by the way | |
plt.imshow(img, cmap='gray') | |
# pretty image should be displayed here | |
mmc.reset() #say good bye |
Comments
Post a Comment