105 lines
4.4 KiB
Python
105 lines
4.4 KiB
Python
run("Devices/Elements")
|
|
|
|
def _create_tables( stream_value, paths, data_type, shape):
|
|
create_dataset(paths["image"], data_type, dimensions = [images, shape[0], shape[1]])
|
|
create_dataset(paths["pid"], 'l', dimensions = [images])
|
|
create_dataset(paths["timestamp_str"], 's', dimensions = [images])
|
|
for id in stream_value.identifiers:
|
|
val = stream_value.getValue(id)
|
|
if id == "image":
|
|
pass
|
|
elif id == "processing_parameters":
|
|
val = json.loads(val)
|
|
for key in val.keys():
|
|
set_attribute(paths["image"], key, "" if val[key] is None else val[key] )
|
|
elif isinstance(val, PyArray):
|
|
create_dataset("/"+id, 'd', dimensions = [images, len(val)])
|
|
elif isinstance(val, PyLong):
|
|
create_dataset("/"+id, 'l', dimensions = [images])
|
|
elif isinstance(val, PyFloat):
|
|
create_dataset("/"+id, 'd', dimensions = [images])
|
|
else:
|
|
print "Unmanaged stream type: ", val, type(val)
|
|
pass
|
|
|
|
|
|
def _append_frame(stream_value, index, paths, data_type, shape):
|
|
print "Saving frame :", index
|
|
#append_dataset(paths["image"], data, index, type = data_type)
|
|
append_dataset(paths["image"], stream_value.getValue("image"),[index,0,0], type = data_type, shape=[1, shape[0], shape[1]])
|
|
append_dataset(paths["pid"], stream_value.getPulseId(), index)
|
|
append_dataset(paths["timestamp_str"], datetime.datetime.fromtimestamp(stream_value.timestampNanos/1e9).strftime('%Y-%m-%d %H:%M:%S'), index)
|
|
|
|
for id in stream_value.identifiers:
|
|
try:
|
|
val = stream_value.getValue(id)
|
|
if id == "image":
|
|
pass
|
|
elif isinstance(val, PyArray):
|
|
append_dataset("/"+id, val, index)
|
|
elif isinstance(val, PyLong):
|
|
append_dataset("/"+id, int(val), index)
|
|
elif isinstance(val, PyFloat):
|
|
append_dataset("/"+id, float(val), index)
|
|
else:
|
|
pass
|
|
except:
|
|
print id, val
|
|
traceback.print_exc()
|
|
|
|
print "Saved frame: ", index
|
|
|
|
|
|
def _write_metadata(camera_name, shared = False, images = 1, interval = -1):
|
|
set_attribute("/", "Camera", camera_name)
|
|
set_attribute("/", "Shared", shared)
|
|
set_attribute("/", "Images", images)
|
|
set_attribute("/", "Interval", interval)
|
|
cam_type = get_camera_type(camera_name)
|
|
set_attribute("/", "Type", cam_type)
|
|
if cam_type=="ELECTRONS":
|
|
set_attribute("/", "Screen", caget(camera_name + (":POSITION" if (camera_name.startswith("DSRM")) else ":GET_SCREEN1_POS"), 's'))
|
|
set_attribute("/", "Filter", caget(camera_name + ":GET_FILTER", 's'))
|
|
|
|
|
|
def save_camera_data(server, camera_name, shared = False, images = 1, interval = -1, parallel = True, pause = False):
|
|
stream_value = server.stream.take()
|
|
if stream_value is None:
|
|
server.waitNext(10000)
|
|
stream_value = server.stream.take()
|
|
|
|
paths = {"image":"/image", "pid":"/pulse_id", "timestamp_str":"/timestamp_str"}
|
|
shape = [stream_value.getValue("height"),stream_value.getValue("width")]
|
|
data_type = stream_value.getValue("image").typecode
|
|
|
|
tasks = []
|
|
if pause:
|
|
server.paused = True
|
|
try:
|
|
for i in range(images):
|
|
if i==0:
|
|
_write_metadata(camera_name, shared, images, interval)
|
|
_create_tables(stream_value, paths, data_type, shape)
|
|
|
|
start = time.time()
|
|
stream_value = server.stream.take()
|
|
if parallel:
|
|
tasks.extend( fork((_append_frame,(stream_value, i, paths, data_type, shape)),) )
|
|
else:
|
|
_append_frame(stream_value, i, paths, data_type, shape)
|
|
|
|
if i< (images-1):
|
|
if interval<=0:
|
|
server.stream.waitCacheChange(10000)
|
|
else:
|
|
sleep_time = float(interval)/1000.0 - (time.time()-start)
|
|
time.sleep(max(sleep_time,0))
|
|
finally:
|
|
if pause:
|
|
server.paused = False
|
|
pass
|
|
|
|
print "Waiting finish persisting..."
|
|
join(tasks)
|
|
print "Done"
|