wip
This commit is contained in:
121
camera.py
121
camera.py
@@ -24,22 +24,31 @@ import epics
|
||||
import numpy as np
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
global imv
|
||||
|
||||
class CamMode(enum.IntEnum):
|
||||
OFF = 0
|
||||
RUNNING = 1
|
||||
|
||||
|
||||
class camera(object):
|
||||
class epics_cam(object):
|
||||
|
||||
def __init__(self, basename="ESB-MX-CAM"):
|
||||
def __init__(self, prefix="ESB-MX-CAM"):
|
||||
self._pv=pv=dict()
|
||||
os.environ['EPICS_CA_ADDR_LIST'] = 'localhost'
|
||||
if basename[-1]!=':': basename+=':'
|
||||
pv['pic']=epics.PV(basename+"FPICTURE", auto_monitor=True, callback=self.new_frame_callback)
|
||||
for k,v in (('w','WIDTH'),('h','HEIGHT'),):
|
||||
pv[k]=epics.PV(basename+v)
|
||||
self._sz=(int(pv['w'].value), int(pv['h'].value))
|
||||
return
|
||||
if prefix[-1]!=':': prefix+=':'
|
||||
self._prefix=prefix
|
||||
pv_w=self.getPv('WIDTH');pv_h=self.getPv('HEIGHT')
|
||||
self._sz=(int(pv_w.value), int(pv_h.value))
|
||||
|
||||
def new_frame_callback(self, **kwargs):
|
||||
def getPv(self,name):
|
||||
try:
|
||||
pv=self._pv[name]
|
||||
except KeyError:
|
||||
prefix=self._prefix
|
||||
pv=epics.PV(prefix+name)
|
||||
self._pv[name]=pv
|
||||
return pv
|
||||
|
||||
def new_frame_cb(self, **kwargs):
|
||||
"""kargs contains:
|
||||
pvname: the name of the pv
|
||||
value: the latest value
|
||||
@@ -68,11 +77,14 @@ class camera(object):
|
||||
chid: integer channel ID
|
||||
cb_info: (index, self) tuple containing callback ID
|
||||
and the PV object"""
|
||||
print('new_frame_callback')
|
||||
print('camera.new_frame_pv_cb')
|
||||
|
||||
def get_image(self):
|
||||
print('camera.get_image')
|
||||
try:
|
||||
pv=self._pv
|
||||
pv_pic=self.getPv('FPICTURE')
|
||||
sz=self._sz
|
||||
pic = pv['pic'].get(count=sz[0]*sz[1], as_numpy=True).reshape(sz[::-1])
|
||||
pic = pv_pic.get(count=sz[0]*sz[1], as_numpy=True).reshape(sz[::-1])
|
||||
except AttributeError as e:
|
||||
logger.warning("failed to fetch image")
|
||||
else:
|
||||
@@ -89,17 +101,34 @@ class camera(object):
|
||||
pic=pic[::trf[0,1],::trf[1,0]].T
|
||||
|
||||
self.pic=pic
|
||||
print(pic[-1][-1])
|
||||
imv.setImage(pic, autoRange=False, autoLevels=False)
|
||||
|
||||
def get_image(self):
|
||||
pv=self._pv
|
||||
try:
|
||||
pic = self.pic
|
||||
except AttributeError:
|
||||
self.new_frame_callback()
|
||||
pic = self.pic
|
||||
return pic
|
||||
def set_exposure(self,exp):
|
||||
pv_exp=self.getPv('EXPOSURE')
|
||||
pv_exp.put(exp, wait=True)
|
||||
pass
|
||||
|
||||
def set_roi(self,rxs,rxe,rys,rye):
|
||||
pv_rxs=self.getPv('REGIONX_START');pv_rxe=self.getPv('REGIONX_END')
|
||||
pv_rys=self.getPv('REGIONY_START');pv_rye=self.getPv('REGIONY_END')
|
||||
self.update_params((pv_rxs,rxs),(pv_rxe,rxe),(pv_rys,rys),(pv_ryerye))
|
||||
|
||||
def set_binning(self,bx,by):
|
||||
pv_bx=self.getPv('BINX');pv_by=self.getPv('BINY')
|
||||
self.update_params((pv_bx,bx),(pv_by,by))
|
||||
|
||||
#def update_params(self, **kargs):
|
||||
def update_params(self, *args):
|
||||
"""update parameters on camera"""
|
||||
for pv, val in args:
|
||||
logging.debug("updating {} = {}".format(pv, v))
|
||||
pv.put(val, wait=True)
|
||||
pv_camera=self.getPv('CAMERA')
|
||||
pv_set_param=self.getPv("SET_PARAM")
|
||||
|
||||
pv_camera.put(CamMode.OFF, wait=True)
|
||||
pv_set_param.put(1, wait=True)
|
||||
pv_camera.put(CamMode.RUNNING, wait=True)
|
||||
#TODO: update self._sz
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
@@ -111,19 +140,13 @@ if __name__ == "__main__":
|
||||
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--ui", "-u", help="qt test", action="store_true")
|
||||
parser.add_argument("--base-pv","-b",help="PV prefix for images; default=ESB-MX-CAM",type=str,default="ESB-MX-CAM",)
|
||||
parser.add_argument("--prefix","-p",help="PV prefix for images; default=ESB-MX-CAM",type=str,default="ESB-MX-CAM",)
|
||||
args = parser.parse_args()
|
||||
|
||||
print('STARTING')
|
||||
os.environ['EPICS_CA_ADDR_LIST']='localhost'
|
||||
cam = camera(basename=args.base_pv)
|
||||
#pv = cam._pv
|
||||
#w, h = int(pv['w'].value), int(pv['h'].value)
|
||||
#a = np.arange(w*h, dtype=np.uint16)
|
||||
#pv['pic'].put(a)
|
||||
#print(pv['pic'].get())
|
||||
|
||||
if not args.ui:
|
||||
cam = epics_cam(prefix=args.base_pv)
|
||||
n = 1
|
||||
base = time.strftime("/tmp/image_%Y%m%d_%H%M%S_{:05d}.png")
|
||||
while True:
|
||||
@@ -140,6 +163,36 @@ if __name__ == "__main__":
|
||||
exit(0)
|
||||
n += 1
|
||||
else:
|
||||
class UIcamera(epics_cam):
|
||||
|
||||
def __init__(self, prefix="ESB-MX-CAM"):
|
||||
epics_cam.__init__(self,prefix)
|
||||
self._pv['pic'] = epics.PV(self._prefix + "FPICTURE", auto_monitor=True, callback=self.new_frame_pv_cb)
|
||||
pass
|
||||
|
||||
def new_frame_pv_cb(self, **kwargs):
|
||||
pv = self._pv
|
||||
sz = self._sz
|
||||
if kwargs['count']==sz[0] * sz[1]:
|
||||
pic = kwargs['value'].reshape(sz[::-1])
|
||||
else:
|
||||
self._sz = (int(pv['w'].value), int(pv['h'].value))
|
||||
print('new_frame_pv_cb SIZE ERROR:',pic[-1][-1],kwargs['count'])
|
||||
return
|
||||
print('new_frame_pv_cb',pic[-1][-1],kwargs['count'])
|
||||
if pic.dtype==np.int16:
|
||||
pic.dtype=np.uint16
|
||||
try:
|
||||
trf=self._transformation
|
||||
except AttributeError as e:
|
||||
pass
|
||||
else:
|
||||
if trf[1,0]==0:
|
||||
pic=pic[::trf[0,0],::trf[1,1]]
|
||||
else:
|
||||
pic=pic[::trf[0,1],::trf[1,0]].T
|
||||
imv.setImage(pic, autoRange=False, autoLevels=False)
|
||||
|
||||
import sys
|
||||
from pyqtgraph.Qt import QtCore, QtGui
|
||||
import pyqtgraph as pg
|
||||
@@ -178,7 +231,9 @@ if __name__ == "__main__":
|
||||
|
||||
## Display the data and assign each frame a time value from 1.0 to 3.0
|
||||
imv.setImage(data, xvals=np.linspace(1., 3., data.shape[0]))
|
||||
imv.setImage(cam.get_image())
|
||||
cam = UIcamera(prefix=args.prefix)
|
||||
cam.get_image()
|
||||
imv.setImage(cam.pic)
|
||||
|
||||
## Set a custom color map
|
||||
colors = [
|
||||
|
||||
Reference in New Issue
Block a user